
import FreeSimpleGUI as sg

#### ---- GUI Constants

WINDOW_TITLE = 'Модуль управления лазерной схемой оптического смесителя (Отдел радиофотоники МФТИ)'
WINDOW_SIZE = [0, 0]

SET_BUTTON_TEXT = 'Задать'

SET_TEMPERATURE_TEXT_1 = 'Установка температуры лазера 1 (C):'
SET_TEMPERATURE_TEXT_2 = 'Установка температуры лазера 2 (C):'
SET_CURRENT_TEXT_1 = 'Управляющий ток лазера 1 (15-60 мА):'
SET_CURRENT_TEXT_2 = 'Управляющий ток лазера 2 (15-60 мА):'
SET_MANUAL_MODE_TEXT = 'Ручной режим ввода'
SET_TEXT_WIDTH = 34
SET_INPUT_WIDTH = 5

SET_MIN_TEMPERATURE_TEXT_1 = 'Минимальная температура лазера 1 (C):'
SET_MAX_TEMPERATURE_TEXT_1 = 'Максимальная температура лазера 1 (C):'
SET_DELTA_TEMPERATURE_TEXT_1 = 'Шаг дискретизации температуры лазера 1 (0.05-1 С):'
SET_MIN_CURRENT_TEXT_1 = 'Мнимальный ток лазера 1 (мА):'
SET_MAX_CURRENT_TEXT_1 = 'Максимальный ток лазера 1 (мА):'
SET_DELTA_CURRENT_TEXT_1 = 'Шаг дискретизации тока лазера 1 (0.002-0.5 мА):'
SET_MIN_TEMPERATURE_TEXT_2 = 'Минимальная температура лазера 2 (C):'
SET_MAX_TEMPERATURE_TEXT_2 = 'Максимальная температура лазера 2 (C):'
SET_DELTA_TEMPERATURE_TEXT_2 = 'Шаг дискретизации температуры лазера 2 (0.05-1 С):'
SET_MIN_CURRENT_TEXT_2 = 'Мнимальный ток лазера 2 (мА):'
SET_MAX_CURRENT_TEXT_2 = 'Максимальный ток лазера 2 (мА):'
SET_DELTA_CURRENT_TEXT_2 = 'Шаг дискретизации тока лазера 2 (0.002-0.5 мА):'
SET_DELTA_T_TEXT = 'Шаг дискретизации времени (1-100 мкс):'
SET_TAU_T_TEXT = 'Время задержки (3-10мс):'
SET_TEXT_WIDTH_NEW = 40

SET_START_BUTTON_TEXT = 'Пуск'
SET_STOP_BUTTON_TEXT = 'Стоп'


GRAPH_POINTS_NUMBER = 100 # Number of most recent data points shown on charts
GRAPH_CANVAS_SIZE = (0, 0)
GRAPH_BG_COLOR = '#303030'
GRAPH_SIGN_AXES_COLOR = 'orange'

GRAPH_T_MIN = 0 # Celsius
GRAPH_T_MAX = 50 # Celsius
GRAPH_I_MIN = 0.0 # mA
GRAPH_I_MAX = 1.0 # mA


READ_TEMPERATURE_TEXT = 'Температура лазера'
READ_CURRENT_TEXT = 'Ток фотодиода'

VOLTAGE_TEXT_WIDTH = 15


#### ---- Setting GUI

def get_screen_size():
    window = sg.Window('Test')
    global WINDOW_SIZE
    WINDOW_SIZE = window.get_screen_size()
    window.close()
    global GRAPH_CANVAS_SIZE
    GRAPH_CANVAS_SIZE = (int(WINDOW_SIZE[0]/3.5), int(WINDOW_SIZE[0]/(3*2.75)))
    return WINDOW_SIZE

