import logging

from lib.helpers import get_resource
from PyQt5 import uic
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QMainWindow
from ui.dialog import Dialog


class Window(QMainWindow):
    _closing = pyqtSignal()

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setAttribute(Qt.WA_DeleteOnClose)
        self.setWindowFlags(Qt.Window)
        me = self.__class__.__name__
        u = get_resource("ui/{0}/{0}.ui".format(me.lower()))
        self.ui = uic.loadUi(u, self)
        # LOGO
        self.setWindowIcon(QIcon(get_resource("ui/imgs/neo.ico")))
        self.log = logging.getLogger(f"{self.__class__.__name__} ({id(self)})")

    def setCentralWidget(self, widget):
        widget.setParent(self)
        super().setCentralWidget(widget)

    def open_dialog(self, widget, show=True):
        d = Dialog()
        widget.setParent(self)
        d.setCentralWidget(widget)
        if show:
            d.show()
        return d

    def closeEvent(self, *args):
        self._closing.emit()
