from datos.Config import ConfigManager
from logica.utilidades.Output import Output
import logica.utilidades.utils as ut
from logica.controladores.TextureController import TextureController
import logica.utilidades.statics_feature as sf
import logica.feature_extractor_logic as logica

config = ConfigManager()
output = Output("feature_extraction")
tc = TextureController("imagenes_features")
tc_match = TextureController("imagenes_matches")

KEYPOINTS_IMAGES = []
NAME_FILES = []

ID_IMAGE_NAME = {}

MATCHES_IMAGES = {}
NAME_FILES_MATCHES = []

keep_colmap_coords = False
mask_categories = []

def init_output_view():
    output.main_view()

def set_output(text):
    output.set_output(text)

def init_process(command):
    return output.init_process(command)

def clear_output():
    output.clear_output()

def delete_view():
    output.delete_view()

def set_model_matcher(new_model):
    config.TIPO_RECONOCIMIENTO = new_model

def set_aabb_scale(new_scale):
    config.ESCALA_AABB = new_scale

def set_keep_colmap_coords(new_value):
    global keep_colmap_coords
    keep_colmap_coords = new_value

def set_mask_categories(new_value):
    global mask_categories
    mask_categories = new_value

def get_mask_categories():
    return mask_categories

def get_keep_colmap_coords():
    return keep_colmap_coords

def get_model_matcher():
    return config.TIPO_RECONOCIMIENTO

def get_aabb_scale():
    return config.ESCALA_AABB

def seleccionar_carpeta():
    return ut.seleccionar_folder_callback(output)
    
def get_keypoints_images():
    return KEYPOINTS_IMAGES

def get_match_images(idx):
    return MATCHES_IMAGES[idx]

def get_matches_images():
    return MATCHES_IMAGES

def delete_keypoints_images():
    global KEYPOINTS_IMAGES
    KEYPOINTS_IMAGES = []

def delete_name_files():
    global NAME_FILES
    NAME_FILES = []

def delete_matches_images():
    global MATCHES_IMAGES
    MATCHES_IMAGES = {}

def delete_matches_name_files():
    global NAME_FILES_MATCHES
    NAME_FILES_MATCHES = []

def get_match_file(idx):
    return NAME_FILES_MATCHES[idx]

def obtener_data_imagen_por_id(id_buscado):
    for clave, imagen in MATCHES_IMAGES.items():
        id_actual, name = clave  # Desempaquetamos la tupla para obtener el ID
        if id_actual == id_buscado:
            return (id_actual, name, imagen)  # Retornamos la imagen si encontramos el ID
    return None  # Retornamos None si no se encuentra el ID

def get_name_file(idx):
    return NAME_FILES[idx]

def get_name_files():
    return NAME_FILES

def extract_keypoints():
    return sf.extract_features(output)

def extract_matches():
    return sf.extract_matches(output)

def push_keypoint_image(image):
    global KEYPOINTS_IMAGES
    return KEYPOINTS_IMAGES.append(image)

def push_name_file(name):
    global NAME_FILES
    return NAME_FILES.append(name)

def push_matches_image(id,image):
    global MATCHES_IMAGES
    MATCHES_IMAGES[id] = image

def push_name_matches(name):
    global NAME_FILES_MATCHES
    return NAME_FILES_MATCHES.append(name)

def init_feature_extraction():
    if not config.is_output_folder_set():
        set_output("Output folder of images not selected.")
        return
    
    if not config.is_db_file_set():
        set_output("Feature folder not selected.")
        return

    logica.feature_extraction_callback()

def get_images_id():
    return ID_IMAGE_NAME

def cargar_imagenes_id():
    global ID_IMAGE_NAME
    resposta = sf.get_imagenes_id(output)
    if resposta == None:
        print("Error when loading images.")
        ID_IMAGE_NAME = {}
    
    ID_IMAGE_NAME = resposta

def save_images_with_keypoints():
    if not config.is_output_folder_set():
        set_output("Image folder not selected.")
        return
    logica.save_images_with_keypoints()

def save_images_with_matches():
    if not config.is_matches_folder_set():
        set_output("Output folder not selected.")
        return
    logica.save_images_with_matches()

def load_images_with_keypoints():
    result = tc.main_view(config.DIR_KEYPOINTS)
    if result:
        output.set_output(result)

def load_images_with_matches():
    result = tc_match.main_view(config.DIR_MATCHES)
    if result:
        output.set_output(result)