#pylint: disable=invalid-name, missing-function-docstring, line-too-long, logging-fstring-interpolation, missing-class-docstring, missing-module-docstring, wildcard-import, unused-wildcard-import
from typing import List
from .utils import *

class ConstellationDeserializationError(Exception):
    pass

class Constellation:
    def __init__(self, from_json=None):
        if from_json:
            try:
                self.constellation_id = from_json["id"]
                self.__hub_interfaces = []
                self.__leaf_interfaces = []
                self.__traffic_mode = None
                # Intentional simplification for Teraflow. Constellation could have
                # diverse traffic modes, however that does not occur in intended TF usage.
                if "hubModule" in from_json:
                    hub = from_json["hubModule"]
                    self.traffic_mode = hub["state"]["module"]["trafficMode"]
                    self.__hub_interfaces.extend(get_constellation_module_ifnames(hub))
                if "leafModules" in from_json:
                    for leaf in from_json["leafModules"]:
                        if not self.__traffic_mode:
                            self.traffic_mode = leaf["state"]["module"]["trafficMode"]
                        self.__leaf_interfaces.extend(get_constellation_module_ifnames(leaf))
            except KeyError as e:
                raise ConstellationDeserializationError(f"Missing mandatory key {str(e)}") from e
        else:
            # May support other initializations in future
            raise ConstellationDeserializationError("JSON dict missing")

        def add_vlan_posfix(ifname, is_hub):
            if is_hub:
                # +100 so we don't need to worry about special meanings of VLANs 0 and 1
                return [f"{ifname}.{vlan+100}" for vlan in range(0,16)]
            else:
                return [f"{ifname}.{chr(ord('a') + vlan)}" for vlan in range(0,16)]

        self.__vti_hub_interfaces = []
        self.__vti_leaf_interfaces = []
        if self.is_vti_mode():
            for ifname in self.__hub_interfaces:
                self.__vti_hub_interfaces.extend(add_vlan_posfix(ifname, True))
            for ifname in self.__leaf_interfaces:
                self.__vti_leaf_interfaces.extend(add_vlan_posfix(ifname, False))

    def ifnames(self) -> List[str]:
        if self.is_vti_mode():
            return self.__vti_hub_interfaces + self.__vti_leaf_interfaces
        else:
            return self.__hub_interfaces + self.__leaf_interfaces

    def is_vti_mode(self) -> bool:
        return self.traffic_mode != "L1Mode"
