from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QLabel, QProgressBar


class BlockingDialog(QDialog):
    def __init__(self, parent=None,message="PER FAVORE ATTENDERE...",wait_time=10):
        super().__init__(parent)
        self.wait_time = wait_time
        self.setWindowTitle("ATTESA")
        self.setModal(True)  # This blocks interaction with the main window

        layout = QVBoxLayout(self)
        self.label = QLabel(message)
        layout.addWidget(self.label)

        self.progress = QProgressBar(self)
        self.progress.setRange(0, wait_time)  # 60 seconds
        layout.addWidget(self.progress)

        self.timer = QTimer(self)
        self.timer.timeout.connect(self.update_progress)
        self.time_left = wait_time

        self.timer.start(1000)  # 1000 milliseconds = 1 second

    def update_progress(self):
        self.time_left -= 1
        self.progress.setValue(self.wait_time - self.time_left)
        if self.time_left == 0:
            self.timer.stop()
            self.close()