from PyQt6 import QtWidgets

from src.logic.webrequest import BibTextTransformer, WebRequest

from .dialog_sources.Ui_fileparser import Ui_Dialog


class FileParserDialog(QtWidgets.QDialog, Ui_Dialog):
    def __init__(self, parent: None) -> None:
        super().__init__(parent)
        self.setupUi(self)
        self.signatures = []
        self.returned = []

    def moveToThread(self, thread):
        self.progressBar.moveToThread(thread)
        self.frame.moveToThread(thread)
        self.horizontalLayoutWidget.moveToThread(thread)
        self.horizontalLayout.moveToThread(thread)
        self.label.moveToThread(thread)
        self.count.moveToThread(thread)
        self.label_2.moveToThread(thread)
        self.frame_2.moveToThread(thread)
        self.listWidget.moveToThread(thread)

    def run(self):
        for signature in self.signatures:
            self.count.setText(str(self.signatures.index(signature) + 1))
            self.listWidget.addItem(signature)
            webdata = WebRequest().get_ppn(signature).get_data()
            bookdata = BibTextTransformer("ARRAY").get_data(webdata).return_data()
            self.returned.append(bookdata)
            self.progressBar.setValue(self.signatures.index(signature) + 1)
        # self.data_gathering_complete.emit()

    def deleteLater(self):
        self.progressBar.deleteLater()
        self.frame.deleteLater()
        self.horizontalLayoutWidget.deleteLater()
        self.horizontalLayout.deleteLater()
        self.label.deleteLater()
        self.count.deleteLater()
        self.label_2.deleteLater()
        self.frame_2.deleteLater()
        self.listWidget.deleteLater()
        self.signatures = []
        self.returned = []
        self.retranslateUi.deleteLater()
        super().deleteLater()
