import re
from dataclasses import dataclass, field

from enum import Enum


@dataclass
class Prof:
    id: int = None
    _title: str = None
    firstname: str = None
    lastname: str = None
    fullname: str = None
    mail: str = None
    telnr: str = None

    # add function that sets the data based on a dict
    def from_dict(self, data: dict):
        for key, value in data.items():
            if hasattr(self, key):
                setattr(self, key, value)
        return self

    @property
    def title(self):
        if self._title is None or self._title == "None":
            return ""
        return self._title

    @title.setter
    def title(self, value):
        self._title = value

    # add function that sets the data from a tuple
    def from_tuple(self, data: tuple):
        setattr(self, "id", data[0])
        setattr(self, "_title", data[1])
        setattr(self, "firstname", data[2])
        setattr(self, "lastname", data[3])
        setattr(self, "fullname", data[4])
        setattr(self, "mail", data[5])
        setattr(self, "telnr", data[6])
        return self

    def name(self, comma=False):
        if self.firstname is None and self.lastname is None:
            if "," in self.fullname:
                self.firstname = self.fullname.split(",")[1].strip()
                self.lastname = self.fullname.split(",")[0].strip()
            else:
                return self.fullname

        if comma:
            return f"{self.lastname}, {self.firstname}"
        return f"{self.lastname} {self.firstname}"


@dataclass
class BookData:
    ppn: str | None = None
    title: str | None = None
    signature: str | None = None
    edition: str | None = None
    link: str | None = None
    isbn: str | list | None = field(default_factory=list)
    author: str | None = None
    language: str | list | None = field(default_factory=list)
    publisher: str | None = None
    place: str | None = None
    year: str | None = None
    pages: str | None = None
    library_location: int | None = None
    in_apparat: bool | None = False
    adis_idn: str | None = None

    def from_dict(self, data: dict):
        for key, value in data.items():
            setattr(self, key, value)

    def to_dict(self):
        return self.__dict__

    def from_dataclass(self, dataclass):
        for key, value in dataclass.__dict__.items():
            setattr(self, key, value)

    def from_string(self, data: str):
        if not data.startswith("BookData"):
            raise ValueError("No valid BookData string")
        else:
            pattern = r"(\w+)='([^']*)'"
            data_dict = dict(re.findall(pattern, data))
            # print(data_dict)
        for key, value in data_dict.items():
            setattr(self, key, value)
        return self


@dataclass
class MailData:
    subject: str | None = None
    body: str | None = None
    mailto: str | None = None
    prof: str | None = None


class Subjects(Enum):
    BIOLOGY = (1, "Biologie")
    CHEMISTRY = (2, "Chemie")
    GERMAN = (3, "Deutsch")
    ENGLISH = (4, "Englisch")
    PEDAGOGY = (5, "Erziehungswissenschaft")
    FRENCH = (6, "Französisch")
    GEOGRAPHY = (7, "Geographie")
    HISTORY = (8, "Geschichte")
    HEALTH_EDUCATION = (9, "Gesundheitspädagogik")
    HTW = (10, "Haushalt / Textil")
    ART = (11, "Kunst")
    MATH_IT = (12, "Mathematik / Informatik")
    MEDIAPEDAGOGY = (13, "Medien in der Bildung")
    MUSIC = (14, "Musik")
    PHILOSOPHY = (15, "Philosophie")
    PHYSICS = (16, "Physik")
    POLITICS = (17, "Politikwissenschaft")
    PRORECTORATE = (18, "Prorektorat Lehre und Studium")
    PSYCHOLOGY = (19, "Psychologie")
    SOCIOLOGY = (20, "Soziologie")
    SPORT = (21, "Sport")
    TECHNIC = (22, "Technik")
    THEOLOGY = (23, "Theologie")
    ECONOMICS = (24, "Wirtschaftslehre")

    @property
    def id(self):
        return self.value[0]

    @property
    def name(self):
        return self.value[1]

    @classmethod
    def get_index(cls, name):
        for i in cls:
            if i.name == name:
                return i.id - 1


@dataclass
class Apparat:
    id: int | None = None
    name: str | None = None
    prof_id: int | None = None
    subject: str | None = None
    appnr: int | None = None
    created_semester: str | None = None
    extended_at: str | None = None
    eternal: bool = False
    extend_until: str | None = None
    deleted: int | None = None
    deleted_date: str | None = None
    apparat_id_adis: str | None = None
    prof_id_adis: str | None = None
    konto: int | None = None

    def from_tuple(self, data: tuple):
        self.id = data[0]
        self.name = data[1]
        self.prof_id = data[2]
        self.subject = data[3]
        self.appnr = data[4]
        self.created_semester = data[5]
        self.extended_at = data[6]
        self.eternal = data[7]
        self.extend_until = data[8]
        self.deleted = data[9]
        self.deleted_date = data[10]
        self.apparat_id_adis = data[11]
        self.prof_id_adis = data[12]
        self.konto = data[13]
        return self

    @property
    def get_semester(self):
        if self.extend_until is not None:
            return self.extend_until
        else:
            return self.created_semester


@dataclass
class ELSA:
    id: int | None = None
    date: str | None = None
    semester: str | None = None
    prof_id: int | None = None

    def from_tuple(self, data):
        self.id = data[0]
        self.date = data[1]
        self.semester = data[2]
        self.prof_id = data[3]
        return self


@dataclass
class ApparatData:
    prof: Prof = field(default_factory=Prof)
    apparat: Apparat = field(default_factory=Apparat)
