
"""
Graphical Interface used to generate burst_quality metric. 

The program takes as input images generated by "generate_pred_vs_def_bursts.py", stored in
""data/burst_quality/images"", and show them, together with three buttons: 'Red', 'Blue', 'Equal'.
When one of the button is clicked, the information about (image, button) is saved in an txt file 
in "GUI", and the next image is loaded. 

"""


import tkinter as tk
from PIL import ImageTk, Image
import glob
import pandas
import numpy as np 
import os



if os.path.exists('GUI') is False:
    os.mkdir('GUI')
writepath = 'GUI/burst_quality.txt'

if os.path.exists(writepath):
    print('File already exists')
    quit()

f = open(writepath, 'w+')

IMAGE_WIDTH = 1600
IMAGE_HEIGHT = 900
IamOnWindows = False

#filenames_and_ids = []
filenames = []
directory_name = "data/burst_quality/images/"
splitter_sign = "/"


for file in glob.glob(directory_name+"*.png"):
    filename = file.split(splitter_sign)[-1]
    filenames.append(filename)

filenames = np.array(filenames)
def load_next_image(fig):

    global at_the_top 
    print(filenames[0])
    img1 = ImageTk.PhotoImage(Image.open(directory_name+"%s" % filenames[0]).resize((IMAGE_WIDTH,IMAGE_HEIGHT)))
    fig.config(image = img1)
    fig.img = img1



    
def classify(fig, quality):
    global f, filenames, at_the_top
    f.write('%s    %s    \n' % (filenames[0], quality))
    filenames = np.delete(filenames, 0)
    
    load_next_image(fig)
    
window = tk.Toplevel()
window.title('Burst classification')
window.rowconfigure(0, weight=1)
window.columnconfigure(2, weight=1)


fr_img = tk.Frame(window)
img = ImageTk.PhotoImage(Image.open("GUI/cover.png").resize((IMAGE_WIDTH,IMAGE_HEIGHT)))
fig = tk.Label(fr_img, image = img, width=IMAGE_WIDTH, height=IMAGE_HEIGHT)
fig.grid(row=0, column = 0)


fr_setting_buttons = tk.Frame(window)
btn_start = tk.Button(fr_setting_buttons, text = 'Start', command = lambda:load_next_image(fig))
btn_start.grid(row=0, column = 0)
    

fr_classify_buttons = tk.Frame(window)
btn_top = tk.Button(fr_classify_buttons, text='Blue', command = lambda:classify(fig, 'Blue'))
btn_bottom = tk.Button(fr_classify_buttons, text = "Red", command = lambda:classify(fig, 'Red'))
btn_equal = tk.Button(fr_classify_buttons, text = 'Equal', command = lambda:classify(fig, 'Equal'))
btn_top.grid(row=0, column=0)
btn_bottom.grid(row=1, column=0)
btn_equal.grid(row=2, column=0)



fr_setting_buttons.grid(row=0, column=0)
fr_img.grid(row=0, column=1)
fr_classify_buttons.grid(row=0, column = 2)

window.mainloop()