import PySimpleGUI as sg

"""
    Demo - A simple minimal window with a material design feel
    
    Contains base64 images for:
    * The PSG Yellow Graphic
    * The 2 toggle buttons
    * The large spinning animation

    Copyright 2021 PySimpleGUI
"""

def make_window(light_mode):
    if light_mode:
        sg.theme('light grey')
    else:
        sg.theme('black')
    BLUE = '#2196f2'
    DARK_GRAY = '#212021'
    LIGHT_GRAY = '#e0e0e0'
    BLUE_BUTTON_COLOR = '#FFFFFF on #2196f2'
    GREEN_BUTTON_COLOR ='#FFFFFF on #00c851'
    LIGHT_GRAY_BUTTON_COLOR = f'#212021 on #e0e0e0'
    DARK_GRAY_BUTTON_COLOR = '#e0e0e0 on #212021'

    layout = [[sg.Col([[sg.T('Welcome to my App')],
                       [sg.T('Your license status: '), sg.T('Trial', k='-LIC STATUS-')],
                       [sg.B('Light', size=(10,2),button_color=LIGHT_GRAY_BUTTON_COLOR), sg.B('Dark', size=(10,2), button_color=DARK_GRAY_BUTTON_COLOR)],
                       [sg.T()],
                       [sg.Image(data=PSG_GRAPHIC)],
                       [sg.B(image_data=T_OFF, k='-TOGGLE1-', metadata=False, button_color=sg.theme_background_color(), border_width=0, image_subsample=2),
                        sg.B(image_data=T_ON, k='-TOGGLE2-', button_color=sg.theme_background_color(), border_width=0, image_subsample=2, metadata=True)],
                       [sg.T()],
                       [sg.B('Do Something', size=(14,2), button_color=BLUE_BUTTON_COLOR),
                        sg.B('Upgrade', size=(14,2), button_color=GREEN_BUTTON_COLOR),
                        sg.B('Exit', size=(14,2), button_color=LIGHT_GRAY_BUTTON_COLOR)],
                       [sg.Image(k='-GIF-', metadata=0)],
                       [sg.T('The end of "my App"')]], element_justification='c', k='-TOP COL-')]]

    layout = [[sg.Titlebar('Material Design Custom Titlebar',
                           background_color=BLUE if light_mode else DARK_GRAY,
                           text_color='white' if light_mode else LIGHT_GRAY,
                           k='-TITLEBAR-')]] + layout

    window = sg.Window('Window Title', layout)

    return window


def main():
    light_mode = True
    window = make_window(light_mode)
    show_animation = False
    # LOADING_GIF = sg.DEFAULT_BASE64_LOADING_GIF
    while True:             # Event Loop
        event, values = window.read(timeout=100)
        if event == sg.WIN_CLOSED or event == 'Exit':
            break
        if event.startswith('-TOGGLE'):
            state = window[event].metadata = not window[event].metadata
            window[event].update(image_data=T_ON if state else T_OFF, image_subsample=2)
        elif event == 'Do Something':
            show_animation = True
            window['-GIF-'].metadata = 0
        elif event == 'Upgrade':
            sg.popup('This is where you would do', 'the upgrade window code')
        elif event == 'Light' and not light_mode or event == 'Dark' and light_mode:
            light_mode = not light_mode
            window.close()
            window = make_window(light_mode)
        # Do the animation stuff
        if show_animation:
            window['-GIF-'].update_animation(LOADING_GIF, time_between_frames=100)
            window['-GIF-'].metadata += 1
            if window['-GIF-'].metadata > 50:
                show_animation = False
                window['-GIF-'].update(data='')     # set to illegal value to erase the image

    window.close()

if __name__ == '__main__':
    T_OFF = b'iVBORw0KGgoAAAANSUhEUgAAAFAAAAA8CAYAAADxJz2MAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAgmSURBVHhe7ZpdbFTHFcfPzBrb613bGLt4/VEC2AVaQ0RIkNIWJN7iB6TyUiXqawrJY0yEhJEiWYpa06oFqr600Ic8VGqlpGqkShWWeCCqpSIRJwURCGVDDP5a24vX66/1x+6dnv/cucv1F14M9l7T+5NW996ZK8v73zNzzpxzyMfHx8fHx8fH5/8SYa6r5u3T5xqlEMf402BlMhEpAhFLWBFBYrt5xRMoUt2KRDxAKpYRIiaV+kYJcfnSL1v+Y15ZFasS8N0zv9urlHpTWdZRkmK/Gd6oRFmETzMZ629/+vX718xYzjyVgLC2gJAf8u/5lhmigoIAbauNUOWWMgqVFFMoGORrkMKhoHnDG0xMpmgqNU2T/JmamqZ4Ikl9A0M0PTNr3iCySFwWQrU+jVXmJOCJ939TJYoKPrQs9XMhqCAgA7S7cRsLV0011ZUkeHAjwquIhuIJetg3SHe/eUizc3P2hFB/lpb44A9nW7rtgeV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htsBWaF0uODwczQYjqBptdfidEIxylJtHK1J5TLUeITNl/9FFMChDNxSHyDxhr0hkBdbQWnkuOXYjQwNP1ChPC+u/JQa2A6LX/f4QAa4Yy60HBos/YZICVA4HVELcutD4016A2xGfzAL3+JUmff9AXY+MyjF6MTemY6ed/Di7VqlL1HRo9MRm3w1oWjfS+v33mZhzv7+wdkIBCwOtE6wrK7cTeuEyGl871TTjhgW7jGDqPh2sSuhL7ePmpsbNRjJcR7DQerz+gLw4TiauOFr26veGPnAP+jNvZGelUg4BfBoL1CxM9tZafyR3uc8n2CikKenAUJwTVxnFd/9SH92TGir9OB8G7cvMW7mRL8lI6w8D41SymyftXWj84cZpXdhTEajNDeMBdAAQg1jPT2tmQ+L9He5uizLY5nOGFkiythss3NzSwCXKlj7HXfTTqOdB75rLbVnt6jSOmOBfSGoL3heetMHQ+8LRyG3vMAa15/Xv7uyYQHHqvsW/ec3qiE+pZNOoTeELQ3oEI/V5otcwVRB+I8hCra26I7VYgP2Gy/MLdMSk67ha4de7yf8YazEdemT0SX90yNdM6Csy2OZzhhIEhOoC7A2548UH3JTGQlJwEmQVeSM0qHWP3WmSkqKCigIhZkIXvr5H9xSNUMLAOJULyQz0NKClmV9DQeC+M6guT0OO9xPJEAk6CxhiMH9IZAI0v05ByFTTdqUnrHcTzLttdlY0oCTILeELQ3oELP/5RK1ElR6kO1ytxiGz38g7t0JllQGPk8pKSQVTHrLi4uLi4uLi4uLjlA9C9TVjLI3KTNogAAAABJRU5ErkJggg=='
    PSG_GRAPHIC = b'[encoded data, 4884 characters]'
    LOADING_GIF = b'[encoded data, 30776 characters]'
    main()