import sys

from lib.helpers import timing
from PyQt5.QtCore import QTimer
from PyQt5.QtGui import QKeySequence
from PyQt5.QtWidgets import QShortcut
from ui.test_test import Test_Test


class Test_Connector(Test_Test):
    def __init__(self, components=None, recipe=None, step=None, pieces=None, run_once=False, reset_on_start=True, enable_override=False):
        super().__init__(components=components, recipe=recipe, step=step, pieces=pieces, run_once=run_once, reset_on_start=reset_on_start, enable_override=enable_override)
        QShortcut(QKeySequence("Return"), self).activated.connect(self.get)
        QShortcut(QKeySequence("Enter"), self).activated.connect(self.get)
        self.connector_le.setFocus()
        self.focus_timer = QTimer()
        self.focus_timer.setSingleShot(True)
        self.focus_timer.timeout.connect(self.set_focus)

    def start(self, recipe=None, step=None, pieces=None):
        show = super().start(recipe=recipe, step=step, pieces=pieces)
        if show is False:
            return show
        self.visualize()
        expected = str(self.step.spec.get("connector", "-"))
        self.parent_assembly_widget().set_text(f"COLLEGARE IL CONNETTORE {expected} AL MULTIMETRO E LEGGERE IL SUO BARCODE")
        self.expected_l.setText(expected)
        self.connector_le.setPalette(self.status_palettes[None])
        self.connector_le.setText("")
        self.connector_le.setFocus()
        self.focus_timer.start(500)

        # TESTING
        if "--test-connector" in sys.argv or "--test" in sys.argv:
            self.connector_le.setText(expected)
            self.test_timer = QTimer()
            self.test_timer.setSingleShot(True)
            self.test_timer.timeout.connect(self.get)
            self.test_timer.start(500)
        # /TESTING
        return show

    # def stop(self):
    #     super().stop()

    # def reset(self):
    #     super().reset()

    def get(self, data=None, override=False):
        if self.done:  # avoid proccessing if completed
            return
        if data is None:
            data = self.connector_le.text()
        if not len(data):
            data = None
        if data is None:
            super().get(None, override=override)
            return
        connector_name=self.step.spec.get("connector", "").lower()
        connector_names=[connector_name,connector_name.replace("scr","scr0")]
        connector_ok = data.lower() in connector_names
        if connector_ok:
            self.connector_le.setPalette(self.status_palettes[True])
        else:
            self.connector_le.setPalette(self.status_palettes[False])
        self.connector_le.setText("")
        self.connector_le.setFocus()
        if connector_ok is True:
            result = data
            ok = True
        else:
            result = None
            ok = None
        super().get([{
            "time": timing(),
            "results": {
                "ok": ok,
                "result": result,
                "data": data,
            },
        }], override=override, fail=ok is False)

    def visualize(self, data=None):
        if data is None:
            data = {}
        self.connector_l.setText(str(data.get("results", {}).get("data", "-")))
        super().visualize(data)

    def check_serial_barcode(self, barcode=None):
        return barcode is not None and len(barcode)

    def set_focus(self):
        self.connector_le.setFocus()
