"""
Create a QT GUI and simulate button clicks.
Also start ArduinoEmulator & hevserver background processes to show
how we could test NativeUI.

Script is purposefully named without a leading or trailing 'test' in
the script name so that it doesn't get executed by pytest by default.

Make sure your PYTHONPATH var is set to the full path of:
'/<your_hev_root_dir>/hev/NativeUI'.
"""
import os
import random
import sys
import tempfile
import time


import hevclient
from tests.integration.integration_utils import start_background_arduinosim_hevserver,\
    kill_process_group, assert_posix, assert_pythonpath

import PySide2
# from PySide2 import QtCore, QtWidgets, QtGui
# from PySide2.QtWidgets import QApplication, QMainWindow, QWidget, QDialog
from PySide2 import QtCore, QtWidgets
from PySide2.QtWidgets import QWidget

# Overwrite the mm file for OS agnostic testing
hevclient.mmFileName = tempfile.gettempdir() + os.path.sep + "HEVCLIENT_last_Data.mmap"


def test_qtbot(qtbot, tmpdir):
    """
    Sample integration test showing how to test a QT GUI application
    with simulated mouse clicks. Also start background arduino and
    hev to show how we could do this when the GUI is NativeUI.
    """
    assert_posix()
    assert_pythonpath()
    background_procs = None
    try:
        background_procs = start_background_arduinosim_hevserver()
        time.sleep(2)
        print(PySide2.__version__)
        # Prints the Qt version used to compile PySide2
        print(PySide2.QtCore.__version__)

        if not QtWidgets.QApplication.instance():
            app = QtWidgets.QApplication(sys.argv)
        else:
            app = QtWidgets.QApplication.instance()

        widget = MyTestWidget()
        qtbot.addWidget(widget)

        # assert starting label before button click
        assert widget.text.text() == "Hello World"
        assert widget.click_count == 0
        # click button and make sure it updates the appropriate label
        qtbot.mouseClick(widget.button, QtCore.Qt.LeftButton)
        assert widget.text.text() == "Hallo Welt"
        assert widget.click_count == 1

        qtbot.mouseClick(widget.button, QtCore.Qt.LeftButton)
        assert widget.click_count == 2

    finally:
        if background_procs is not None:
            for proc in background_procs:
                kill_process_group(proc.pid)


class MyTestWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.click_count = 0
        self.hello = ["Hallo Welt"]
        self.button = QtWidgets.QPushButton("Please click me, oh please!")
        self.text = QtWidgets.QLabel("Hello World")
        self.text.setAlignment(QtCore.Qt.AlignCenter)
        self.layout = QtWidgets.QVBoxLayout()
        self.layout.addWidget(self.text)
        self.layout.addWidget(self.button)
        self.setLayout(self.layout)
        self.button.clicked.connect(self.magic)

    def magic(self):
        self.text.setText(random.choice(self.hello))
        self.click_count += 1
