from imgui_bundle import (
    imgui,
    immapp,
    imgui_md,
    hello_imgui
)

from grader import get_gradings, get_grader

from dbmodel import *

@immapp.static(inited=False)
def editor() -> None:
    """
    Database Editor UI Function.
    
    Calls the class editor function to render its UI component.
    """
    if db.is_closed():
        imgui.text("Please open a Database")
        return 

    statics = editor
    if not statics.inited:
        statics.selected = 0
        statics.actions = ["Create", "Update", "Delete"]
        statics.action = str()
        statics.inited = True
        statics.selectors = dict()

    imgui.text("Select what you want to Edit:")
    changed, statics.selected = imgui.combo('##DBSelector', statics.selected, table_labels)
    for action in statics.actions:
        if imgui.button(action):
            imgui.open_popup(table_labels[statics.selected])
            statics.action = action
        imgui.same_line()
    
    if imgui.begin_popup_modal(table_labels[statics.selected])[0]:
        table = tables[statics.selected]
        imgui_md.render(f"# {statics.action} {table_labels[statics.selected]}")
        
        if imgui.begin_table("Editor Grid", 2, imgui.TableFlags_.row_bg.value):
            # Setup Header
            for header in ["Attribute", "Value"]:
                imgui.table_setup_column(header, imgui.TableColumnFlags_.width_stretch.value, 1/len(header))
            imgui.table_headers_row()
        

            student_editor(statics.action)

            imgui.end_table()
        
        
        imgui.end_popup()

@immapp.static(inited=False)
def student_editor(action: str) -> dict:
    '''

    '''
    statics = student_editor
    if not statics.inited:
        statics.classes = tuple(Class.select())
        statics.residences = tuple(Residence.select())
        statics.classes_labels = tuple(clas.name for clas in statics.classes)
        statics.graders = tuple(grader.alt_name for grader in get_gradings())
        statics.buffer = {
            'prename': "",
            'surname': "",
            'sex': 0,
            'class_id': 0,
            'group_id': 0,
            'grader': 0,
            'residence': 0,

        }
        statics.inited = True
    
    imgui.table_next_row()
    imgui.table_set_column_index(0)
    imgui.text("First Name")
    imgui.table_set_column_index(1)
    _, statics.buffer['prename'] = imgui.input_text("##prename", statics.buffer['prename'])

    imgui.table_next_row()
    imgui.table_set_column_index(0)
    imgui.text("Last Name")
    imgui.table_set_column_index(1)
    _, statics.buffer['surname'] = imgui.input_text("##surname", statics.buffer['surname'])
   
    imgui.table_next_row()
    imgui.table_set_column_index(0)
    imgui.text("Gender")
    imgui.table_set_column_index(1)
    _, statics.buffer['sex'] = imgui.combo("##sex", statics.buffer['sex'], ['Male', 'Female'])

    imgui.table_next_row()
    imgui.table_set_column_index(0)
    imgui.text("Class")
    imgui.table_set_column_index(1)
    _, statics.buffer['class_id'] = imgui.combo("##class_id", statics.buffer['class_id'], statics.classes_labels)
    
    imgui.table_next_row()
    imgui.table_set_column_index(0)
    imgui.text("Project Group")
    imgui.table_set_column_index(1)
    groups = tuple(Group.select().where(Group.class_id == statics.classes[statics.buffer['class_id']].id))
    _, statics.buffer['group_id'] = imgui.combo("##group_id", statics.buffer['group_id'], tuple(group.name for group in groups))
    
    imgui.table_next_row()
    imgui.table_set_column_index(0)
    imgui.text("Grader")
    imgui.table_set_column_index(1)
    _, statics.buffer['grader'] = imgui.combo("##grader", statics.buffer['grader'], statics.graders)

    imgui.table_next_row()
    imgui.table_set_column_index(0)
    imgui.text("Residence")
    imgui.table_set_column_index(1)
    _, statics.buffer['residence'] = imgui.combo("##residence", statics.buffer['residence'], [str(residence.id) for residence in statics.residences])

    if imgui.button(action):
        match action:
            case "Create":
                Student.create(
                    prename = statics.buffer['prename'],
                    surname = statics.buffer['surname'],
                    sex = 'Female' if statics.buffer['sex'] else 'Male',
                    class_id = statics.classes[statics.buffer['class_id']].id,
                    group_id = groups[statics.buffer['group_id']].id,
                    residence = statics.residences[statics.buffer['residence']] 
                )
            case "Update":
                pass
            case "Delete":
                pass
        statics.inited = False
        imgui.close_current_popup()


