import sys

from lib.db import Users
from PyQt5.QtCore import QTimer
from PyQt5.QtGui import QKeySequence
from PyQt5.QtWidgets import QMessageBox, QShortcut
from ui.widget import Widget


class Test_Admin_Permission(Widget):
    txt = "È necessario il permesso di un amministratore.\nInserire password per continuare"

    def __init__(self, info=""):
        super().__init__()
        QShortcut(QKeySequence("Return"), self).activated.connect(self.continue_b.click)
        QShortcut(QKeySequence("Enter"), self).activated.connect(self.continue_b.click)
        if len(info):
            self.info_l.setText(f"{info}\n{self.txt}")
        else:
            self.info_l.setText(self.txt)
        self.continue_b.clicked.connect(self.verify)
        self.cancel_b.clicked.connect(self.cancel)
        # TESTING
        if "--auto-accept-test-admin-permission" in sys.argv or "--test" in sys.argv:
            self.password_le.setText("123123")
            self.test_timer = QTimer()
            self.test_timer.setSingleShot(True)
            self.test_timer.timeout.connect(self.continue_b.clicked.emit)
            self.test_timer.start(500)
        # /TESTING

    def verify(self):
        if Users.get_user("ADMIN").verify(self.password_le.text()):
            self.parentWidget().accept()
        else:
            QMessageBox.critical(None, "Errore password", "la password inserita non e corretta")

    def cancel(self):
        self.parentWidget().reject()
