# Custom
from model import *
from appstate import AppState

# Layouts
from analyzer import analyzer_layout
from database import database_editor_layout

# External
from imgui_bundle import imgui, immapp, hello_imgui, ImVec2

# Built In
import shelve
from typing import List

def menu_bar(runner_params: hello_imgui.RunnerParams) -> None:
    hello_imgui.show_app_menu(runner_params)
    hello_imgui.show_view_menu(runner_params)
    if imgui.begin_menu("File"):
        clicked, _ = imgui.menu_item("Open", "", False)
        if clicked:
            pass
        imgui.end_menu()

def status_bar(app_state: AppState) -> None:
    imgui.text("Student Analyzer by @DerGrumpf")

def main() -> None:
    app_state = AppState()
    
    # Load Database 
    with shelve.open("state") as state:
        v = state.get("DB")
        if v:
            db.init(v)
            db.connect()
            db.create_tables([Class, Student, Lecture, Submission])
            app_state.update()

    # Set Asset Folder
    #hello_imgui.set_assets_folder()
    
    # Set Theme

    # Set Window Params
    runner_params = hello_imgui.RunnerParams()
    runner_params.app_window_params.window_title = "Analyzer"
    runner_params.imgui_window_params.menu_app_title = "Analyzer"
    runner_params.app_window_params.window_geometry.size = (1000, 900)
    runner_params.app_window_params.restore_previous_geometry = True
    runner_params.app_window_params.borderless = True
    runner_params.app_window_params.borderless_movable = True 
    runner_params.app_window_params.borderless_resizable = True 
    runner_params.app_window_params.borderless_closable = True 

    # Load Fonts 
    #runner_params.callbacks.load_additional_fonts = lambda: f()

    # Status Bar & Main Menu
    runner_params.imgui_window_params.show_menu_bar = True
    runner_params.imgui_window_params.show_menu_app = False
    runner_params.imgui_window_params.show_menu_view = False
    runner_params.imgui_window_params.show_status_bar = True
    # Inside `show_menus`, we can call `hello_imgui.show_view_menu` and `hello_imgui.show_app_menu` if desired
    runner_params.callbacks.show_menus = lambda: menu_bar(runner_params)
    # Optional: add items to Hello ImGui default App menu
    #runner_params.callbacks.show_app_menu_items = show_app_menu_items
    runner_params.callbacks.show_status = lambda: status_bar(app_state)
    
    # Application layout
    runner_params.imgui_window_params.default_imgui_window_type = (
        hello_imgui.DefaultImGuiWindowType.provide_full_screen_dock_space
    )
    runner_params.imgui_window_params.enable_viewports = True 

    runner_params.docking_params = analyzer_layout(app_state)
    runner_params.alternative_docking_layouts = [
        database_editor_layout(app_state)
    ]

    # Save App Settings
    runner_params.ini_folder_type = hello_imgui.IniFolderType.app_user_config_folder
    runner_params.ini_filename = "Analyzer/Analyzer.ini"
    
    # Uncomment if layout will stay the same at start
    runner_params.docking_params.layout_condition = hello_imgui.DockingLayoutCondition.application_start

    # Run it
    add_ons_params = immapp.AddOnsParams()
    add_ons_params.with_markdown = True
    add_ons_params.with_implot = True
    add_ons_params.with_implot3d = True
    immapp.run(runner_params, add_ons_params)
    
if __name__ == "__main__":
    main()
