from typing import Literal, Union, List, Tuple
import dearpygui.dearpygui as dpg
from . import layout
import traceback

#+----------------------------------------------------------------+
#|                           MORE DATA                            |
#+----------------------------------------------------------------+

#=========== MoreData ===========
#Almacena los datos necesarios para la ejecucion del layout
class MoreData():
    def __init__(self):
        self._execute_after = []
        self._window_loader_tags = ()

    #============ EXECUTE AFTER ============
    def add_execute_after(self, callback):
        self._execute_after.append(callback)

    def get_execute_after(self):
        return self._execute_after
    
    #=========== WINDOW LOADER ============
    def add_window_loader_tags(self, tag, tag_primary_window):
        self._window_loader_tags = (tag, tag_primary_window)

    def get_window_loader_tags(self):
        return self._window_loader_tags

    #============== CLEAR LAYOUT ==============
    def clear_data_execute_after(self):
        self._execute_after.clear()

more_data = MoreData()

#+----------------------------------------------------------------+
#|                             MORE                               |
#+----------------------------------------------------------------+

def execute_after():
    callbacks = more_data.get_execute_after()
    for call in callbacks:
        call()
    more_data.clear_data_execute_after()

def execute_loader():
    tags = more_data.get_window_loader_tags()
    if tags:
        def _main():
            size_primary_window = dpg.get_item_rect_size(tags[1])
            with dpg.window(modal=True, 
                            no_resize=True, 
                            no_collapse=True, 
                            no_title_bar=True, 
                            width=size_primary_window[0], 
                            height=size_primary_window[1],
                            tag=tags[0]
                            ):
                pass
            layout.ExecuteLayout()
        layout.wait_rendering([tags[1]], _main)
    else:
        layout.ExecuteLayout()