# pylint: disable=[missing-module-docstring] # only single class in module

from PyQt6.QtWidgets import QDialog,QLabel,QGridLayout
# Users who add custom controls can use the build-in features at their own risk.
# If you want your module to be more independent,implement your own replacement for the following imports.
from esibd.plugins import Device
from esibd.core import Parameter, parameterDict, PluginManager, Channel

def providePlugins():
    return [CustomDevice]

class CustomDevice(Device):
    """The minimal code in *examples/CustomDevice.py* is an example of how to integrate a custom device.
    See :ref:`sec:plugin_system` for more information."""
    documentation = """The minimal code in examples/CustomDevice.py is an example of how to integrate a custom device."""

    name = 'CustomDevice'
    version = '1.0'
    pluginType = PluginManager.TYPE.INPUTDEVICE

    def __init__(self,**kwargs):
        super().__init__(**kwargs)
        self.customSetting = 'Custom/Setting'
        self.channelType=CustomChannel
        self.icon = self.makeIcon('cookie.png')

    def initGUI(self):
        """Initialize your custom user interface"""
        super().initGUI()
        # a base UI is provided by parent class,it can be extended like this if required
        self.addAction(self.customAction,'Custom tooltip.',self.makeIcon('cookie.png'))

    def customAction(self):
        """Execute your custom code"""
        dlg = QDialog(self)
        dlg.setWindowTitle('Custom Dialog')
        lbl = QLabel(f'This could run your custom code.\nThe value of your custom setting is {self.custom}')
        lay = QGridLayout()
        lay.addWidget(lbl)
        dlg.setLayout(lay)
        dlg.exec()

    def apply(self, apply=False):
        """ Executed when values have changed.
            Should only apply channels where value has changed."""
        # for c in self.channels:
        #     pass # implement depending on hardware

    def getDefaultSettings(self):
        """ Define device specific settings that will be added to the general settings tab.
        These will be included if the settings file is deleted and automatically regenerated.
        Overwrite as needed."""
        settings = super().getDefaultSettings()
        settings['Custom/Setting'] = parameterDict(value=100, _min=100, _max=10000, toolTip='Custom Tooltip',
                                                                                    widgetType=Parameter.TYPE.INT, attr='custom')
        return settings

class CustomChannel(Channel):
    """custom channel"""

    # def __init__(self,**kwargs):
    #     super().__init__(**kwargs)
    ID = 'ID'

    def getDefaultChannel(self):
        channel = super().getDefaultChannel()
        channel[self.VALUE   ][Parameter.HEADER] = 'Value (X)' # overwrite to change header
        channel[self.ID] = parameterDict(value=0, widgetType=Parameter.TYPE.INT, advanced=True, header='ID', attr='id')
        return channel

    def setDisplayedParameters(self):
        super().setDisplayedParameters()
        self.insertDisplayedParameter(self.ID,before=self.COLOR)

    def finalizeInit(self,item):
        super().finalizeInit(item)
        # add custom parameters as needed,make sure they are present in your ini file and generated by makeDefault if needed.
        _id = self.getParameterByName(self.ID)
        _id.getWidget().setMinimum(-1000)
        _id.getWidget().setMaximum(+1000)
