import logging

from PyQt5 import uic
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QDialog
from ui.helpers import replace_widget

dialogs = {}


class Dialog(QDialog):
    _closing = pyqtSignal()

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        global dialogs
        dialogs[id(self)] = self
        self.setAttribute(Qt.WA_DeleteOnClose)
        u = "src/ui/{0}/{0}.ui".format("dialog")
        self.ui = uic.loadUi(u, self)
        # LOGO
        self.setWindowIcon(QIcon("src/ui/imgs/neo.ico"))
        self.log = logging.getLogger(f"{self.__class__.__name__} ({id(self)})")

    def setCentralWidget(self, widget):
        widget.setParent(self)
        replace_widget(self, "centralWidget", widget)

    def centralWidget(self):
        return self.centralwidget

    def closeEvent(self, *args):
        self._closing.emit()
        global dialogs
        dialogs.pop(id(self), None)
