import logging

from lib.helpers import get_resource
from PyQt5 import uic
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget
from ui.dialog import Dialog


class Widget(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setAttribute(Qt.WA_DeleteOnClose)
        me = self.__class__.__name__
        u = get_resource("ui/{0}/{0}.ui".format(me.lower()))
        self.ui = uic.loadUi(u, self)
        self.setWindowTitle(me)
        self.log = logging.getLogger(f"{self.__class__.__name__} ({id(self)})")

    def setParent(self, parent):
        parent._closing.connect(self._parent_closing)
        super().setParent(parent)

    def open_dialog(self, widget, show=True, maximized=False):
        d = Dialog()
        d.setCentralWidget(widget)
        if show:
            if maximized:
                d.showMaximized()
            else:
                d.show()
        return d

    def _parent_closing(self):
        pass
