import numpy as np
from imgui_bundle import imgui
import random

from datatypes import *

from model import *

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

        self.classes = None
        self.students = None

        self.select_class = 0 
        self.select_student = 0

    def __call__(self):
        
        self.classes = Class.select()
        self.students = Student.select().where(Student.class_id == self.classes[self.select_class].id) if self.classes else None
        
        # Setup Data 
        submissions = Submission.select().where(Submission.student_id == self.students[self.select_student].id) if self.students else None
        data = np.array([submission.points/Lecture.get_by_id(submission.lecture_id).points*100 for submission in submissions], dtype=np.float32) if submissions else None
        
        with imgui.begin("Student Graph", False, imgui.WINDOW_NO_MOVE | imgui.WINDOW_NO_RESIZE | imgui.WINDOW_NO_COLLAPSE):

            w, h = imgui.get_content_region_available()
            
            if not isinstance(data, np.ndarray): 
                imgui.text("No Submission available for this Student")    
            else:
                imgui.plot_histogram(
                    "##Data", data, overlay_text="Performance per Lecture (in %)",
                    scale_min=0.0, scale_max=100,
                    graph_size=(w, h*0.69)
                )
            
            with imgui.begin_child("Select Class", w/3, h*0.3, border=True):
                if not self.classes:
                    imgui.text("No Class could be queried")
                else:
                    for n, c in enumerate(self.classes, start = 1):
                        display = f"{n}. {c.name}"
                        opened, _ = imgui.selectable(display, self.select_class == n-1)
                        if opened:
                            self.select_class = n-1
                            self.select_student = 0
            
            imgui.same_line()

            with imgui.begin_child("Select Student", w/3, h*0.3, border=True):
                if not self.students:
                    imgui.text("No Student in this class")
                else:
                    for n, s in enumerate(self.students, start = 1):
                        display = f"{n}. {s.prename} {s.surname}"
                        opened, _ = imgui.selectable(display, self.select_student == n-1)
                        if opened:
                            self.select_student = n-1

            imgui.same_line()

            with imgui.begin_child("Student Info", w/3, h*0.3, border=True):
                if not submissions:
                    imgui.text("No Submissions for this Student")
                else:
                    for n, s in enumerate(submissions):
                        lecture = Lecture.get_by_id(s.lecture_id)
                        points = s.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)
