# Form implementation generated from reading ui file 'c:\Users\aky547\GitHub\Semesterapparate\ui\dialogs\parsed_titles.ui'
#
# Created by: PyQt6 UI code generator 6.3.1
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt6 import QtCore, QtGui, QtWidgets

from src.logic.log import MyLogger
from src.logic.threads import AutoAdder

logger = MyLogger("AutoTitleAdder")


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(402, 316)
        self.frame = QtWidgets.QFrame(Form)
        self.frame.setGeometry(QtCore.QRect(10, 10, 381, 41))
        self.frame.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Shadow.Raised)
        self.frame.setObjectName("frame")
        self.horizontalLayoutWidget = QtWidgets.QWidget(self.frame)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(0, 0, 381, 41))
        self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.label = QtWidgets.QLabel(self.horizontalLayoutWidget)
        self.label.setObjectName("label")
        self.horizontalLayout.addWidget(self.label)
        self.count = QtWidgets.QLabel(self.horizontalLayoutWidget)
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.count.setFont(font)
        self.count.setTextFormat(QtCore.Qt.TextFormat.PlainText)
        self.count.setObjectName("count")
        self.horizontalLayout.addWidget(self.count)
        self.label_2 = QtWidgets.QLabel(self.horizontalLayoutWidget)
        self.label_2.setObjectName("label_2")
        self.horizontalLayout.addWidget(self.label_2)
        spacerItem = QtWidgets.QSpacerItem(
            40,
            20,
            QtWidgets.QSizePolicy.Policy.Expanding,
            QtWidgets.QSizePolicy.Policy.Minimum,
        )
        self.horizontalLayout.addItem(spacerItem)
        self.frame_2 = QtWidgets.QFrame(Form)
        self.frame_2.setGeometry(QtCore.QRect(10, 80, 381, 201))
        self.frame_2.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel)
        self.frame_2.setFrameShadow(QtWidgets.QFrame.Shadow.Raised)
        self.frame_2.setObjectName("frame_2")
        self.horizontalLayoutWidget_2 = QtWidgets.QWidget(self.frame_2)
        self.horizontalLayoutWidget_2.setGeometry(QtCore.QRect(0, 10, 381, 191))
        self.horizontalLayoutWidget_2.setObjectName("horizontalLayoutWidget_2")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget_2)
        self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.listWidget = QtWidgets.QListWidget(self.horizontalLayoutWidget_2)
        self.listWidget.setObjectName("listWidget")
        self.horizontalLayout_2.addWidget(self.listWidget)
        self.listWidget_done = QtWidgets.QListWidget(self.horizontalLayoutWidget_2)
        self.listWidget_done.setObjectName("listWidget_done")
        self.horizontalLayout_2.addWidget(self.listWidget_done)
        self.progressBar = QtWidgets.QProgressBar(Form)
        self.progressBar.setGeometry(QtCore.QRect(10, 60, 381, 23))
        self.progressBar.setProperty("value", 24)
        self.progressBar.setObjectName("progressBar")
        self.buttonBox = QtWidgets.QDialogButtonBox(Form)
        self.buttonBox.setGeometry(QtCore.QRect(230, 290, 156, 23))
        self.buttonBox.setStandardButtons(
            QtWidgets.QDialogButtonBox.StandardButton.Cancel
            | QtWidgets.QDialogButtonBox.StandardButton.Ok
        )
        self.buttonBox.setObjectName("buttonBox")
        self.toolButton = QtWidgets.QToolButton(Form)
        self.toolButton.setGeometry(QtCore.QRect(20, 290, 25, 19))
        self.toolButton.setObjectName("toolButton")
        self.signatures = []
        self.prof_id = None
        self.app_id = None
        self.thread = QtCore.QThread()
        self.toolButton.hide()
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)
        self.toolButton.clicked.connect(self.start)
        # if cancel is clicked, terminate the thread
        self.buttonBox.rejected.connect(self.thread_quit)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.label.setText(_translate("Form", "Es wurden"))
        self.count.setText(_translate("Form", "0"))
        self.label_2.setText(_translate("Form", "Signaturen gefunden."))
        self.toolButton.setText(_translate("Form", "..."))

    def populate_table(self):
        for i in range(len(self.signatures)):
            self.listWidget.addItem(QtWidgets.QListWidgetItem())
            self.listWidget.item(i).setText(self.signatures[i])
        self.listWidget.item(i).setToolTip("Daten werden gesammelt")

    def update_progress_bar(self, value: int):
        self.progressBar.setValue(value)

    def thread_quit(self):
        print("Terminating thread")
        self.thread.terminate()
        self.thread.quit()
        self.thread.deleteLater()
        self.thread = None

    def start(self):
        logger.log_info("Starting AutoAdder")

        self.thread = AutoAdder(
            data=self.signatures,
            app_id=self.app_id,
            prof_id=self.prof_id,
        )
        self.thread.finished.connect(self.on_completion)
        self.thread.updateSignal.connect(self.update_progress_bar)
        self.thread.setTextSignal.connect(self.update_lists)
        self.thread.progress.connect(self.determine_progress)
        self.thread.finished.connect(self.thread.quit)
        self.thread.finished.connect(self.thread.deleteLater)
        # self.thread.updateSignal.connect(self.update_progress_label)
        # worker.finished.connect(worker.deleteLater)

        self.thread.start()

    def on_completion(self):
        logger.log_info("AutoAdder finished")
        logger.log_info("Returning data")

    # create a function that closes the dialog

    def determine_progress(self, signal):
        # check length of listWidget
        length = self.listWidget.count()
        print(f"Length of listWidget: {length}")
        if length == 0:
            logger.log_info("AutoAdder finished")
            self.buttonBox.accepted.emit()

    def update_lists(self, signal):
        # get text of first entry in listWidget
        text = self.listWidget.item(0).text()
        # remove first entry
        self.listWidget.takeItem(0)
        # add first entry to listWidget_done
        self.listWidget_done.addItem(text)
