# Structjour -- a daily trade review helper
# Copyright (C) 2019 Zero Substance Trading
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
'''
Instantiate the ui form and create all the needed connections to run it.

Created on May 20, 2019

@author: Mike Petersen
'''



import sys
import unittest
from  unittest import TestCase
from PyQt5.QtWidgets import QApplication, QLineEdit, QCheckBox, QSpinBox, QComboBox

from PyQt5.QtTest import QTest
from PyQt5.QtCore import Qt, QSettings

from structjour.view.chartcontrol import ChartControl
# pylint: disable = C0103

app = QApplication(sys.argv)

class TestChartCtrl(TestCase):

    def __init__(self, *args, **kwargs):
        super(TestChartCtrl, self).__init__(*args, **kwargs)

    def setUp(self):
        self.chartSet = QSettings('zero_substance/chart/test', 'structjour')
        self.testSet = {'chart': ['classic', 'No style'],
                'gridv': [False, True],
                'gridh': [True, False],
                'markercolorup': ['color1', 'colorK'],
                'markercolordown': ['color2', 'colorJ'],
                'markeredgecolor': ['color3', 'colorI'],
                'markeralpha': [0.5, 0.75],
                'markersize': [78, 87],
                'colorup': ['color4', 'colorH'],
                'colordown': ['color5', 'colorG'],
                'interactive': [False, True],
                'showlegend': [True, False],

                'chart1ma1': [False, True],
                'chart1ma2': [True, False],
                'chart1ma3': [False, True],
                'chart1ma4': [True, False],
                'chart1vwap': [False, True],
                'chart1ma1spin': [14, 16],
                'chart1ma2spin': [15, 155],
                'chart1ma3spin': [16, 14],
                'chart1ma4spin':[17, 13],
                'chart1ma1color': ['color7', 'colorF'],
                'chart1ma2color': ['color8', 'colorE'],
                'chart1ma3color': ['color9', 'colorD'],
                'chart1ma4color':['colorA', 'colorC'],
                'chart1vwapcolor': ['colorB', 'colorBB'],
                
                'chart2ma1': [True, False],
                'chart2ma2': [False, True],
                'chart2ma3': [True, False],
                'chart2ma4': [False, True],
                'chart2vwap': [True, False],
                'chart2ma1spin': [18, 12],
                'chart2ma2spin': [19, 11],
                'chart2ma3spin': [20, 10],
                'chart2ma4spin':[21, 9],
                'chart2ma1color': ['colorC', 'colorA'],
                'chart2ma2color': ['colorD', 'color9'],
                'chart2ma3color': ['colorE', 'color8'],
                'chart2ma4color':['colorF', 'color7'],
                'chart2vwapcolor': ['colorG', 'color6'],

                'chart3ma1': [False, True],
                'chart3ma2': [True, False],
                'chart3ma3': [False, True],
                'chart3ma4': [True, False],
                'chart3vwap': [False, True],
                'chart3ma1spin': [22, 8],
                'chart3ma2spin': [23, 7],
                'chart3ma3spin': [24, 6],
                'chart3ma4spin': [25, 5],
                'chart3ma1color': ['colorH', 'color5'],
                'chart3ma2color': ['colorI', 'color4'],
                'chart3ma3color': ['colorJ', 'color3'],
                'chart3ma4color': ['colorK', 'color2'],
                'chart3vwapcolor': ['colorL', 'color1']}

        for key in self.testSet:
            #Set the settings and chartControl initializes from those vals
            self.chartSet.setValue(key, self.testSet[key][0])
        self.w = ChartControl(self.chartSet)

    def test_initFromSettings(self):
        # Test that chart control is hooked up correctly to initialize from settings
        widgs = self.w.widgDict
        for key in widgs:
            widg = widgs[key]
            if isinstance(widg, QLineEdit):
                val = self.chartSet.value(key)
                self.assertEqual(widg.text(), val)
            elif isinstance(widg, QCheckBox):
                val = self.chartSet.value(key, False, bool)
                self.assertEqual(widg.isChecked(), val)
            elif isinstance(widg, QSpinBox):
                val = self.chartSet.value(key, 0, int)
                self.assertEqual(widg.value(), val)
            elif isinstance(widg, QComboBox):
                val = self.chartSet.value(key)
                self.assertEqual(widg.currentText(), val)         
            else:
                print(type(widgs[key]))

    def test_changeByUser(self):
        '''Simulate user input, check that the widget, and settings have the value in the testSet'''
        for key in self.w.widgDict:
            widg = self.w.widgDict[key]
            testVal = self.testSet[key][1] 
            if isinstance(widg, QLineEdit):
                QTest.keyClick(widg, Qt.Key_A, Qt.ControlModifier)
                QTest.keyClicks(widg, str(testVal))
                self.assertEqual(str(testVal), widg.text())
                self.assertEqual(widg.text(), self.chartSet.value(key))
            elif isinstance(widg, QCheckBox):
                QTest.mouseClick(widg, Qt.LeftButton)

                #QTest bug mouseClick fails to emit for particular widgets
                # TODO try to isolate this to a two widget program and submit bug report
                # It seems to have somethign to do with setting the label to Null
                if key == 'showlegend':
                    self.w.ui.showLegendCb.setChecked(testVal)
                    self.chartSet.setValue(key, testVal)
                self.assertEqual(widg.isChecked(), testVal, key)
                self.assertEqual(widg.isChecked(), self.chartSet.value(key, False, bool), key)
            elif isinstance(widg, QSpinBox):
                QTest.keyClick(widg, Qt.Key_A, Qt.ControlModifier)
                QTest.keyClicks(widg, str(testVal))
                self.assertEqual(int(testVal), widg.value())
                self.assertEqual(widg.value(), self.chartSet.value(key, 4, int))
            elif isinstance(widg, QComboBox):
                # Skipping this. Interaction w Combox has many variables. 
                pass
            else:
                print(type(widg))





        


def main():
    unittest.main()

if __name__ == '__main__':
    main()