from .widget_sources.Ui_admin_edit_prof import Ui_Dialog  #
from PyQt6 import QtWidgets
from src import logger
from src.logic import Prof
from src.backend import Database


class EditProf(QtWidgets.QDialog, Ui_Dialog):
    def __init__(self):
        super(EditProf, self).__init__()
        self.setupUi(self)
        # Variables
        self.db = Database()

        self.edit_faculty_member_select_member.currentTextChanged.connect(
            self.edit_faculty_member_set_data
        )
        self.update_faculty_member.clicked.connect(self.edit_faculty_member_action)
        self.gather_data()

    def gather_data(self):
        self.add_faculty_member_data()
        apparats = self.db.getApparatsByProf(
            self.db.getProfId(
                Prof(fullname=self.edit_faculty_member_select_member.currentText())
            )
        )
        if len(apparats) == 0:
            self.delete_faculty_member.setEnabled(True)
        for apparat in apparats:
            if apparat.deleted == 0:
                self.delete_faculty_member.setEnabled(False)
                self.delete_faculty_member.setToolTip(
                    "Professoren mit Apparaten können nicht gelöscht werden"
                )
                break

    def add_faculty_member_data(self):
        faculty_members = self.db.getFacultyMembers()
        names = [f"{member[5]}" for member in faculty_members]
        self.edit_faculty_member_select_member.clear()
        self.edit_faculty_member_select_member.addItems(names)
        self.edit_faculty_member_select_member.addItem("")
        self.edit_faculty_member_select_member.setCurrentText("")

    def edit_faculty_member_set_data(self):
        # get the selected member
        name = self.edit_faculty_member_select_member.currentText()
        fullname = name.replace(",", "")
        # print(fullname, name)
        # get the data for the selected member
        data = self.db.getProfByName(fullname)
        # set the data
        # print(data)
        if data is None:
            self.edit_faculty_member_title.setText("")
            self.faculty_member_old_telnr.setText("")
            self.faculty_member_oldmail.setText("")
            self.edit_faculty_member_title.setText("")
        else:
            self.faculty_member_old_telnr.setText(data.telnr)
            self.faculty_member_oldmail.setText(data.mail)
            logger.debug(data)
            (
                self.edit_faculty_member_title.setText(data.title)
                if data.title is not None
                else self.edit_faculty_member_title.setText("")
            )

    def edit_faculty_member_action(self):
        def __gen_fullname(fname, lname, data):
            if fname == "" and lname == "":
                return data[0]
            if fname == "" and lname != "":
                return f"{lname} {data[1]}"
            if fname != "" and lname == "":
                return f"{data[2]} {fname}"
            if fname != "" and lname != "":
                return f"{lname} {fname}"

        # get the data and use new value if it is not none and does not mach the old value
        if self.edit_faculty_member_select_member.currentText() == "":
            return
        olddata = self.db.getProfByName(
            self.edit_faculty_member_select_member.currentText()
        )
        logger.debug(olddata)
        data = olddata
        oldlname = data.lastname
        oldfname = data.firstname
        # take data except first and last entry

        titel = (
            self.edit_faculty_member_new_title.text()()
            if self.edit_faculty_member_new_title.text() != ""
            else None
        )
        fname = (
            self.edit_faculty_member_new_surname.text()
            if self.edit_faculty_member_new_surname.text() != ""
            else self.edit_faculty_member_select_member.currentText().strip()
        )
        lname = (
            self.user_faculty_member_new_name.text()
            if self.user_faculty_member_new_name.text() != ""
            else self.edit_faculty_member_select_member.currentText().strip()
        )
        fullname = __gen_fullname(fname, lname, data)
        telnr = self.user_faculty_member_new_telnr.text()
        mail = self.user_faculty_member_new_mail.text()
        new_data = {
            "titel": titel,
            "fname": fname,
            "lname": lname,
            "fullname": fullname,
            "mail": mail,
            "telnr": telnr,
        }
        new_data = {key: value for key, value in new_data.items() if value != ""}
        self.db.updateFacultyMember(data=new_data, oldlname=oldlname, oldfname=oldfname)
        self.add_faculty_member_data()
        self.edit_faculty_member_new_title.setText("")
        self.edit_faculty_member_new_surname.clear()
        self.user_faculty_member_new_name.clear()
        self.user_faculty_member_new_telnr.clear()
        self.user_faculty_member_new_mail.clear()
