import sys

from modules.errors import SPFUnknownVariable, SPFUninitializedVariable, SPFAlreadyDefined, SPFIncompatibleType, SPFIndexError

trace_format = '\033[1m -> '
reset_format = '\033[0m'

class Variables:

    class Variable:
        types = { "entier": int,
                  "texte": str,
                  "booléen": bool,
                  "liste": list }

        def __init__(self, typ, value=None):
            assert typ in self.types.keys(), "Ce type de variable est inconnu"
            self.type = typ
            if not self.checkType(value, typ):
                raise SPFIncompatibleType(value, self.type)
            self.value = value

        def set(self, value):
            if not self.checkType(value, self.type):
                raise SPFIncompatibleType(value, self.type)
            self.value = value

        def __str__(self):
            if self.type == "booléen":
                return "Vrai" if self.value else "Faux"
            return f"{self.value}"

        def __repr__(self):
            if self.type == "texte":
                return f"\"{self.value}\""
            return f"{self.value}"

        def checkType(self, value, typ) -> bool:
            return value is None or type(value) == self.types[typ]

        def default(self, typ):
            if typ == "entier":
                return 0
            if typ == "texte":
                return ""
            if typ == "booléen":
                return False
            if typ == "liste":
                return []

    def __init__(self, trace=False):
        self.variables = {}
        self.trace = trace

    def get(self, name):
        if name not in self.variables:
            raise SPFUnknownVariable(name)
        if self.variables[name].value == None:
            raise SPFUninitializedVariable(name)
        if self.trace:
            print(f"{trace_format}accède {name}{reset_format}", file=sys.stderr)
        return self.variables[name].value

    def declare(self, typ, name, value=None):
        if name in self.variables:
            raise SPFAlreadyDefined(name)
        self.variables[name] = self.Variable(typ, value)
        if self.trace:
            print(f"{trace_format}déclare {name} = {value}{reset_format}", file=sys.stderr)

    def assign(self, name, value):
        if name not in self.variables:
            raise SPFUnknownVariable(name)
        self.variables[name].set(value)
        if self.trace:
            print(f"{trace_format}modifie {name} = {value}{reset_format}", file=sys.stderr)

    def dump(self):
        name_len = max(map(len, self.variables.keys()))
        name_len = name_len if name_len >= len("name") else len("name")
        var_len = max(map(len,map(str, self.variables.values())))
        var_len = var_len if var_len >= len("value") else len("value")
        print(f"┌{'─' * name_len}┬{'─' * var_len}┐", file=sys.stderr)
        print(f"│{'Name':>{name_len}}│{'Value':<{var_len}}│", file=sys.stderr)
        print(f"├{'─' * name_len}┼{'─' * var_len}┤", file=sys.stderr)
        for name, var in self.variables.items():
            print(f"│{name:>{name_len}}│{str(var):<{var_len}}│", file=sys.stderr)
        print(f"└{'─' * name_len}┴{'─' * var_len}┘", file=sys.stderr)
