module RuckusApi
  API_PATH_PREFIX = "/wsg/api/public/"
  PORT = ":8443"
  
  def self.get_API_url_endpoint sz_host
    protoprefix = sz_host.include?("https") ? "" : "https://"
    protoprefix + sz_host + PORT + API_PATH_PREFIX
    # "https://" + APPLICATION['defaults']['RUCKUS']['HOSTNAME'] + PORT + API_PATH_PREFIX
  end

  def self.make_api_call(int_type, path, method, params={}, query_params={})
    api_key = int_type.api_key
    sz_host = int_type.details['sz_host']

    url = URI(self.get_API_url_endpoint(sz_host) + path)
    query_params['serviceTicket'] = api_key if api_key.present?
    url.query = URI.encode_www_form(query_params) if query_params.present?
    
    Rails.logger.info url
    https = Net::HTTP.new(url.host, url.port)
    https.use_ssl = true
    https.verify_mode = OpenSSL::SSL::VERIFY_NONE
    
    if method == 'POST'
      request = Net::HTTP::Post.new(url)
      request["Content-Type"] = "application/json;charset=UTF-8"
      request.body = params.to_json
    elsif method == 'GET'
      puts "PATH:::::::::::::::#{path}"
 
      request = Net::HTTP::Get.new(url)
    elsif method == 'PUT'
      request = Net::HTTP::Put.new(url)
      request["Content-Type"] = "application/json;charset=UTF-8"
        p params.to_json
      request.body = params.to_json
    elsif method == 'PATCH'
      request = Net::HTTP::Patch.new(url)
      request["Content-Type"] = "application/json;charset=UTF-8"
        p params.to_json
      request.body = params.to_json
    elsif method == "DELETE"
      request = Net::HTTP::Delete.new(url)
      puts "delete method"
    end
    response = https.request(request)
    if response.code == "401"
      Rails.logger.info "[RUCKUS RESPONSE] :: [#{path}] :: --> #{response.read_body}"
      Rails.logger.info "[RUCKUS - #{response.code}] :: [#{path}] :: rest the service token"
      if(!path.include?('serviceTicket'))
        token_status = Ruckus::BaseApi.service_ticket(int_type)
        self.make_api_call(int_type, path, method, params, query_params) unless token_status.blank?
      else
        {}
      end
    else
      JSON.parse(response.read_body) rescue {}
    end
  end

  def self.send_request(api_key, method, path, params={}, query_params={})

    url = URI(self.get_API_url_endpoint + path)
    url.query = URI.encode_www_form(query_params) if query_params.present?
    p url
    https = Net::HTTP.new(url.host, url.port)
    https.use_ssl = true
    https.verify_mode = OpenSSL::SSL::VERIFY_NONE
    

    if method == 'POST'
      request = Net::HTTP::Post.new(url)
      request["Content-Type"] = "application/json;charset=UTF-8"
      request["Cookie"] = "JSESSIONID=#{JSESSIONID}"
      request["Set-cookie"] = "JSESSIONID=#{JSESSIONID}; Path=/wsg; Secure"
      
      request.body = params.to_json
    elsif method == 'GET'
      puts "PATH:::::::::::::::#{path}"

      request = Net::HTTP::Get.new(url)
      # request["X-Cisco-Meraki-API-Key"] = "#{api_key}"
      request["Set-cookie"] = "JSESSIONID=#{JSESSIONID}; Path=/wsg; Secure"
    elsif method == 'PUT'
      request = Net::HTTP::Put.new(url)
      request["Content-Type"] = "application/json;charset=UTF-8"
        p params.to_json
      # request["X-Cisco-Meraki-API-Key"] = "#{api_key}"
      request["Set-cookie"] = "JSESSIONID=#{JSESSIONID}; Path=/wsg; Secure"
      request.body = params.to_json
    elsif method == "DELETE"
      puts "delete method"
    end
    response = https.request(request)
    Raisl.logger.info response.read_body
    JSON.parse(response.read_body) rescue {}
  end
end