import dearpygui.dearpygui as dpg

import os

from remote_run_constants import *
from remote_run_callbacks import *
from remote_run import *

def construct_remote_run_window():
    if 'USE_BUILDS' in os.environ:
        conn = {
            LAB: Connection(LAB_HOST, os.getenv('SERVER_LOGIN', ''), os.getenv('SERVER_PASS', '')),
            # LOMONOSOV: Connection(LOMONOSOV_HOST, os.getenv('LOMONOSOV_LOGIN'), id_rsa_path=os.getenv('ID_RSA_PATH', ''))
        }
        builds = {
            (LAB, URBAN_LES): 'prebuild/code/nsenx',
            # (LOMONOSOV, URBAN_LES): '6e094bc0-a5f6-4474-8c71-a2e3a90d8e85/build/nse-gabls1-urban-les'
        }
        user_data = {
            'connections': conn,
            'builds': builds
        }
    else:
        user_data = {
            'connections': dict(),
            'builds': dict()
        }

    with dpg.window(
                label='remote run',
                tag='run_window',
                horizontal_scrollbar=True,
                show=False,
                width=1000,
                height=600,
                pos=[20, 60],
                user_data=user_data):
        with dpg.menu_bar():
            dpg.add_menu_item(label='run', callback=show_run_model_window_clb, tag='run')

        with dpg.table(header_row=True, row_background=False, borders_outerH=True, borders_innerH=True, borders_innerV=False, borders_outerV=False, delay_search=True, tag='models_table'):
            dpg.add_table_column(label='Type')
            dpg.add_table_column(label='Running on')
            dpg.add_table_column(label='Status')
            dpg.add_table_column(label='Progress')
            dpg.add_table_column(label='')

    with dpg.theme(tag='progress_bar_theme'):
        with dpg.theme_component(dpg.mvAll):
            dpg.add_theme_color(dpg.mvThemeCol_FrameBg, (255, 255, 255, 100), category=dpg.mvThemeCat_Core)

    # load_running_sessions()

def construct_run_model_window():
    xoffset = 400
    width = 300
    with dpg.window(label='remote run settings', show=False, tag='run_model_window', autosize=True, pos=[50, 50]):
        with dpg.group(horizontal=True, xoffset=xoffset, tag='machines_group'):
            dpg.add_text('choose machine')
            dpg.add_radio_button(items=('lab server', 'lomonosov 2'), default_value='lab server', callback=update_run_model_window_clb, tag='machine', horizontal=True)
        with dpg.group(horizontal=True, xoffset=xoffset, tag='models_group'):
            dpg.add_text('choose model')
            dpg.add_radio_button(items=('urban les',), callback=update_run_model_window_clb, default_value='urban les', tag='model', horizontal=True)

        with dpg.child_window(label='credentials', tag='credentials', auto_resize_y=True):
            with dpg.group(tag='gitlab_group'):
                dpg.add_input_text(label='gitlab username', tag='gitlab_username', password=False, default_value=os.getenv('GITLAB_LOGIN', ''), width=width)
                dpg.add_input_text(label='gitlab password', tag='gitlab_password', password=True, default_value=os.getenv('GITLAB_PASS', ''), width=width)

            with dpg.group(tag='lab_server_group'):
                dpg.add_input_text(label='server username', tag='server_username', password=False, default_value=os.getenv('SERVER_LOGIN', ''), width=width)
                dpg.add_input_text(label='server password', tag='server_password', password=True, default_value=os.getenv('SERVER_PASS', ''), width=width)

            with dpg.group(tag='lomonosov_server_group'):
                dpg.add_input_text(label='lomonosov username', tag='lomonosov_username', default_value=os.getenv('LOMONOSOV_LOGIN', ''), width=width)

        with dpg.group(horizontal=True, xoffset=xoffset, tag='config_file_path_group'):
            dpg.add_text('config file path')
            with dpg.group(horizontal=True):
                dpg.add_input_text(default_value='', tag='config_file_path', width=width)
                dpg.add_button(label='...', callback=open_config_file_path_dialog_clb)
        with dpg.group(horizontal=True, xoffset=xoffset):
            dpg.add_text('number of processes for mpi run')
            dpg.add_input_int(default_value=8, width=width, min_value=1, max_value=200, min_clamped=True, max_clamped=True, tag='-np')
        with dpg.group(horizontal=True, xoffset=xoffset):
            dpg.add_text('arch')
            dpg.add_radio_button(items=('gpu', 'cpu', 'mix'), label='arch', default_value='cpu', tag='-arch', horizontal=True)
        with dpg.group(horizontal=True, xoffset=xoffset):
            dpg.add_text('use dump')
            dpg.add_checkbox(default_value=False, tag='use-udump', callback=lambda s, a, u: dpg.show_item('-udump-group') if a else dpg.hide_item('-udump-group'))
        with dpg.group(horizontal=True, xoffset=xoffset, tag='-udump-group', show=False):
            dpg.add_text('dump from this control points')
            dpg.add_input_int(default_value=1, width=width, min_value=1, max_value=200, min_clamped=True, max_clamped=True, tag='-udump', enabled=False)
        with dpg.group(horizontal=True, xoffset=xoffset):
            dpg.add_text('write model stdout to file')
            dpg.add_checkbox(default_value=False, tag='use-model-stdout', callback=lambda s, a, u: dpg.show_item('-model-stdout-group') if a else dpg.hide_item('-model-stdout-group'))
        with dpg.group(horizontal=True, xoffset=xoffset, tag='-model-stdout-group', show=False):
            dpg.add_text('filename')
            dpg.add_input_text(default_value='log.txt', tag='-model-stdout', width=width, enabled=False)

        dpg.add_button(label='run', callback=run_model_clb)

def construct_select_download_folder_dialog():
    with dpg.file_dialog(directory_selector=True, modal=True, show=False, callback=set_download_output_to_folder_clb, tag='download_to_folder_dialog', width=500, height=400):
        dpg.add_file_extension('.*', color=(255, 255, 255, 255))

def construct_select_config_file_path_dialog():
    with dpg.file_dialog(directory_selector=False, modal=True, show=False, callback=set_config_file_path_clb, tag='config_file_path_dialog', width=500, height=400):
        dpg.add_file_extension('.txt', color=(0, 255, 0, 255))
        dpg.add_file_extension('.*', color=(255, 255, 255, 255))

def construct_model_output_window():
    with dpg.window(label='select model output to download', show=False, tag='model_output_window', width=600, height=600, pos=[50, 50]):
        dpg.add_child_window(height=500, tag='model_output_child_window')
        with dpg.group(horizontal=True):
            dpg.add_text('download to')
            dpg.add_input_text(default_value='output', tag='download_output_to_folder')
            dpg.add_button(label='...', callback=open_download_to_folder_dialog_clb)
        dpg.add_button(label='download selected files', callback=download_output_clb)

def construct_ui():
    construct_remote_run_window()
    construct_run_model_window()
    construct_model_output_window()
    construct_select_download_folder_dialog()
    construct_select_config_file_path_dialog()