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

from Interfaces.OpoTestsInterface import OpoTestsInterface
from typing import Self
from Models.UserModel import UserModel

class UsersManager:

    def __init__(self:Self, op:OpoTestsInterface) -> None:
        self.op:OpoTestsInterface = op
        self.__users:list[UserModel] = []

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

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

    def empty(self:Self) -> None:
        self.__users = []

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

        users:dict[str, str]

        for users in self.op.load_json_data(inputs):

            nick:str
            password:str

            for nick, password in users.items():

                i:int
                user:UserModel
                allowed:bool = True

                for i, user in enumerate(self.__users):
                    if user.name == nick:
                        if overwrite:
                            self.__users[i] = UserModel(nick, password)
                        allowed = False
                        break

                if allowed:
                    self.__users += [UserModel(nick, password)]

    def validate(self:Self, nick:str, password:str) -> bool:

        user:UserModel

        for user in self.__users:
            if user.name == nick and user.password == password:
                return True
        return False