import sys
import weakref

from PyQt5.QtCore import Qt

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


class Test_Warning_Img(Test_Test):
    def __init__(self, components=None, recipe=None, step=None, run_once=False, reset_on_start=False, enable_override=False,bench_name="generic"):
        super().__init__(components=components, recipe=recipe, step=step, run_once=run_once, reset_on_start=reset_on_start, enable_override=enable_override)
        self.bench_name = bench_name

    def start(self, recipe=None, step=None, pieces=None):
        show = super().start(recipe=recipe, step=step)

        if step is not None:
            img_path=f"config/warning_images/{self.bench_name}/{step.spec['warning_img']}.png"
            self.continue_b.clicked.connect(lambda checked, self=weakref.ref(self): self().get(0))
            self.img = QPixmap(str(img_path))
            self.img_l.setPixmap(self.img)

        return show

    def get(self, data=None, override=False):
        if self.done:  # avoid proccessing if completed
            return
            # if data is None:
            #     data = self.amount_sb.value()
        if data is None:
            super().get(None, override=override)
            return
        result = data
        ok = True
        super().get([{
            "time": timing(),
            "results": {
                "ok": ok,
                "result": result,
                "data": data,
            },
        }], override=override, fail=ok is False, skip_delay=True)

    def set_done(self):
        self.done=True
        self.done_ok=True


    def stop(self):
        #self.done=True
        #self.done_ok = True
        super().stop()

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