from PIL import Image, ImageTk
import PIL
import tkinter as tk
from tkinter import filedialog, messagebox
import cv2
import numpy as np
from Farbaenderung import gammaCorrection, reverseGammaCorrection, Dyschromasie
from Scrollbar import ScrollFrame

root = tk.Tk()
simGrad = tk.IntVar(root)

root.title("Projekt Dyschromasie")

SB = ScrollFrame(root)

img = np.array([])
rows = 0
cols = 0
kanaele = 0

sim_pro = tk.IntVar(root)
sim_deut = tk.IntVar(root)
sim_tri = tk.IntVar(root)

simulationsGradient = tk.Scale(SB.frame, from_=0, to_=100, variable=simGrad, orient='horizontal')
simulationsGradient.grid(column= 0, row = 1, columnspan=10)

def browse():
    # Auswahl des FilePaths
    try:
        path = tk.filedialog.askopenfilename(filetypes=[("Image File", '.jpg'),("Image File", '.png')])
        im = Image.open(path)
    except:
        tk.messagebox.showerror(title='Datenfehler', message='Kein Bild gefunden/ausgewählt')
    global simulateButton
    if len(path) > 0:
        simulateButton.config(state='active')

        # Anzeigen des Bildes
        tkimage = ImageTk.PhotoImage(im)
        myvar = tk.Label(SB.frame, image=tkimage)
        myvar.image = tkimage
        myvar.grid(columnspan=5)

        # Einspeichern der Path-Informationen
        global img, rows, cols, kanaele
        img = cv2.imread(path)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        #img = cv2.GaussianBlur(img, (3, 3), 0)
        rows, cols, kanaele = img.shape


def simulate():
    global img, rows, cols, kanaele, sim_pro, sim_deut, sim_tri
    if sim_deut.get():
        d = Dyschromasie(img, rows, cols, kanaele, simGrad.get()/100, 'd')
        display_array_deut = np.copy(d.Simulate()).astype('uint8')

        T = tk.Text(SB.frame, height=1, width=15)
        T.grid(columnspan=5)
        print_string = "Deutranop(" + str(round(d.sim_faktor*100)) + "%)"
        T.insert('current', print_string)

        conv_SimulationPic_deut = ImageTk.PhotoImage(image=PIL.Image.fromarray(display_array_deut))
        sim_pic_deut = tk.Label(SB.frame, image=conv_SimulationPic_deut)
        sim_pic_deut.Image = conv_SimulationPic_deut
        sim_pic_deut.grid(columnspan=5)
    elif sim_tri.get():
        t = Dyschromasie(img, rows, cols, kanaele, simGrad.get()/100, 't')
        display_array_tri = np.copy(t.Simulate()).astype('uint8')

        T = tk.Text(SB.frame, height=1, width=15)
        T.grid(columnspan=5)
        print_string = "Tritanop(" + str(round(t.sim_faktor * 100)) + "%)"
        T.insert('current', print_string)

        conv_SimulationPic_tri = ImageTk.PhotoImage(image=PIL.Image.fromarray(display_array_tri))
        sim_pic_tri = tk.Label(SB.frame, image=conv_SimulationPic_tri)
        sim_pic_tri.Image = conv_SimulationPic_tri
        sim_pic_tri.grid(columnspan=5)
    elif sim_pro.get():
        p = Dyschromasie(img, rows, cols, kanaele, simGrad.get()/100, 'p')
        display_array_pro = np.copy(p.Simulate()).astype('uint8')

        T = tk.Text(SB.frame, height=1, width=15)
        T.grid(columnspan=5)
        print_string = "Protanop(" + str(round(p.sim_faktor * 100)) + "%)"
        T.insert('current', print_string)

        conv_SimulationPic_pro = ImageTk.PhotoImage(image=PIL.Image.fromarray(display_array_pro))
        sim_pic_pro = tk.Label(SB.frame, image=conv_SimulationPic_pro)
        sim_pic_pro.Image = conv_SimulationPic_pro
        sim_pic_pro.grid(columnspan=5)


btn = tk.Button(SB.frame, text="Browse", width=25, command=browse, bg='light blue')
btn.grid(column=0, row=0, columnspan=2)

simulateButton = tk.Button(SB.frame, text="Simulate", width=25, command=simulate, bg='light blue')
simulateButton.grid(column=1, row=0, columnspan=2)
simulateButton.config(state='disabled')

checkButton_p = tk.Checkbutton(SB.frame, text="Protanop", variable=sim_pro, onvalue=1, offvalue=0, height=5, width=20)
checkButton_d = tk.Checkbutton(SB.frame, text="Deutanop", variable=sim_deut, onvalue=1, offvalue=0, height=5, width=20)
checkButton_t = tk.Checkbutton(SB.frame, text="Tritanop", variable=sim_tri, onvalue=1, offvalue=0, height=5, width=20)

checkButton_p.grid(column=0, row=2)
checkButton_d.grid(column=1, row=2)
checkButton_t.grid(column=2, row=2)

SB.update()
root.mainloop()
