from imgui_bundle import (
    imgui,
    immapp,
    imgui_md
)

from dbmodel import *
from .analyzer_state import AnalyzerState

state = AnalyzerState()

@immapp.static(inited=False)
def table(class_id: int) -> None:
    if class_id < 1:
        return 

    statics = table 
    
    if not statics.inited:
        statics.class_id = class_id
        statics.table_flags = (
            imgui.TableFlags_.row_bg.value
            | imgui.TableFlags_.borders.value 
            | imgui.TableFlags_.resizable.value 
            | imgui.TableFlags_.sizing_stretch_same.value
        )
        
        statics.students = Student.select().where(Student.class_id == statics.class_id)
        statics.lectures = Lecture.select().where(Lecture.class_id == statics.class_id)

        statics.rows = len(statics.students)
        statics.cols = len(statics.lectures)
        statics.grid = [
            list(Submission.select().where(Submission.student_id == student.id))
            for student in statics.students
        ]

        statics.table_header = [f"{lecture.title} ({lecture.points})" for lecture in statics.lectures]
        statics.inited = True

    if statics.class_id != class_id:
        statics.inited = False

    if statics.cols != Lecture.select().where(Lecture.class_id == statics.class_id).count():
        statics.inited = False

    if len(statics.students) != Student.select().where(Student.class_id == statics.class_id).count():
        statics.inited = False

    if imgui.begin_table("Student Grid", statics.cols+1, statics.table_flags):
        # Setup Header 
        imgui.table_setup_column("Students")
        for header in statics.table_header:
            imgui.table_setup_column(header)
        imgui.table_headers_row()

        # Fill Student names
        for row in range(statics.rows):
            imgui.table_next_row()
            imgui.table_set_column_index(0)
            student = statics.students[row]
            imgui.text(f"{student.prename} {student.surname}")

            for col in range(statics.cols):
                imgui.table_set_column_index(col+1)
                changed, value = imgui.input_float(f"##{statics.grid[row][col]}", statics.grid[row][col].points, 0.0, 0.0, "%.1f")
                
                if changed:
                    # Boundary Check
                    if value < 0:
                        value = 0 
                    if value > statics.lectures[col].points:
                        value = statics.lectures[col].points 
                    
                    old_value = statics.grid[row][col].points
                    statics.grid[row][col].points = value
                    statics.grid[row][col].save()

                    student = statics.students[row]
                    lecture = statics.lectures[col]
                    sub = statics.grid[row][col]

        imgui.end_table()


def submission_table() -> None:
    
    imgui_md.render_unindented("# Submissions")

    if db.is_closed():
        imgui.text("No DB loaded")
        return
    
    table(state.class_id)
