# coding: utf-8
"""
    Cohesive Networks SDK

    Cohesive Networks SDK is a thin wrapper around our product APIs providing complete control of your network's addresses, routes, rules and edge  # noqa: E501

    The version of the OpenAPI document: 4.8
    Contact: solutions@cohesive.net
    Generated by: https://openapi-generator.tech
"""

from __future__ import absolute_import

from cohesivenet.version import LATEST_VNS3_MS_VERSION
from cohesivenet.api_client import APIClient, api_as_property

from cohesivenet.api import vns3ms as vns3ms_apis


class MSClient(APIClient):
    """Generic API client for OpenAPI client library builds.

    OpenAPI generic API client. This client handles the client-
    server communication, and is invariant across implementations. Specifics of
    the methods and models for each application are generated from the OpenAPI
    templates.

    NOTE: This class is auto generated by OpenAPI Generator.
    Ref: https://openapi-generator.tech
    Do not edit the class manually.

    :param configuration: .Configuration object for this client
    :param header_name: a header to pass when making calls to the API.
    :param header_value: a header value to pass when making calls to
        the API.
    :param cookie: a cookie to include in the header when making calls
        to the API
    :param pool_threads: The number of threads to use for async requests
        to the API. More threads means more concurrent API requests.
    """

    BASE_PATH = "api"
    DEF_REQ_TIMEOUT = 15.0
    TOKEN_LIFETIME = 600  # seconds

    # Client API Groups available as attributes: e.g. vns3_client.peering.delete_peer(4)
    access = api_as_property("access", vns3ms_apis.AccessApi)
    admin = api_as_property("admin", vns3ms_apis.AdministrationApi)
    backups = api_as_property("backups", vns3ms_apis.BackupsApi)
    cloud_monitoring = api_as_property(
        "cloud_monitoring", vns3ms_apis.CloudMonitoringApi
    )
    system = api_as_property("system", vns3ms_apis.SystemApi)
    user = api_as_property("user", vns3ms_apis.UserApi)
    vns3_management = api_as_property("vns3_management", vns3ms_apis.VNS3ManagementApi)

    @staticmethod
    def latest_version():
        return LATEST_VNS3_MS_VERSION

    @property
    def state(self):
        return getattr(self, "_state", {})

    def add_to_state(self, key, value):
        state = getattr(self, "_state", {})
        state[key] = value
        setattr(self, "_state", state)
        return None

    def update_state(self, state_updates_dict):
        state = getattr(self, "_state", {})
        state.update(state_updates_dict)
        setattr(self, "_state", state)
        return None

    def query_state(self, key):
        return self.state.get(key)

    @property
    def ms_version(self):
        return self.state.get("ms_version")

    @ms_version.setter
    def ms_version(self, version):
        prev_v = self.ms_dot_version
        if prev_v != version:
            self.reset_api_version_sdk()

        self.add_to_state("ms_version", version)
        return self

    @property
    def ms_dot_version(self):
        ms_version = self.ms_version
        return ms_version.split("-")[0] if ms_version else None

    @property
    def dot_version(self):
        return self.ms_dot_version

    @property
    def host_uri(self):
        return self.configuration.host_uri

    def refresh_token(self):
        """
        """
        response = self.access.post_create_token(
            self.configuration.username, self.configuration.api_key
        ).json()

        self.configuration.api_token = response["api_token"]
