from tkinter import Tk
import os
from datos.ConfigBase import Config

class ConfigManager(Config):
    _instance = None
    
    script_path = os.path.abspath(__file__)
    default_path = os.path.dirname(script_path)
    RUTA_NGP = ""
    SCREEN_WIDTH = 0
    SCREEN_HEIGHT = 0
    DATABASE_NAME = "colmap.db"
    DEFAULT_WIDTH = 480
    DEFAULT_HEIGHT = 250
    DEFAULT_IMAGE = os.path.join(default_path, "images", "default.jpg")
    
    
    def __new__(cls):
        if cls._instance is None:
            cls._instance = super(ConfigManager, cls).__new__(cls)
            # Inicializaciones que se realizan una vez
            root = Tk()
            cls._instance.SCREEN_WIDTH = root.winfo_screenwidth()
            cls._instance.SCREEN_HEIGHT = root.winfo_screenheight()
            root.destroy()
        return cls._instance

    def set_fps(cls, num_fps):
        cls._instance.FPS = num_fps

    def set_ruta_ejecutable(cls, ruta):
        if os.path.isfile(ruta):
            cls._instance.RUTA_NGP = ruta
        else:
            cls._instance.RUTA_NGP = os.path.join(ruta, "instant-ngp.exe")

    def resetear_configuracion(self):
        super().resetear_configuracion()

    def cargar_desde_json(self, config_json):
        super().cargar_desde_json(config_json)

    def get_rutaNGP(cls):
        return cls._instance.RUTA_NGP

    def set_output_file_type(cls, file_type):
        cls._instance.FILE_TYPE = file_type

    def get_output_file_type(cls):
        return cls._instance.FILE_TYPE

    def set_images_folder(cls, new_folder):
        cls._instance.IMAGES_FOLDER = new_folder

    def get_images_folder(cls):
        return cls._instance.IMAGES_FOLDER
    
    def set_output_folder(cls, new_folder):
        cls._instance.DIR_OUTPUT = new_folder

        temp = os.path.join(cls._instance.DIR_OUTPUT, "keypoints")
        temp_matches = os.path.join(cls._instance.DIR_OUTPUT, "matches")
        cls._instance.DIR_KEYPOINTS =  os.path.normpath(temp)
        cls._instance.DIR_MATCHES = os.path.normpath(temp_matches)

        permisos = "F"  # Permiso para control total
        # Construir el comando icacls
        comando = f"icacls {cls._instance.DIR_OUTPUT} /grant:r *S-1-1-0:{permisos} /t /c"
        # Ejecutar el comando en el símbolo del sistema
        os.system(comando)

    def set_camera_model(cls, model):
        cls._instance.MODEL_CAMERA = model

    def set_registro_folder(cls, folder):
        cls._instance.REGISTRO_FOLDER = folder

    def set_config_folder(cls, folder):
        cls._instance.CONFIG_FOLDER = folder

    def set_base_folder(cls, new_folder):
        cls._instance.DIR_ACT = new_folder
        
    def set_video_folder(cls, new_folder):
        cls._instance.DIR_VIDEO = new_folder

    def set_image_global_size(cls, width, height):

        cls._instance.IMAGE_GLOBAL_WIDTH = width
        cls._instance.IMAGE_GLOBAL_HEIGHT = height

        ratio = width / height

        if width < 1920 or height < 1080: 

            new_width =  width / 2
            new_height = int(new_width / ratio)

            cls._instance.DEFAULT_WIDTH = new_width
            cls._instance.DEFAULT_HEIGHT = new_height
            return
        else:
            new_width =  width / 4
            new_height = int(new_width / ratio)

            cls._instance.DEFAULT_WIDTH = new_width
            cls._instance.DEFAULT_HEIGHT = new_height
            return


    def is_images_folder_set(cls):
        return cls._instance.IMAGES_FOLDER != ""
    
    def is_output_folder_set(cls):
        return cls._instance.DIR_OUTPUT != ""
    
    def is_base_folder_set(cls):
        return cls._instance.DIR_ACT != ""
    
    def is_matches_folder_set(cls):
        return cls._instance.DIR_MATCHES != ""
    
    def is_video_folder_set(cls):
        return cls._instance.DIR_VIDEO != ""
    
    def is_db_file_set(cls):
        return cls._instance.db_file != ""
    
    def set_default_size(cls, width, height):
        cls._instance.DEFAULT_WIDTH = width
        cls._instance.DEFAULT_HEIGHT = height
        
    def get_global_size(cls):
        return cls._instance.IMAGE_GLOBAL_WIDTH, cls._instance.IMAGE_GLOBAL_HEIGHT
    
    def get_output_folder(cls):
        return cls._instance.DIR_OUTPUT
    
    def set_dir_database(cls, new_folder):
        cls._instance.DIR_DATABASE = new_folder
        cls._instance.db_file = os.path.join(cls._instance.DIR_DATABASE, cls._instance.DATABASE_NAME)

    def is_keypoints_folder_set(cls):
        return cls._instance.DIR_KEYPOINTS != ""
    
    def set_selected_mesh(cls, mesh):
        cls._instance.SELECTED_MESH = mesh

    def get_selected_mesh(cls):
        return cls._instance.SELECTED_MESH