from imgui_bundle import imgui, imgui_ctx, ImVec2
from model import *
import random

class DatabaseEditor:
    def __init__(self):
        super().__init__()
        self.add_name = str()
        self.select_class = 0
        self.select_lecture = 0
        self.select_student = 0
        self.select_submission = 0

        self.class_name = str()
        self.add_class_name = str()

        self.student_prename = str()
        self.student_surname = str()
        self.student_sex = False
        self.add_student_prename = str()
        self.add_student_surname = str()
        self.add_student_sex = False

        self.lecture_title = str()
        self.lecture_points = 0
        self.add_lecture_title = str()
        self.add_lecture_points = 0

        self.submission_points = 0.0
        self.add_submission_lecture = 0
        self.add_submission_points = 0.0

    def content_list(self, content: list, selector: int, id: str, height: int) -> int:
        w = imgui.get_window_size().x
        with imgui_ctx.begin_child(str(id), ImVec2(w*0.3, height)):
            for n, c in enumerate(content, start = 1):
                _, clicked = imgui.selectable(f"{n}. {c}", selector == n-1)
                if clicked:
                    selector = n-1
        return selector

    def class_editor(self):
        w, h = imgui.get_window_size()
        classes = Class.select()
        content = [f"{c.name}" for c in classes]
        self.select_class = self.content_list(content, self.select_class, "class_content", h*0.15)
        imgui.same_line()

        with imgui_ctx.begin_child("Class", ImVec2(w*0.25, h*0.15)):
            imgui.text("Edit Class")
            _, self.class_name = imgui.input_text_with_hint("##class_edit1", content[self.select_class], self.class_name)
            
            if imgui.button("Update"):
                id = classes[self.select_class].id
                Class.update(name=self.class_name).where(Class.id == id).execute()

            imgui.same_line()

            if imgui.button("Delete"):
                id = classes[self.select_class].id
                students = Student.select().where(Student.class_id == id)
                for student in students:
                    Submission.delete().where(Submission.student_id == student.id).execute()
                Student.delete().where(Student.class_id == id).execute()
                Lecture.delete().where(Lecture.class_id == id).execute()
                Class.delete().where(Class.id == id).execute()


            imgui.separator()

            imgui.text("Add Class")
            _, self.add_class_name = imgui.input_text_with_hint("##class_edit2", "Class Name", self.add_class_name)

            if imgui.button("Add"):
                Class.create(name=self.add_class_name)

        return classes[self.select_class].id

    def student_editor(self, class_id: int): 
        w, h = imgui.get_window_size()
        students = Student.select().where(Student.class_id == class_id)
        content = [f"{s.prename} {s.surname}" for s in students]
        self.select_student = self.content_list(content, self.select_student, "student_content", h*0.45)
        imgui.same_line()

        with imgui_ctx.begin_child("Student", ImVec2(w*0.25, h*0.4)):    
            imgui.text("Edit Student")

            prename = students[self.select_student].prename
            _, self.student_prename = imgui.input_text_with_hint("##student_edit1", prename, self.student_prename)

            surname = students[self.select_student].surname
            _, self.student_surname = imgui.input_text_with_hint("##student_edit2", surname, self.student_surname)

            if imgui.radio_button("Male##1", not self.student_sex):
                self.student_sex = not self.student_sex
            imgui.same_line()
            if imgui.radio_button("Female##1", self.student_sex):
                self.student_sex = not self.student_sex

            if imgui.button("Update"):
                Student.update(
                    prename = self.student_prename,
                    surname = self.student_surname,
                    sex = "Female" if self.student_sex else "Male"
                ).where(Student.id == students[self.select_student].id).execute()
            
            imgui.same_line()

            if imgui.button("Delete"):
                id = students[self.select_student].id
                Student.delete().where(Student.id == id).execute()
                Submission.delete().where(Submission.student_id == id).execute()
                self.select_student = 0
           
            imgui.separator()

            imgui.text("Add Student")
            _, self.add_student_prename = imgui.input_text_with_hint("##student_edit3", "First Name", self.add_student_prename)
            _, self.add_student_surname = imgui.input_text_with_hint("##student_edit4", "Last Name", self.add_student_surname)
            
            if imgui.radio_button("Male##2", not self.add_student_sex):
                self.add_student_sex = not self.add_student_sex
            imgui.same_line()
            if imgui.radio_button("Female##2", self.add_student_sex):
                self.add_student_sex = not self.add_student_sex

            if imgui.button("Add"):
                Student.create(
                    prename=self.add_student_prename,
                    surname=self.add_student_surname,
                    sex="Female" if self.add_student_sex else "Male",
                    class_id=class_id
                )
                self.add_student_prename = str()
                self.add_student_surname = str()
                self.add_student_sex = False

        return students[self.select_student].id

    def lecture_editor(self, class_id: int):
        w, h = imgui.get_window_size()
        lectures = Lecture.select().where(Lecture.class_id == class_id)
        content = [f"{l.title}" for l in lectures]
        self.select_lecture = self.content_list(content, self.select_lecture, "lecture_content", h*0.15)
        imgui.same_line()

        with imgui_ctx.begin_child("Lecture", ImVec2(w*0.25, h*0.15)):
            imgui.text("Edit Lecture")
            _, self.lecture_title = imgui.input_text_with_hint("##lecture_edit1", content[self.select_lecture], self.lecture_title)
            _, self.lecture_points = imgui.input_int("##lecture_points1", self.lecture_points)
            if self.lecture_points < 0:
                self.lecture_points = 0
        
            if imgui.button("Update"):
                Lecture.update(title=self.lecture_title, points=self.lecture_points).where(Lecture.id == lectures[self.select_lecture].id).execute()

            imgui.same_line()

            if imgui.button("Delete"):
                id = lectures[self.select_lecture].id 
                Submission.delete().where(Submission.lecture_id == id).execute()
                Lecture.delete().where(Lecture.id == id).execute()

            imgui.separator()

            imgui.text("Add Lecture")
            _, self.add_lecture_title = imgui.input_text_with_hint("##lecture_edit2", "Lecture Title", self.add_lecture_title)
            _, self.add_lecture_points = imgui.input_int("##lecture_points2", self.add_lecture_points)
            if self.add_lecture_points < 0:
                self.add_lecture_points = 0
        
            if imgui.button("Add"):
                Lecture.create(title=self.add_lecture_title, points=self.add_lecture_points, class_id=class_id)

        return lectures[self.select_lecture].id

    def submission_editor(self, student_id: int):
        w, h = imgui.get_window_size()
        submissions = Submission.select().where(Submission.student_id == student_id)
        lectures = [Lecture.get_by_id(sub.lecture_id) for sub in submissions]
        content = [l.title for l in lectures]
        self.select_submission = self.content_list(content, self.select_submission, "submission_content", h*0.2)
        imgui.same_line()

        with imgui_ctx.begin_child("Submission", ImVec2(w*0.25, h*0.2)):
            imgui.text("Edit Submission")
            imgui.text(content[self.select_submission])
            
            points = submissions[self.select_submission].points
            if points.is_integer():
                points = int(points)

            max_points = lectures[self.select_submission].points

            _, self.submission_points = imgui.input_float(f"{points}/{max_points}", self.submission_points, 0.5, 10, "%.1f")
            if self.submission_points < 0:
                self.submission_points = 0
            
            if imgui.button("Update"):
                Submission.update(points=self.submission_points).where(Submission.id == submissions[self.select_submission].id).execute()

            imgui.same_line()

            if imgui.button("Delete"):
                Submission.delete().where(Submission.id == submissions[self.select_submission].id).execute()

            imgui.separator()

            imgui.text("Add Submission")
            available_lectures = Lecture.select().where(Lecture.class_id == Student.get_by_id(student_id).class_id)
            combo_items = [l.title for l in available_lectures]
            _, self.add_submission_lecture = imgui.combo("##lecture_combo", self.add_submission_lecture, combo_items, len(combo_items))
            _, self.add_submission_points = imgui.input_float("##lecture_title", self.add_submission_points, 0.5, 10, "%.1f")
            if self.add_submission_points < 0:
                self.add_submission_points = 0
            
            if imgui.button("Add"):
                Submission.create(
                    points=self.add_submission_points,
                    lecture_id=available_lectures[self.add_submission_lecture].id,
                    student_id=student_id
                )

        return submissions[self.select_submission].id

    def __call__(self):
        with imgui_ctx.begin("Database Editor"):
            class_id = self.class_editor()
            imgui.separator()
            self.lecture_editor(class_id)
            imgui.separator()
            student_id = self.student_editor(class_id)
            imgui.separator()
            self.submission_editor(student_id)
        return
        classes = Class.select()

        with imgui_ctx.begin("Database Editor"):
            imgui.text("Add Class")

            _, self.add_name = imgui.input_text(" ", self.add_name)

            if imgui.button("Add"):
                if self.add_name:
                    Class.create(name=self.add_name)
                    self.add_name = str()

            imgui.separator()

            if not classes:
                imgui.text("No Dataset could be queried")
                return 
    
            for n, c in enumerate(classes, start=1):
                display = f"{n}. {c.name}"
                opened, _ = imgui.selectable(display, self.select == n-1)
                if opened:
                    self.select = n-1

        return classes[self.select]    
