
# for QtDragon:
# put/rename the file in YOUR CONFIG/qtvcp/screens/qtdragon/qtdragon_handler.py
# or for qtdragon_hd:
# put/rename the file in YOUR CONFIG/qtvcp/screens/qtdragon_hd/qtdragon_hd_handler.py

import sys
import os
import importlib

from PyQt5 import QtWidgets, QtCore, QtGui

from qtvcp.core import Info, Path, Qhal
PATH = Path()
QHAL = Qhal()
INFO = Info()

# get reference to original handler file so we can subclass it
sys.path.insert(0, PATH.SCREENDIR)
module = "{}.{}_handler".format(PATH.BASEPATH,PATH.BASEPATH)
mod = importlib.import_module(module, PATH.SCREENDIR)
sys.path.remove(PATH.SCREENDIR)
HandlerClass = mod.HandlerClass

PATH.TOUCHOFF_SUBPROGRAM = os.path.abspath(os.path.join(
    PATH.SCREENDIR, 'touchoff_subprogram.py'))

CRITICAL = 2

# return our subclassed handler object
def get_handlers(halcomp, widgets, paths):
    return [UserHandlerClass(halcomp, widgets, paths)]

class UserHandlerClass(HandlerClass):
    def __init__(self, halcomp, widgets, paths):
        super().__init__(halcomp, widgets, paths)

    def init_widgets(self):
        super().init_widgets()
        print('Custom subclassed handler "dialog window for custom panel" loaded\n')
        self.add_status('Custom subclassed handler "dialog window for custom panel" loaded\n',CRITICAL)

        self.Btn = None
        for name, loc, cmd in INFO.ZIPPED_TABS:
            if loc =='DIALOG':

                # add panel to a dialog window
                self.w['popup'] = QtWidgets.QDialog(self.w)
                temp = self.w[name.replace(' ','_')]
                # set to apropriate size for panel
                self.w['popup'].setMinimumSize(600,400)
                layout = QtWidgets.QGridLayout(self.w['popup'])
                layout.addWidget(temp, 0, 0)

                # this will remove the dualog window controls
                #self.w['popup'].setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)

                # add launch button to screen
                self.Btn = QtWidgets.QPushButton(self.w)
                self.Btn.setEnabled(True)
                self.Btn.setMinimumSize(64, 40)
                self.Btn.setIconSize(QtCore.QSize(38, 38))
                self.Btn.setIcon(QtGui.QIcon(':/qt-project.org/styles/commonstyle/images/up-32.png'))
                self.Btn.clicked.connect(self.togglePopup)
                self.w.layout_buttonbar.addWidget(self.Btn)

                continue

    def init_pins(self):
        # initialize the standard pins
        super().init_pins()

        # generate the new pins
        self.popup_pin = QHAL.newpin("popup-window", QHAL.HAL_BIT, QHAL.HAL_IN)
        self.popup_pin.pinValueChanged.connect(lambda p,v: self.showPopup(v))

    def togglePopup(self):
        if self.w['popup'].isVisible():
            self.w['popup'].hide()
        else:
            self.w['popup'].show()

    def showPopup(self,state):
        if self.Btn is None:
            return
        if state:
            self.w['popup'].show()
        else:
            self.w['popup'].hide()
