import time

# from icecream import ic
from PyQt6.QtCore import QThread
from PyQt6.QtCore import pyqtSignal as Signal

from src.backend import Database


# from src.transformers import RDS_AVAIL_DATA


class AutoAdder(QThread):
    updateSignal = Signal(int)

    setTextSignal = Signal(int)
    progress = Signal(int)

    def __init__(self, data=None, app_id=None, prof_id=None, parent=None):
        super().__init__(parent)
        self.data = data
        self.app_id = app_id
        self.prof_id = prof_id

        # print("Launched AutoAdder")
        # print(self.data, self.app_id, self.prof_id)

    def run(self):
        self.db = Database()
        # show the dialog, start the thread to gather data and dynamically update progressbar and listwidget
        logger.info("Starting worker thread")
        item = 0
        for entry in self.data:
            try:
                self.updateSignal.emit(item)
                self.setTextSignal.emit(entry)
                item += 1
                self.progress.emit(item)
                time.sleep(1)

            except Exception as e:
                # print(e)
                logger.exception(
                    f"The query failed with message {e} for signature {entry}"
                )
                continue
        if item == len(self.data):
            logger.info("Worker thread finished")
            # teminate thread
            self.finished.emit()
