import traceback
from tkinter import messagebox

from shared.utils import Logger
from shared.contracts import UpdaterViewSignature, UpdaterWindowSignature, UpdateDoneCallback
from updater.src.services.update_service import UpdateService

# =============== Updater View Controller ===============
class UpdaterViewController:
    def __init__(self, updater_view_master: UpdaterWindowSignature, updater_view: UpdaterViewSignature, update_done_callback: UpdateDoneCallback, kit_url: str):
        # Logger
        self.logger = Logger.create_logger(logger_name = "Updater Window Controller", log_file_name = "update_app")
        # Callback Functions
        self.updateDoneCallback: UpdateDoneCallback = update_done_callback
        # Views
        self.master: UpdaterWindowSignature = updater_view_master
        self.updaterView: UpdaterViewSignature = updater_view
        
        # Helper Class Instance
        self.updateServiceInstance: UpdateService = UpdateService()
        # Kit URL
        self.kitUrl = kit_url
        
        # Start Update
        self.updaterView.updateProgressBar.start()
        self.updaterView.updateProgressBar.configure(
            progress_color = self.updaterView.template["ProgressbarActive"]["ProgressBarUpdateInProgressColor"])
        self.start_update()
        
    def start_update(self) -> None:
        try:
            self.updateServiceInstance.update(kit_url = self.kitUrl, after_update_callback = self.after_update, after_update_fail_callback = self.after_update_fail)
        except:
            traceBack = traceback.format_exc()
            self.logger.error(
                f"[{self.__class__.__name__} - {self.start_update.__name__}] "
                f"Failed to start the update process. \n{traceBack}")
    
    def after_update(self):
        try:
            self.updaterView.updateProgressBar.stop()
            self.updaterView.updateProgressBar.configure(
                fg_color = self.updaterView.template["ProgressbarActive"]["ProgressBarUpdateInProgressColor"])
            self.updaterView.updateLabelMain.configure(
                text = self.updaterView.template["LabelActive"]["DoneText"]
            )
            self.master.after(1000, self.updateDoneCallback)
        except:
            traceBack = traceback.format_exc()
            self.logger.error(
                f"[{self.__class__.__name__} - {self.after_update.__name__}] "
                f"Failed to start the validator after the update process. \n{traceBack}")
    
    def after_update_fail(self):
        try:
            self.updaterView.updateProgressBar.stop()
            self.updaterView.updateProgressBar.configure(
                fg_color = self.updaterView.template["ProgressbarActive"]["ProgressBarUpdateFailProgressColor"],
                progress_color = self.updaterView.template["ProgressbarActive"]["ProgressBarUpdateFailProgressColor"])
            self.updaterView.updateLabelMain.configure(
                text = self.updaterView.template["LabelActive"]["FailText"]
            )
            self.master.after(1000, self.updateDoneCallback)
        except:
            traceBack = traceback.format_exc()
            self.logger.error(
                f"[{self.__class__.__name__} - {self.after_update.__name__}] "
                f"Failed to start the validator after the update process. \n{traceBack}")
# =======================================================