from imgui_bundle import imgui
from datatypes import *

from model import *

class LectureEditor:
    def __init__(self):
        super().__init__()
        
        self.select = 0
        self.add_lecture_text = str()
        self.add_lecture_points = 0

    def __call__(self, clas: Class):
        id = clas.id if clas else None
        lectures = Lecture.select().where(Lecture.class_id == id) if id else None 

        with imgui.begin("Lecture Editor", False, imgui.WINDOW_NO_MOVE | imgui.WINDOW_NO_RESIZE | imgui.WINDOW_NO_COLLAPSE):
            imgui.text("Add Lecture")
            _, self.add_lecture_text = imgui.input_text("Title", self.add_lecture_text)
            if self.add_lecture_points < 0:
                self.add_lecture_points = 0
            _, self.add_lecture_points = imgui.input_int("Points", self.add_lecture_points)

            if imgui.button("Add"):
                Lecture.create(
                    title=self.add_lecture_text,
                    points=self.add_lecture_points,
                    class_id=id
                )

            imgui.separator()

            if not lectures:
                imgui.text("No Lectures could be queried")
                return

            for n, lecture in enumerate(lectures, start=1):
                display = f"{n}. {lecture.title}"
                opened, _ = imgui.selectable(display, self.select == n-1)
                if opened:
                    self.select = n-1

        return lectures[self.select]
