# -*- coding: utf-8 -*-
"""
Simple example showing the creation of a node that uses the multiple buttons
terminal widget.
"""

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

# 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, tools


# Custom Table Node
class ButtonNode(Node):
    name = 'Button Node'

    def __init__(self):
        # define the terminal options
        self.terminalOpts = {
            'pushbtns': {'widget': 'pushbuttons',
                         'showlabel': False,
                         'buttons': ['add', 'remove', 'done'], # list of buttons
                         'checkable': True
                         },
            'toolbtns': {'widget': 'toolbuttons',
                         'showlabel': False,
                         'buttons': ['stop', 'play', 'save'],  # list of buttons
                         'checkable':True
                         }
            }

        # initialize the Node
        Node.__init__(self)

        # connect buttons through attributes
        self.pushbtns.widget.add.valueChanged.connect(self.add)

        # connect buttons through indexing
        self.terminals['pushbtns'].widget['remove'].valueChanged.connect(self.remove)

        # connect buttons through a mixture
        self.terminals['pushbtns'].widget.done.valueChanged.connect(self.done)

        # add some icons
        self.toolbtns.widget.stop.setIcon(tools.get_icon('stop'))
        self.toolbtns.widget.play.setIcon(tools.get_icon('play'))
        self.toolbtns.widget.save.setIcon(tools.get_icon('save'))

        # More connections!
        self.toolbtns.widget.stop.valueChanged.connect(self.stop)
        self.toolbtns.widget.play.valueChanged.connect(self.play)
        self.toolbtns.widget.save.valueChanged.connect(self.save)

        # Or a single connection for each button
        self.pushbtns.valueChanged.connect(self.allbuttons)
        self.toolbtns.valueChanged.connect(self.allbuttons)

    def add(self):
        print('add')

    def remove(self):
        print('remove')

    def done(self):
        print('done')

    def stop(self):
        print('stop')

    def play(self):
        print('play')

    def save(self):
        print('save')

    def allbuttons(self, btn):
        print('the buttons states are {}'.format(btn))


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(ButtonNode, [])

    # 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()
