import dearpygui.dearpygui as dpg

from utils import *
from constants import *
from styles import setup_fonts, setup_themes, apply_theme, get_accent_color
from handlers import setup_handlers
from callbacks import *


def construct_main_window_ui():
    with dpg.window(label='config file editor', tag='main', user_data={'config': None, 'config_file_path': None}):
        with dpg.menu_bar():
            with dpg.menu(label='config'):
                dpg.add_menu_item(label='open\t\t', shortcut='ctrl+o', callback=open_config_open_dialog_clb, tag='open_config_open_dialog')
                dpg.add_menu_item(label='search\t\t', shortcut='ctrl+f', callback=show_config_search_window_clb, tag='show_config_search_window')
                dpg.add_menu_item(label='save\t\t', shortcut='ctrl+s', callback=config_save_clb, tag='save_config')
                dpg.add_menu_item(label='save as\t\t', shortcut='ctrl+shift+s', callback=open_config_save_as_dialog_clb, tag='open_config_save_as_dialog')

            with dpg.menu(label='map'):
                dpg.add_menu_item(label='open map', callback=open_map_open_dialog_clb, tag='open_map_open_dialog')
                with dpg.menu(label='generate map'):
                    for lcz_type in LCZS:
                        dpg.add_menu_item(label=lcz_type, check=False, callback=combo_menu(generate_map_clb), tag=lcz_type)

            with dpg.menu(label='run'):
                dpg.add_menu_item(label='run on lab server', callback=remote_run_clb, tag='remote_run')

            with dpg.menu(label='view'):
                with dpg.menu(label='ui', tag='ui'):
                    for ui in UIS:
                        dpg.add_menu_item(label=ui, check=True, default_value=False, callback=combo_menu(construct_config_ui_clb), tag=ui)
                    dpg.set_value(UIS[1], True)

                with dpg.menu(label='theme', tag='theme'):
                    for theme in THEMES:
                        dpg.add_menu_item(label=theme, check=True, default_value=False, callback=combo_menu(apply_theme_clb), tag=theme)
                    dpg.set_value('orange', True)
    
            dpg.add_progress_bar(label='progress', default_value=0.0, tag='progress_bar', height=FONT_SIZE, width=200, pos=[dpg.get_viewport_width() // 2 - 100 + SPACE, SPACE])
            dpg.add_text('', tag='status_text', show=True)
            dpg.add_loading_indicator(style=1, color=(255, 255, 255, 255), thickness=2, radius=2, tag='loading', show=False)

def construct_crendentials_window():
    with dpg.window(label='credentials', show=False, tag='login_modal', width=550, height=350):
        dpg.add_input_text(label='server username', tag='server_username', password=False, default_value=os.getenv('SERVER_LOGIN', ''), width=300)
        dpg.add_input_text(label='server password', tag='server_password', password=True, default_value=os.getenv('SERVER_PASS', ''), width=300)
        dpg.add_input_text(label='gitlab username', tag='gitlab_username', password=False, default_value=os.getenv('GITLAB_LOGIN', ''), width=300)
        dpg.add_input_text(label='gitlab password', tag='gitlab_password', password=True, default_value=os.getenv('GITLAB_PASS', ''), width=300)
        dpg.add_button(label='submit', callback=start_remote_execution_clb)

def construct_config_open_dialog():
    with dpg.file_dialog(directory_selector=False, show=False, callback=config_open_clb, tag='config_open', width=600, height=400):
        dpg.add_file_extension('.txt', color=(0, 255, 0, 255))
        dpg.add_file_extension('.*', color=(255, 255, 255, 255))

def construct_config_save_as_dialog():
    with dpg.file_dialog(directory_selector=False, show=False, callback=config_save_as_clb, tag='config_save_as', width=600, height=400):
        dpg.add_file_extension('.txt', color=(0, 255, 0, 255))
        dpg.add_file_extension('.*', color=(255, 255, 255, 255))

def construct_map_open_dialog():
    with dpg.file_dialog(directory_selector=False, show=False, callback=map_open_clb, tag='map_open', width=600, height=400):
        dpg.add_file_extension('.txt', color=(0, 255, 0, 255))
        dpg.add_file_extension('.*', color=(255, 255, 255, 255))

def construct_map_save_as_dialog():
    with dpg.file_dialog(directory_selector=False, show=False, callback=map_save_as_clb, tag='map_save_as', width=600, height=400):
        dpg.add_file_extension('.txt', color=(0, 255, 0, 255))
        dpg.add_file_extension('.*', color=(255, 255, 255, 255))

def construct_set_file_path_dialog():
    with dpg.file_dialog(directory_selector=True, show=False, callback=set_file_path_clb, tag='set_folder_dialog', width=500, height=400):
        dpg.add_file_extension('.*', color=(255, 255, 255, 255))

def construct_message_window():
    with dpg.window(label='', no_collapse=True, autosize=True, no_resize=True, modal=True, tag='message_popup', show=False, pos=(dpg.get_viewport_width() / 3, dpg.get_viewport_height() / 3)):
        dpg.add_text('file saved successfully:)')

def construct_search_window():
    with dpg.window(label='', no_collapse=True, autosize=True, no_resize=True, tag='search_popup', show=False, pos=(40, 40)):
        dpg.add_input_text(hint='search for...', on_enter=True, callback=search_clb, tag='search')

def construct_map_edit_window():
    with dpg.texture_registry():
        dpg.add_dynamic_texture(800, 800, np.zeros((800, 800, 4), dtype=np.uint8), tag='heatmap_texture')

    with dpg.theme() as item_theme:
        with dpg.theme_component(dpg.mvAll):
            dpg.add_theme_color(dpg.mvThemeCol_ChildBg, (0, 0, 0, 0), category=dpg.mvThemeCat_Core)
            dpg.add_theme_style(dpg.mvStyleVar_ChildRounding, 0, category=dpg.mvThemeCat_Core)

    dpg.add_colormap_registry(label='colormap registry', tag='colormap_registry')
    dpg.add_colormap(list(map(apply_tint, [[0, 0, 0], [100, 100, 100], [255, 255, 255]])), False, tag='colormap', parent='colormap_registry')

    with dpg.window(label='edit map', tag='map_window', user_data={'height_map': None, 'height_map_file_path': None, 'drawing': False, 'prev_coords': None, 'current_action': None}, horizontal_scrollbar=True, show=False, width=600, height=400, pos=[20, 60], on_close=map_window_on_close_clb):
        with dpg.menu_bar():
            with dpg.menu(label='file'):
                dpg.add_menu_item(label='save', callback=map_save_clb, tag='map_save')
                dpg.add_menu_item(label='save as', callback=open_map_save_as_dialog_clb, tag='open_map_save_as_dialog')

        with dpg.group(horizontal=True):
            with dpg.group(width=200, tag='tools'):
                dpg.add_button(label='erase', callback=set_action_clb, tag='erase')
                dpg.add_button(label='draw rectangle', callback=set_action_clb, tag='draw_rect')
                dpg.add_colormap_slider(tag='height_input')
                dpg.bind_colormap(dpg.last_item(), 'colormap')
            dpg.add_image('heatmap_texture', tint_color=(0, 119, 200, 255), tag='map')

        dpg.add_child_window(tag='drawing_frame', show=False, width=0, height=0)

    dpg.bind_item_theme('drawing_frame', item_theme)


def construct_ui():
    construct_main_window_ui()

    construct_message_window()

    construct_map_open_dialog()
    construct_map_save_as_dialog()
    construct_map_edit_window()
    construct_crendentials_window()

    construct_config_save_as_dialog()
    construct_config_open_dialog()
    construct_search_window()

    construct_set_file_path_dialog()
    
    setup_handlers()
    setup_fonts()
    setup_themes()