
from enum import Enum
from acemist.rest.rest import RestClient
from acemist.print_utils.print_self import print_self

class Region(str, Enum):
    """Mist cloud regions. Value is the CLI slug, attributes hold the URLs."""

    def __new__(cls, value: str, api_url: str):
        obj = str.__new__(cls, value)
        obj._value_ = value
        obj.api_url = api_url
        return obj

    GLOBAL_01 = ("global-01", "api.mist.com")
    GLOBAL_02 = ("global-02", "api.gc1.mist.com")
    GLOBAL_03 = ("global-03", "api.ac2.mist.com")
    GLOBAL_04 = ("global-04", "api.gc2.mist.com")
    GLOBAL_05 = ("global-05", "api.gc4.mist.com")
    EMEA_01   = ("emea-01", "api.eu.mist.com")
    EMEA_02   = ("emea-02", "api.gc3.mist.com")
    EMEA_03   = ("emea-03", "api.ac6.mist.com")
    EMEA_04   = ("emea-04", "api.gc6.mist.com")
    APAC_01   = ("apac-01", "api.ac5.mist.com")
    APAC_02   = ("apac-02", "api.gc5.mist.com")
    APAC_03   = ("apac-03", "api.gc7.mist.com")



class Mist:

    def __init__(self, baseurl: str | None = None, token: str | None = None, org_id: str | None=None, site_id: str | None=None):
        self.rest = RestClient(
            baseurl=baseurl or "https://api.mist.com/api/v1",
            token=token,
        )
        self.user_data = {}
        self.org_id = org_id
        self.site_id =site_id
        
    def _poplulate_self(self):
        self.user_data = self.rest.get("self")
        print(self.user_data)
    def _self(self):
        self._poplulate_self()
        print_self(self.user_data)

    def site_list(self):
        data = self.rest.get(f"orgs/{self.org_id}/sites")
        return data

    def site_get(self,):
        data = self.rest.get(f"sites/{self.site_id}")
        return data

    def network_templates_list(self):
        data = self.rest.get(f"orgs/{self.org_id}/networktemplates")
        return data

    def network_templates_get(self, template_id: str):
        data = self.rest.get(f"orgs/{self.org_id}/networktemplates/{template_id}")
        return data

    def network_templates_update(self, template_id: str, payload: dict):
        data = self.rest.put(f"orgs/{self.org_id}/networktemplates/{template_id}", payload)
        return data
    
    def site_network_templates_get(self):
        
        data = self.rest.get(f"sites/{self.site_id}/networktemplates/derived?")
        
        return data
    def site_list_devices(self):
        data = self.rest.get(f"sites/{self.site_id}/devices?type=switch&limit=100&page=1")
        return data
    
    def site_devices_get(self,device_id:str):
        data = self.rest.get(f"sites/{self.site_id}/devices/{device_id}")
        return data