#!/usr/bin/env python
import PySimpleGUI as sg
import io
from PIL import Image
import base64

"""
Shows some fancy button graphics with the help of PIL

Usually when you create Base64 buttons to embed in your PySimpleGUI code, you make the exactly the correct size.  Resizing isn't an option when
    using the tkinter version of PySimpleGUI (except for crude "Scaling")

The PIL code resizes the button images prior to creating the sg.B
"""


DEF_BUTTON_COLOR = ('white', 'black')


def resize_base64_image(image64, size):
    '''
    May not be the original purpose, but this code is being used to resize an image for use with PySimpleGUI (tkinter) button graphics
    :param image64: (str) The Base64 image
    :param size: Tuple[int, int] Size to make the image in pixels (width, height)
    :return: (str) A new Base64 image
    '''
    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 GraphicButton(text, key, image_data, color=DEF_BUTTON_COLOR, size=(100, 50)):
    '''
    A user defined element.  Use this function inside of your layouts as if it were a Button element (it IS a Button Element)
    Only 3 parameters are required.

    :param text: (str) Text you want to display on the button
    :param key:  (Any) The key for the button
    :param image_data: (str) The Base64 image to use on the button
    :param color: Tuple[str, str] Button color
    :param size: Tuple[int, int] Size of the button to display in pixels (width, height)
    :return: (PySimpleGUI.Button) A button with a resized Base64 image applied to it
    '''
    return sg.Button(text, image_data=resize_base64_image(image_data, size), button_color=color, font='Any 15', pad=(0, 0), key=key, border_width=0)


def ShowMeTheButtons():

    sg.theme('Black')

    frame_layout = [[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))],
                    [GraphicButton('Next', '-NEXT-', button64),
                        GraphicButton('Submit', '-SUBMIT-', red_pill64),
                        GraphicButton('OK', '-OK-', green_pill64),
                        GraphicButton('Exit', '-EXIT-', orange64)], ]

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

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

    # ---===--- The event loop --- #
    while True:
        event, values = window.read()
        print(event)
        if event in ('-EXIT-', None):   # Exit button or X
            break


if __name__ == '__main__':

    # To convert your images, use the PySimpleGUI program - Demo_Base64_Image_Encoder.py located on the GitHub (http://www.PySimpleGUI.com)

    orange64 = '[encoded data, 4784 characters]'
    green_pill64 = '[encoded data, 8808 characters]'
    red_pill64 = '[encoded data, 8576 characters]'
    button64 = '[encoded data, 5848 characters]'
    ShowMeTheButtons()