def setup_gui(params):

    sg.theme("DarkBlue12")

    screen_size = get_screen_size()

    layout_input_col1 = [[sg.Text(SET_TEMPERATURE_TEXT_1), sg.Push(),
                          sg.Input(params['Temp_1'], disabled_readonly_background_color="Gray", size=(SET_INPUT_WIDTH,1), key='-InputT1-', disabled = True)],
                        
                        [sg.Text(SET_CURRENT_TEXT_1), sg.Push(),
                         sg.Input(params['Iset_1'], disabled_readonly_background_color="Gray", size=(SET_INPUT_WIDTH,1), key='-InputI1-', disabled = True)],
                        
                        [sg.HSeparator(pad=(1,20))],
                        
                        [sg.Push(), sg.Text(READ_TEMPERATURE_TEXT+' 1: ', key='-TOUT_1-')],
                        
                        [sg.Graph(canvas_size=GRAPH_CANVAS_SIZE, graph_bottom_left=(0, GRAPH_T_MIN), graph_top_right=(GRAPH_POINTS_NUMBER, GRAPH_T_MAX),
                            background_color=GRAPH_BG_COLOR, enable_events=False, drag_submits=False, key='-GraphT1-')],
                        
                        # [sg.HSeparator(pad=(10,15), color=sg.theme_background_color())],

                        [sg.Push(), sg.Text(READ_CURRENT_TEXT+' 1: ', pad=(5, (20,5)), key='-IOUT_1-')],
                        
                        [sg.Graph(canvas_size=GRAPH_CANVAS_SIZE, graph_bottom_left=(0, GRAPH_I_MIN), graph_top_right=(GRAPH_POINTS_NUMBER, GRAPH_I_MAX),
                            background_color=GRAPH_BG_COLOR, enable_events=False, drag_submits=False, key='-GraphI1-')]]

    
    layout_input_col2 = [[sg.Text(SET_TEMPERATURE_TEXT_2), sg.Push(),
                            sg.Input(params['Temp_2'], disabled_readonly_background_color="Gray", size=(SET_INPUT_WIDTH,1), key='-InputT2-', disabled = True)],
                        
                        [sg.Text(SET_CURRENT_TEXT_2), sg.Push(), 
                            sg.Input(params['Iset_2'], disabled_readonly_background_color="Gray", size=(SET_INPUT_WIDTH,1), key='-InputI2-', disabled = True)],
                        
                        [sg.HSeparator(pad=(1,20))],
                        
                        [sg.Push(), sg.Text(READ_TEMPERATURE_TEXT+' 2: ', key='-TOUT_2-')],
                        
                        [sg.Graph(canvas_size=GRAPH_CANVAS_SIZE, graph_bottom_left=(0, GRAPH_T_MIN), graph_top_right=(GRAPH_POINTS_NUMBER, GRAPH_T_MAX),
                            background_color=GRAPH_BG_COLOR, enable_events=False, drag_submits=False, key='-GraphT2-')],

                        # [sg.HSeparator(pad=(10,15), color=sg.theme_background_color())],
                        
                        [sg.Push(), sg.Text(READ_CURRENT_TEXT+' 2: ', pad=(5, (20,5)), key='-IOUT_2-')],
                        
                        [sg.Graph(canvas_size=GRAPH_CANVAS_SIZE, graph_bottom_left=(0, GRAPH_I_MIN), graph_top_right=(GRAPH_POINTS_NUMBER, GRAPH_I_MAX),
                            background_color=GRAPH_BG_COLOR, enable_events=False, drag_submits=False, key='-GraphI2-')]]
    
    layout_input_col3 = [
                        [sg.Text(SET_MANUAL_MODE_TEXT), sg.Checkbox('', default=False, key='-EnableManualSettings-')],
                        
                        [sg.Text(SET_MIN_TEMPERATURE_TEXT_1, size=(SET_TEXT_WIDTH_NEW,1)), 
                            sg.Input(params['Min_Temp_1'], size=(SET_INPUT_WIDTH,1), key='-InputMinT1-', disabled=True, disabled_readonly_background_color="Gray"), sg.Checkbox('', default=False, key='-EnableT1-')],
                        
                        [sg.Text(SET_MAX_TEMPERATURE_TEXT_1, size=(SET_TEXT_WIDTH_NEW,1)), 
                            sg.Input(params['Max_Temp_1'], size=(SET_INPUT_WIDTH,1), key='-InputMaxT1-', disabled=True, disabled_readonly_background_color="Gray")],
                        
                        [sg.Text(SET_MIN_CURRENT_TEXT_1, size=(SET_TEXT_WIDTH_NEW,1)), 
                            sg.Input(params['Min_Current_1'], size=(SET_INPUT_WIDTH,1), key='-InputMinC1-', disabled=True, disabled_readonly_background_color="Gray"), sg.Checkbox('', default=False, key='-EnableC1-')],
                        
                        [sg.Text(SET_MAX_CURRENT_TEXT_1, size=(SET_TEXT_WIDTH_NEW,1)), 
                            sg.Input(params['Max_Current_1'], size=(SET_INPUT_WIDTH,1), key='-InputMaxC1-', disabled=True, disabled_readonly_background_color="Gray")],
                        
                        [sg.Text(SET_DELTA_TEMPERATURE_TEXT_1, size=(SET_TEXT_WIDTH_NEW,1)), 
                            sg.Input(params['Delta_Temp_1'], size=(SET_INPUT_WIDTH,1), key='-InputDeltaT1-', disabled=True, disabled_readonly_background_color="Gray")],
                        
                        [sg.Text(SET_DELTA_CURRENT_TEXT_1, size=(SET_TEXT_WIDTH_NEW,1)), 
                            sg.Input(params['Delta_Current_1'], size=(SET_INPUT_WIDTH,1), key='-InputDeltaC1-', disabled=True, disabled_readonly_background_color="Gray")],
                        
                        [sg.HSeparator(pad=(1,20))],
                        
                        [sg.Text(SET_MIN_TEMPERATURE_TEXT_2, size=(SET_TEXT_WIDTH_NEW,1)), 
                            sg.Input(params['Min_Temp_2'], size=(SET_INPUT_WIDTH,1), key='-InputMinT2-', disabled=True, disabled_readonly_background_color="Gray"), sg.Checkbox('', default=False, key='-EnableT2-')],
                        
                        [sg.Text(SET_MAX_TEMPERATURE_TEXT_2, size=(SET_TEXT_WIDTH_NEW,1)), 
                            sg.Input(params['Max_Temp_2'], size=(SET_INPUT_WIDTH,1), key='-InputMaxT2-', disabled=True, disabled_readonly_background_color="Gray")],
                        
                        [sg.Text(SET_MIN_CURRENT_TEXT_2, size=(SET_TEXT_WIDTH_NEW,1)), 
                            sg.Input(params['Min_Current_2'], size=(SET_INPUT_WIDTH,1), key='-InputMinC2-', disabled=True, disabled_readonly_background_color="Gray"), sg.Checkbox('', default=False, key='-EnableC2-')],
                        
                        [sg.Text(SET_MAX_CURRENT_TEXT_2, size=(SET_TEXT_WIDTH_NEW,1)), 
                            sg.Input(params['Max_Current_2'], size=(SET_INPUT_WIDTH,1), key='-InputMaxC2-', disabled=True, disabled_readonly_background_color="Gray")],
                        
                        [sg.Text(SET_DELTA_TEMPERATURE_TEXT_2, size=(SET_TEXT_WIDTH_NEW,1)), 
                            sg.Input(params['Delta_Temp_2'], size=(SET_INPUT_WIDTH,1), key='-InputDeltaT2-', disabled=True, disabled_readonly_background_color="Gray")],
                        
                        [sg.Text(SET_DELTA_CURRENT_TEXT_2, size=(SET_TEXT_WIDTH_NEW,1)), 
                            sg.Input(params['Delta_Current_2'], size=(SET_INPUT_WIDTH,1), key='-InputDeltaC2-', disabled=True, disabled_readonly_background_color="Gray")],
                        
                        [sg.HSeparator(pad=(1,20))],

                        [sg.Text(SET_DELTA_T_TEXT, size=(SET_TEXT_WIDTH_NEW,1)), 
                            sg.Input(params['Delta_Time'], size=(SET_INPUT_WIDTH,1), key='-InputDeltaTime-', disabled=True, disabled_readonly_background_color="Gray")],
                        
                        [sg.Text(SET_TAU_T_TEXT, size=(SET_TEXT_WIDTH_NEW,1)), 
                            sg.Input(params['Tau'], size=(SET_INPUT_WIDTH,1), key='-InputTau-', disabled=True, disabled_readonly_background_color="Gray")],
                        
                        [sg.HSeparator(pad=(1,20))],
                        
                        [sg.Button(SET_START_BUTTON_TEXT, key='-StartCycle-', disabled_button_color=("Gray22", "Blue"), disabled=True), sg.Button(SET_STOP_BUTTON_TEXT, disabled_button_color=("Gray22", "Blue"), key='-StopCycle-', disabled=True)]]


    layout = [[sg.Column(layout_input_col1), sg.VSeparator(), sg.Column(layout_input_col2), sg.VSeparator(), sg.Column(layout_input_col3)],
              
              [sg.HSeparator(pad=(25,10))],
              
              [sg.Text('', size=(7,1)),
                    sg.Text('T терм 1:', size=(VOLTAGE_TEXT_WIDTH,1), key='-TTerm1-'), sg.Text('T терм 2:', size=(VOLTAGE_TEXT_WIDTH,1), key='-TTerm2-'), 
                    sg.Text('3V3:', size=(VOLTAGE_TEXT_WIDTH,1), key='-3V3-'), sg.Text('5V1:', size=(VOLTAGE_TEXT_WIDTH,1), key='-5V1-'), 
                    sg.Text('5V2:', size=(VOLTAGE_TEXT_WIDTH,1), key='-5V2-'), sg.Text('7V0:', size=(VOLTAGE_TEXT_WIDTH,1), key='-7V0-'),
                    sg.Push(), sg.Text('', key='-DateTime-', pad=(1,10)),
                    sg.Text('', size=(10,1))],
              
              [sg.Exit('Выход', pad=(1,5), size=(10,1), key='-EXIT-')]]
    
    window = sg.Window(WINDOW_TITLE, layout,  finalize=True, element_justification='c', size=screen_size)
    window.bind('<Escape>', '-EXIT-')
    return window


