#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from typing import Self, Any
from Interfaces.ControllersAbstractInterface import ControllersAbstractInterface
from Interfaces.OpoTestsInterface import OpoTestsInterface
from Models.RequestModel import RequestModel

class ControllersManager:

    def __init__(self:Self, ot:OpoTestsInterface) -> None:
        self.ot:OpoTestsInterface = ot
        self.__controllers:dict[str, ControllersAbstractInterface] = {}

    def start(self:Self) -> None:
        pass

    def add(self:Self, inputs:str|ControllersAbstractInterface|list|tuple, overwrite:bool = False) -> None:
        
        item:dict[str, ControllersAbstractInterface]

        for item in self.ot.load_json_data(inputs, False) or (inputs,):

            key:str
            controller:ControllersAbstractInterface

            for key, controller in item.items():
                if overwrite or key not in self.__controllers:
                    self.__controllers[key] = controller

    def get(self:Self, 
        key:str, 
        action:str, 
        request:RequestModel
    ) -> tuple[Any|None, dict[str, Any|None]]|None:
        return self.__controllers[key].get(action, request) if key in self.__controllers else None