class Reposync::ClientService < ApplicationService
  attr_reader :url, :params 

  def initialize(options={})
    @url = options[:url]
    @params = options[:params]
  end

  def post(url, params={})
    puts "[reposync][POST] request params: #{params}"
    conn.post do |req|
      req.url full_url(url)
      req.body = params[:data].to_json 
    end
  end

  def get(url, params={})
    puts "[reposync][GET] request params: #{params}"
    conn.get do |req|
      req.url full_url(url, 'get')
      params.each_pair do |key, value|
        req.params["#{key}"] = value
      end
    end
  end

  def delete(url, params={})
    puts "[reposync][DELETE] request params: #{params}"
    conn.delete do |req|
      req.url full_url(url)
      req.body = params[:data].to_json
    end
  end

  def patch(url, params={})
    puts "[reposync][PATCH] request params: #{params}"
    conn.patch do |req|
      req.url full_url(url)
      req.body = params[:data].to_json
    end
  end

  def put(url, params={})
    puts "[reposync][PUT] request params: #{params}"
    conn.put do |req|
      req.url full_url(url)
      req.body = params[:data].to_json
    end
  end

  def conn 
    @client ||= begin
      Faraday.new(url: domain) do |req|
        req.request :url_encoded
        req.headers['Content-Type'] = 'application/json'
        req.adapter Faraday.default_adapter
        req.options.timeout = 100           # open/read timeout in seconds
        req.options.open_timeout = 10      # connection open timeout in seconds
      end
    end

    @client
  end

  def domain
    EduSetting.get("reposync_api_domain") || "http://106.75.110.152:50087"
  end

  def full_url(api_rest, action='post')
    url = [domain, api_rest].join('').freeze
    url = action === 'get' ? url : URI.escape(url)
    url = URI.escape(url) unless url.ascii_only?
    puts "[reposync] request url: #{url}"
    return url
  end

  def log_error(status, body)
    puts "[reposync] status:  #{status}"
    puts "[reposync] body:    #{body}"
  end

  def render_response(response)
    status = response.status
    body = JSON.parse(response&.body)

    log_error(status, body)

    if status == 200 
      if body["code_status"].to_i == 0
        return [body["code_status"], body["data"], body["msg"]]
      else 
        puts "[reposync][ERROR] code: #{body["code_status"]}"
        puts "[reposync][ERROR] message: #{body["msg"]}"
        return [body["code_status"], body["data"], body["msg"]]
      end
    end
  end

  def render_list_response(response)
    status = response.status
    body = JSON.parse(response&.body)

    log_error(status, body)

    if status == 200 
      if body["code_status"].to_i == 0
        return [body["code_status"], body["data"], body["total"], body["msg"]]
      else 
        puts "[reposync][ERROR] code: #{body["code_status"]}"
        puts "[reposync][ERROR] message: #{body["msg"]}"
        return [body["code_status"], body["data"], body["total"], body["msg"]]
      end
    end
  end
end