import dearpygui.dearpygui as dpg
from datos.Config import ConfigManager
import vistas.feature_extractor as fe
import vistas.meshlab as ml
import vistas.image_preprocessing as ip
import vistas.segmentation as seg
import vistas.graphics as gr
import os
from logica.utilidades.Output import Output
import sys

if len(sys.argv) < 3 or sys.argv[1] != "-r":
    print("Uso: python main.py -r <ruta_ejecutable>")
    sys.exit(1)

ruta_ejecutable = sys.argv[2]

script_path = os.path.abspath(__file__)
actual_path = os.path.dirname(script_path)

config = ConfigManager()

config.set_ruta_ejecutable(ruta_ejecutable)

# Inicializar todos los logs
output = Output("pipeline")

dpg.create_context()

with dpg.font_registry():
    font_path = os.path.join(actual_path, "datos", "font", "Roboto-Medium.ttf")
    default_font = dpg.add_font(font_path, 18)
    dpg.add_font(font_path, 14, tag="min_font")

views = ["preprocessing", "segmentation", "feature_extraction", "meshlab", "graphics"]
labels = ["1.- Segmentation", "2.- Image Preprocessing", "3.- Feature Extraction","4.- 3D Modeling", "Graphics & History"]

def callback_image_preprocessing(sender):
    hide_handler(sender)

    if dpg.does_item_exist("preprocessing"):
        ip.show_view()
    else:
        ip.main_view("pipeline")

    output.hide_all_except("preprocessing")

def callback_segmentation(sender):
    hide_handler(sender)
    
    if dpg.does_item_exist("segmentation"):
        seg.show_view()
    else:
        seg.main_view("pipeline")

    output.hide_all_except("segmentation")

def callback_feature_extraction(sender):
    hide_handler(sender)
    
    if dpg.does_item_exist("feature_extraction"):
        fe.show_view()
    else:
        fe.main_view("pipeline")

    output.hide_all_except("feature_extraction")

def callback_3d_modeling(sender):
    hide_handler(sender)
    
    if dpg.does_item_exist("meshlab"):
        ml.show_view()
    else:
        ml.main_view("pipeline")

    output.hide_all_except("meshlab")

def callback_graphics(sender):
    hide_handler(sender)
    
    if dpg.does_item_exist("graphics"):
        dpg.show_item("graphics")
    else:
        gr.main_view("pipeline")

    output.hide_all_except("graphics")

def hide_handler(sender):
    for index, label in enumerate(labels):
        if label != sender:
            if dpg.does_item_exist(views[index]):
                dpg.hide_item(views[index])

with dpg.window(label="Pipeline", width=config.SCREEN_WIDTH*0.59, height=config.SCREEN_HEIGHT, show=True, tag="pipeline"):
    tab_bar = dpg.add_tab_bar(label="Tabs")
    dpg.add_tab_button(label="1.- Segmentation", parent=tab_bar, callback=callback_segmentation)
    dpg.add_tab_button(label="2.- Image Preprocessing", parent=tab_bar, callback=callback_image_preprocessing)
    dpg.add_tab_button(label="3.- Feature Extraction", parent=tab_bar, callback=callback_feature_extraction)
    dpg.add_tab_button(label="4.- 3D Modeling", parent=tab_bar, callback=callback_3d_modeling)
    dpg.add_tab_button(label="Graphics & History", parent=tab_bar, callback=callback_graphics)

    dpg.bind_font(default_font)

dpg.create_viewport(title='TFG Reconstruccio 3D', width=config.SCREEN_WIDTH, height=config.SCREEN_HEIGHT, x_pos=0, y_pos=0)

seg.main_view("pipeline")

dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()