#!/usr/bin/env python
import sys
if sys.version_info[0] >= 3:
    import PySimpleGUI as sg
else:
    import PySimpleGUI27 as sg
import io
from PIL import Image
import base64


def image_file_to_bytes(image64, size):
    image_file = io.BytesIO(base64.b64decode(image64))
    img = Image.open(image_file)
    img.thumbnail(size, Image.ANTIALIAS)
    bio = io.BytesIO()
    img.save(bio, format='PNG')
    imgbytes = bio.getvalue()
    return imgbytes


def ShowMeTheButtons():
    bcolor = ('black', 'black')
    wcolor = ('white', 'black')

    sg.ChangeLookAndFeel('Black')
    sg.SetOptions(auto_size_buttons=True, border_width=0, button_color=sg.COLOR_SYSTEM_DEFAULT)

    toolbar_buttons = [ [sg.Text('Who says Windows have to be ugly when using tkinter?', size=(45,3))],
                         [sg.Text('All of these buttons are part of the code itself', size=(45,2))],

        [sg.Button('Next', image_data=image_file_to_bytes(button64, (100,50)),button_color=wcolor, font='Any 15', pad=(0,0), key='_close_'),
        # [sg.Button('Exit', image_data=image_file_to_bytes(black64, (100,50)),button_color=bcolor, font='Any 15', pad=(0,0), key='_close_'),],
        sg.Button('Submit', image_data=image_file_to_bytes(red_pill64, (100,50)),button_color=wcolor, font='Any 15', pad=(0,0), key='_close_'),
        sg.Button('OK', image_data=image_file_to_bytes(green_pill64, (100,50)),button_color=bcolor, font='Any 15', pad=(0,0), key='_close_'),
        sg.Button('Exit', image_data=image_file_to_bytes(orange64, (100,50)),button_color=bcolor, font='Any 15', pad=(0,0), key='_close_'),],
                        ]

    # layout = toolbar_buttons
    layout =  [[sg.Frame('Nice Buttons', toolbar_buttons, font=('any 18'), background_color='black')]]

    window = sg.Window('Demo of Nice Looking Buttons',
                       no_titlebar=False,
                       grab_anywhere=True,
                       keep_on_top=True,
                       use_default_focus=False,
                       font='any 15',
                       background_color='black').Layout(layout).Finalize()

    # ---===--- Loop taking in user input --- #
    while True:
        button, value = window.Read()
        print(button)
        if button == '_close_' or button is None:
            break           # exit button clicked

if __name__ == '__main__':

    # To convert your PNG into Base 64:
    #   Go to https://www.base64-image.de/
    #   Drag and drop your PNG image onto the webpage
    #   Choose "Copy image"
    #   Create a string variable name to hold your image
    #   Paste data from webpage as a string
    #   Delete the "header" stuff - up to the data portion (data:image/png;base64,)
    orange64 = '[encoded data, 4784 characters]'

    green_pill64 = '[encoded data, 8808 characters]'

    red_pill64 = '[encoded data, 8576 characters]'

    button64 = '[encoded data, 5848 characters]'

    ShowMeTheButtons()

