import logging
import json
from requests import request, Session

from schluter.thermostat import Thermostat

API_BASE_URL = "https://OWD5-SL003-App.ojelectronics.com"
API_SIGNIN_URL = API_BASE_URL + "/api/UserProfile/SignIn"
API_GET_GROUP_URL = API_BASE_URL + "/api/Group/GroupContents"
#API_SET_TEMPERATURE_URL = API_BASE_URL + "/api/thermostat"
APIKEY = ""
CUSTOMERID = 
CLIENTSWVERSION = 

_LOGGER = logging.getLogger(__name__)

class Api:
    def __init__(self, timeout=10, command_timeout=60, http_session: Session = None):
        self._timeout = timeout
        self._command_timeout = command_timeout
        self._http_session = http_session

    def get_session(self, username, password):
        response = self._call_api(
            "post", 
            API_SIGNIN_URL,
            params = None,
            json = { 
                'APIKEY': APIKEY,
                'UserName' : username, 
                'Password': password, 
                'CustomerId': CUSTOMERID,
                'ClientSWVersion' : CLIENTSWVERSION
            })

        return response
    
    def get_group(self, sessionId):
        params = {
        	'sessionid': sessionId,
        	'APIKEY' : APIKEY
        	}
        group = self._call_api("get", API_GET_GROUP_URL, params).json()
        groups = group["GroupContents"]
        thermostats = groups[0]["Thermostats"]

#         thermostat_list = []
#         for group in groups:
#             for thermostat in group["Thermostats"]:
#                 thermostat_list.append(Thermostat(thermostat))

        return thermostats
    
#     def set_temperature(self, sessionId, serialNumber, temperature):
#         modifiedTemp = int(temperature * 100)
#         params = { 'sessionId': sessionId, 'serialnumber': serialNumber }
#         json = { 'ManualTemperature': modifiedTemp, "RegulationMode": 3, "VacationEnabled": False}
#         result = self._call_api("post", API_SET_TEMPERATURE_URL, params = params, json = json).json()
#         
#         return result["Success"]

    def _call_api(self, method, url, params, **kwargs):
        payload = kwargs.get("params") or kwargs.get("json")

        if "timeout" not in kwargs:
            kwargs["timeout"] = self._timeout
        
        _LOGGER.debug("Calling %s with payload=%s", url, payload)

        response = self._http_session.request(method, url, params = params, **kwargs) if\
            self._http_session is not None else\
            request(method, url, params = params, **kwargs)

        _LOGGER.debug("API Response received: %s - %s", response.status_code, response.content)

        response.raise_for_status()
        return response
