import traceback
from tkinter import messagebox

from shared.utils import Logger
from shared.contracts import ValidatorWindowSignature
from validator.src.services.authentification_service import Authentification

# =============== Validator Window Controller ===============
class ValidatorWindowController:
    def __init__(self, validator_window: ValidatorWindowSignature):
        # Logger
        self.logger = Logger.create_logger(logger_name = "Validator Window Controller", log_file_name = "validator_app")

        # Validator window
        self.validatorWindow: ValidatorWindowSignature = validator_window
        
        # Helper Classes Instances
        self.authInstance = Authentification()

        # Autologin
        self.autologin()
    
    # ========== Autologin ==========
    def autologin(self):
        """Will autologin the user."""
        try:
            autologinStatus = self.authInstance.autologin()
            
            if autologinStatus == True:
                self.validatorWindow.create_validator_view()
            else:
                self.validatorWindow.create_login_view()
            
        except Exception as e:
            traceBack = traceback.format_exc()
            self.logger.error(
                f"[{self.__class__.__name__} - {self.autologin.__name__}] "
                f"Failed to autologin the user. \n{traceBack}")
    # ===============================
    
    # ========== Messageboxes ==========
    def handle_info(self, message: str):
        """Pop-up for info."""
        try:
            messagebox.showinfo(title = "Info", message = message)
        except Exception as e:
            traceBack = traceback.format_exc()
            self.logger.error(
                f"[{self.__class__.__name__} - {self.handle_error.__name__}] "
                f"Failed to show info message. \n{traceBack}")
            
    def handle_warning(self, message: str):
        """Pop-up for warning."""
        try:
            messagebox.showwarning(title = "Warning", message = message)
        except Exception as e:
            traceBack = traceback.format_exc()
            self.logger.error(
                f"[{self.__class__.__name__} - {self.handle_error.__name__}] "
                f"Failed to show warning. \n{traceBack}")
    
    def handle_error(self, message: str):
        """Pop-up for error."""
        try:
            messagebox.showerror(title = "Error", message = message)
        except Exception as e:
            traceBack = traceback.format_exc()
            self.logger.error(
                f"[{self.__class__.__name__} - {self.handle_error.__name__}] "
                f"Failed to show error. \n{traceBack}")
    
    def handle_critical_error(self, message: str):
        """Pop-up for critical error."""
        try:
            messagebox.showerror(title = "Error", message = message)
        except Exception as e:
            traceBack = traceback.format_exc()
            self.logger.error(
                f"[{self.__class__.__name__} - {self.handle_error.__name__}] "
                f"Failed to handle encountered critical error. \n{traceBack}")
    # ==================================
# ===========================================================