import imgui
from datatypes import *

from model import *

class StudentInfo:
    def __init__(self):
        super().__init__()

    def __call__(self, student: Student):
        submissions = Submission.select().where(Submission.student_id == student.id) if student else None

        with imgui.begin("Student Info", False, imgui.WINDOW_NO_MOVE | imgui.WINDOW_NO_RESIZE | imgui.WINDOW_NO_COLLAPSE):
            if not student:
                imgui.text("No Student selected")
                return
             
            w, h = imgui.get_window_size()
            imgui.text_colored(f"{student.prename} {student.surname}", *COLOR_TEXT)
            
            content = Class.get_by_id(student.class_id).name
            text_size = imgui.calc_text_size(content)
            imgui.same_line(position=w-1.5*text_size.x)
            imgui.text(content)

            if submissions:
                overall_points = sum([s.points for s in submissions])
                if overall_points.is_integer():
                    overall_points = int(overall_points)
                overall_max = sum([lectures.points for lectures in [Lecture.get_by_id(s.lecture_id) for s in submissions]])
                percentile = overall_points / overall_max
                imgui.progress_bar(percentile, (w*0.5, h*0.05), f"{overall_points}/{overall_max} {percentile:.1%}")
            
            content = "Delete"
            if submissions:
                text_size = imgui.calc_text_size(content)
                imgui.same_line(position=w-2*text_size.x)
            if imgui.button(content):
                # Delete all Submissions related to that Student
                #for submission in submissions:
                #    submission.delete().execute()
                # Delete Student
                #student.delete().execute()
                return

            imgui.separator()

            if not submissions:
                 imgui.text("No Submission for this Student")

            for n, submission in enumerate(submissions, start=1):
                lecture = Lecture.get_by_id(submission.lecture_id)
                
                points = submission.points
                if points.is_integer():
                    points = int(points)
                
                display = f"{n}. {lecture.title} {points}/{lecture.points}"
                COLOR = COLOR_TEXT_PASSED if points > lecture.points*0.3 else COLOR_TEXT_FAILED
                imgui.text_colored(display, *COLOR) 
