# -*- coding: utf-8 -*-
"""
Simple example showing the creation of a dynamic node (show/hide termianls)
based on other node inputs.
"""

# append the path to allow usage of nodeworks outside of python's sitepackages
import sys
sys.path.append('../')

# import OrderDict to preserve terminal order
from collections import OrderedDict

# import qt, qtpy is a PySide/PyQt4/PyQt5 interface that uses Qt5 syntax
# > pip install qtpy
from qtpy import QtWidgets

# Import nodeworks
from nodeworks import NodeWidget, Node


# Custom Dynamic Node
class DynamicNode(Node):
    name = 'Dynamic Node'

    def __init__(self):
        # define terminal options
        self.terminalOpts = OrderedDict([
            ('type', {'widget': 'combobox',
                      'in': True,
                      'out': False,
                      'showlabel': True,
                      'dtype': str,
                      'multiinput': False,
                      'items': ['all', 'spinbox', 'doublespinbox', 'checkbox',
                                'lineedit'],
                      'value': 'all',
                      }),
            ('spinbox', {'widget': 'spinbox',
                         'in': True,
                         'out': False,
                         'showlabel': True,
                         'dtype': int,
                         }),
            ('doublespinbox', {'widget': 'doublespinbox',
                               'in': True,
                               'out': False,
                               'showlabel': True,
                               'dtype': float,
                               }),
            ('checkbox', {'widget': 'checkbox',
                          'in': True,
                          'out': False,
                          'showlabel': True,
                          'dtype': bool,
                          }),
            ('lineedit', {'widget': 'lineedit',
                          'in': True,
                          'out': False,
                          'showlabel': True,
                          'dtype': str,
                          }),
            ])

        # initialize the Node
        Node.__init__(self)

        # Connect the "type" terminal value change event to "updateNode"
        self.terminals['type'].valueChanged.connect(self.updateNode)

    def updateNode(self):

        # Get the current value
        showterm = self.terminals['type'].value

        # Show/hide terminals
        for term in ['spinbox', 'doublespinbox', 'checkbox', 'lineedit']:
            if term == showterm or showterm == 'all':
                self.terminals[term].show()
            else:
                self.terminals[term].hide()

        # update size
        self.nodeGraphic.resizeToMinimum()

    def process(self):
        pass


def main():
    # Create a Qt App
    app = QtWidgets.QApplication(sys.argv)

    # initialize the node widget
    nodechart = NodeWidget(showtoolbar=True)

    # Build default node library
    nodechart.nodeLibrary.buildDefaultLibrary()

    # Add the dynamic node
    nodechart.nodeLibrary.addNode(DynamicNode, [])

    # create the main window
    mainwindow = QtWidgets.QMainWindow()
    mainwindow.setCentralWidget(nodechart)
    mainwindow.setGeometry(200, 200, 800, 500)
    mainwindow.setWindowTitle('Nodeworks')
    mainwindow.show()

    # Start Qt Event Loop
    app.exec_()
    app.deleteLater()
    sys.exit()

if __name__ == '__main__':
    main()