import sip
from PyQt5.QtCore import pyqtSignal
from ui.window import Window


class Main_Window(Window):
    do = pyqtSignal(dict)

    def __init__(self):
        super().__init__()
        self.do.connect(self._do)
        # print("MAIN_WINDOW  ", str(int(QThread.currentThreadId())), flush=True)

    @staticmethod
    def _do(config):
        return config["f"](*config.get("a", []), **config.get("k", {}))

    def open_tab(self, widget):
        self.setCentralWidget(widget)

    def open_window(self, widget, show=True, maximized=False):
        wt = widget.__class__.__name__
        if wt not in self.windows or self.windows[wt] is None or sip.isdeleted(self.windows[wt]):
            w = Window()
            self.windows[wt] = w
            w.setCentralWidget(widget)
            if show:
                if maximized:
                    w.showMaximized()
                else:
                    w.show()
            return w
        else:
            self.windows[wt].activateWindow()
            return self.windows[wt]
