class Sms::UcloudService < ApplicationService
  attr_reader :phone, :code, :send_type

  def initialize(phone, code, send_type)
    @phone = phone
    @code = code
    @send_type = send_type
  end

  def call
    public_key = EduSetting.get("ucloud_public_key") || "4Z7QYDY0SumplMtmNmd9PERgPPFiMpR1R"
    private_key = EduSetting.get("ucloud_private_key") || "7wxMoGoaQ1DtcQjDxgJrOGOXnIiZq4amEWvmi7eBtm2d"

    project_id = "org-3ozbh2"
    sign_params = {
      "Action" => "SendUSMSMessage",
      "ProjectId" => project_id,
      "TemplateId" => get_template_id(@send_type),
      "PublicKey" => public_key,
      "PhoneNumbers.0" => @phone,
      "TemplateParams.0" => "#{@code}",
      "SigContent" => "GitLink确实开源"
    }
    sequence = sign_params.sort.map { |k, v| "#{k}#{v}" }.join('')
    # Rails.logger.info("create_signature=========#{sequence}#{private_key}")
    req_params = sign_params.merge("Signature" => Digest::SHA1.hexdigest("#{sequence}#{private_key}"))
    uri = URI("https://api.ucloud.cn")
    uri.query = req_params.map { |k, v| "#{k}=#{URI.escape(v.to_s)}" }.join('&')
    # Rails.logger.info("uri.query=========#{uri.query}")

    Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
      req = Net::HTTP::Get.new uri.request_uri

      response = http.request(req)
      # Rails.logger.info("ucloud sms response.body=========#{response.body}")
      result = ActiveSupport::JSON.decode(response.body)
      result['RetCode']
    end
  end

  def send_by_params(opt={})
    public_key = "4Z7QYDY0SumplMtmNmd9PERgPPFiMpRR"
    private_key = "7wxMoGoaQ1DtcQjDxgJrOGOXnIiZq4amEWvmi7eBtmd"

    project_id = "org-3ozbh2"
    sign_params = {
      "Action" => "SendUSMSMessage",
      "ProjectId" => project_id,
      "TemplateId" => "#{opt[:TemplateId]}",
      "PublicKey" => public_key,
      "PhoneNumbers.0" => "#{opt[:PhoneNumbers]}",
      "TemplateParams.0" => "#{opt[:TemplateParams]}",
      "SigContent" => "GitLink确实开源"
    }
    sequence = sign_params.sort.map { |k, v| "#{k}#{v}" }.join('')
    # Rails.logger.info("create_signature=========#{sequence}#{private_key}")
    req_params = sign_params.merge("Signature" => Digest::SHA1.hexdigest("#{sequence}#{private_key}"))
    uri = URI("https://api.ucloud.cn")
    uri.query = req_params.map { |k, v| "#{k}=#{URI.escape(v.to_s)}" }.join('&')
    # Rails.logger.info("uri.query=========#{uri.query}")

    Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
      req = Net::HTTP::Get.new uri.request_uri

      response = http.request(req)
      # Rails.logger.info("ucloud sms response.body=========#{response.body}")
      ActiveSupport::JSON.decode(response.body)
    end
  end

  def send_sms(template_id)

  end

  def GetProjectList
    public_key = "4Z7QYDY0SumplMtmNmd9PERgPPFiMpRR"
    private_key = "7wxMoGoaQ1DtcQjDxgJrOGOXnIiZq4amEWvmi7eBtmd"

    sign_params = {
      "Action" => "GetProjectList",
      "PublicKey" => public_key
    }
    sequence = sign_params.sort.map { |k, v| "#{k}#{v}" }.join('')
    Rails.logger.info("create_signature=========#{sequence}#{private_key}")
    req_params = sign_params.merge("Signature" => Digest::SHA1.hexdigest("#{sequence}#{private_key}"))
    uri = URI("https://api.ucloud.cn")
    uri.query = req_params.map { |k, v| "#{k}=#{URI.escape(v.to_s)}" }.join('&')
    Rails.logger.info("uri.query=========#{uri.query}")

    Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
      req = Net::HTTP::Get.new uri.request_uri

      response = http.request(req)
      Rails.logger.info("ucloud sms response.body=========#{response.body}")
      response.body
    end
  end


  # 1：注册手机验证码 2：找回密码手机验证码  4：绑定手机  9：验证手机号有效
  def get_template_id(send_type)
    case send_type
    when 1, 2, 9
      "UTA221114S2MGTY"
    when 4
      "UTA22112486FXLZ"
    else
      "UTA221114S2MGTY"
    end
  end

end