def sign_axes(window):
    signs_dict = {}
    signs_dict['-GraphT1-'] = \
        (window['-GraphT1-'].draw_text(text=str(GRAPH_T_MIN)+' C', location=(3, GRAPH_T_MIN+(GRAPH_T_MAX-GRAPH_T_MIN)*0.05), color=GRAPH_SIGN_AXES_COLOR),
         window['-GraphT1-'].draw_text(text=str(GRAPH_T_MAX)+' C', location=(3, GRAPH_T_MAX-(GRAPH_T_MAX-GRAPH_T_MIN)*0.05), color=GRAPH_SIGN_AXES_COLOR))
    signs_dict['-GraphI1-'] = \
        (window['-GraphI1-'].draw_text(text=str(GRAPH_I_MIN)+' мА', location=(4, GRAPH_I_MIN+(GRAPH_I_MAX-GRAPH_I_MIN)*0.05), color=GRAPH_SIGN_AXES_COLOR),
         window['-GraphI1-'].draw_text(text=str(GRAPH_I_MAX)+' мА', location=(4, GRAPH_I_MAX-(GRAPH_I_MAX-GRAPH_I_MIN)*0.05), color=GRAPH_SIGN_AXES_COLOR))
    signs_dict['-GraphT2-'] = \
        (window['-GraphT2-'].draw_text(text=str(GRAPH_T_MIN)+' C', location=(3, GRAPH_T_MIN+(GRAPH_T_MAX-GRAPH_T_MIN)*0.05), color=GRAPH_SIGN_AXES_COLOR),
         window['-GraphT2-'].draw_text(text=str(GRAPH_T_MAX)+' C', location=(3, GRAPH_T_MAX-(GRAPH_T_MAX-GRAPH_T_MIN)*0.05), color=GRAPH_SIGN_AXES_COLOR))
    signs_dict['-GraphI2-'] = \
        (window['-GraphI2-'].draw_text(text=str(GRAPH_I_MIN)+' мА', location=(4, GRAPH_I_MIN+(GRAPH_I_MAX-GRAPH_I_MIN)*0.05), color=GRAPH_SIGN_AXES_COLOR),
         window['-GraphI2-'].draw_text(text=str(GRAPH_I_MAX)+' мА', location=(4, GRAPH_I_MAX-(GRAPH_I_MAX-GRAPH_I_MIN)*0.05), color=GRAPH_SIGN_AXES_COLOR))
    return signs_dict

