class OpenWifi::BaseApi
  extend WrapperApi

  API_PATH_PREFIX = "/api/v1/"

  def self.get_API_url_endpoint hostname, portnumber
    "https://" + hostname + ':' + portnumber + API_PATH_PREFIX
  end

  def mac_to_serialno mac_id
    mac_id.gsub(':', '').downcase
  end

  def get_integration_details ln, org=nil, router_inventory=nil
    int_type = nil
    int_type = router_inventory.present? ? MasterRouterInventory.where(mac_id: router_inventory.mac_id).last.try(:integration_type) : nil
    int_type = (ln.blank? ? org.integration_types.where(vendor_name: 'openwifi').last : ln.organisation.integration_types.where(vendor_name: 'openwifi').last) unless int_type.present?
    
    int_type
  end

  def get_controller_details int_type, endpoint
    Rails.logger.info "[OPENWIFI-Get controller endpoint] INT_TYPE :: #{int_type} :: ENDPOINT :: #{endpoint}"
    if int_type
      hostname = (int_type['details'][endpoint] || {})['host'] || APPLICATION['defaults']['OPEN_WIFI'][endpoint]['HOSTNAME']
      port_number = (int_type['details'][endpoint] || {})['port'] || APPLICATION['defaults']['OPEN_WIFI'][endpoint]['PORTNUMBER']
    else
      hostname = APPLICATION['defaults']['OPEN_WIFI'][endpoint]['HOSTNAME']
      port_number = APPLICATION['defaults']['OPEN_WIFI'][endpoint]['PORTNUMBER']
    end
    OpenWifi::BaseApi.get_API_url_endpoint hostname, port_number
  end
end