import tkinter as tk
import tkinter.messagebox
from Control import Control

class View:
    def __init__(self, control : Control.Control):
        self.control = control

        # Init Window
        self.root = tk.Tk()
        self.root.title(u"Header File Generator")
        self.root.geometry("700x800")

        self.config_frame = tk.Frame(self.root)

        # Config Table
        lb_symbol = tk.Label(self.config_frame, width = 20)
        lb_symbol["text"] = "Symbol"
        lb_symbol.grid(row = 0, column = 0)

        lb_description = tk.Label(self.config_frame, width = 40)
        lb_description["text"] = "Detail"
        lb_description.grid(row = 0, column = 1)

        lb_enable = tk.Label(self.config_frame, width = 10)
        lb_enable["text"] = "Enable"
        lb_enable.grid(row = 0, column = 2)

        for i, config in enumerate(self.control.getConfigs()):
            symbol_entry = tk.Entry(self.config_frame, width=20)
            symbol_entry.insert(tk.END, config.symbol)
            symbol_entry.config(state = tk.DISABLED)
            symbol_entry.config(disabledforeground = "black", disabledbackground = "white")
            symbol_entry.grid(row= i + 1, column = 0)

            detail_entry = tk.Entry(self.config_frame, width=40)
            detail_entry.insert(tk.END, config.detail)
            detail_entry.config(state = tk.DISABLED)
            detail_entry.config(disabledforeground = "black", disabledbackground = "white")
            detail_entry.grid(row= i + 1, column = 1)

            bt_enable = tk.Button(self.config_frame, text="ON", width= 5)
            bt_enable["text"] = "ON" if config.enable else "OFF"
            color = "green" if config.enable else "red"
            bt_enable.config(bg=color, activebackground = color)
            bt_enable["command"] = lambda id = i, button = bt_enable : self.toggle_config_enable(id, button)
            bt_enable.grid(row = i + 1, column = 2)
        self.config_frame.pack(side=tk.TOP, anchor=tk.NW)


        self.value_config_frame = tk.Frame(self.root)

        # Config Table
        lb_symbol = tk.Label(self.value_config_frame, width = 20)
        lb_symbol["text"] = "Symbol"
        lb_symbol.grid(row = 0, column = 0)

        lb_description = tk.Label(self.value_config_frame, width = 40)
        lb_description["text"] = "Detail"
        lb_description.grid(row = 0, column = 1)

        lb_value = tk.Label(self.value_config_frame, width = 10)
        lb_value["text"] = "Value"
        lb_value.grid(row = 0, column = 2)

        lb_enable = tk.Label(self.value_config_frame, width = 10)
        lb_enable["text"] = "Enable"
        lb_enable.grid(row = 0, column = 3)

        for i, val_config in enumerate(self.control.getValConfigs()):
            symbol_entry = tk.Entry(self.value_config_frame, width=20)
            symbol_entry.insert(tk.END, val_config.symbol)
            symbol_entry.config(state = tk.DISABLED)
            symbol_entry.config(disabledforeground = "black", disabledbackground = "white")
            symbol_entry.grid(row= i + 1, column = 0)

            detail_entry = tk.Entry(self.value_config_frame, width=40)
            detail_entry.insert(tk.END, val_config.detail)
            detail_entry.config(state = tk.DISABLED)
            detail_entry.config(disabledforeground = "black", disabledbackground = "white")
            detail_entry.grid(row= i + 1, column = 1)

            value_entry = tk.Entry(self.value_config_frame, width=10)
            value_entry.insert(tk.END, val_config.value)
            value_entry.config(state = tk.DISABLED)
            value_entry.config(disabledforeground = "black", disabledbackground = "white")
            value_entry.grid(row= i + 1, column = 2)

            bt_enable = tk.Button(self.value_config_frame, text="ON", width= 5)
            bt_enable["text"] = "ON" if val_config.enable else "OFF"
            color = "green" if val_config.enable else "red"
            bt_enable.config(bg=color, activebackground = color)
            bt_enable["command"] = lambda id = i, button = bt_enable : self.toggle_val_config_enable(id, button)
            bt_enable.grid(row = i + 1, column = 3)
        self.value_config_frame.pack(side=tk.TOP, anchor=tk.W)

        # Generator Button
        self.bt_generate = tk.Button(self.root)
        self.bt_generate["text"] = "Generate Header"
        self.bt_generate["command"] = self.generateHeader
        self.bt_generate.pack(side=tk.BOTTOM, anchor=tk.SE)

    def start(self):
        self.root.mainloop()

    def generateHeader(self):
        self.control.generateHeader()
        tk.messagebox.showinfo("Header Generator Info", "Generated:{0}".format(self.control.header_config.path))

    def update(self):
        pass

    def toggle_config_enable(self, id, button : tk.Button):
        config = self.control.getConfigs()[id]
        config.enable = not config.enable
        button["text"] = "ON" if config.enable else "OFF"
        color = "green" if config.enable else "red"
        button.config(bg=color, activebackground = color)
    
    def toggle_val_config_enable(self, id, button : tk.Button):
        val_config = self.control.getValConfigs()[id]
        val_config.enable = not val_config.enable
        button["text"] = "ON" if val_config.enable else "OFF"
        color = "green" if val_config.enable else "red"
        button.config(bg=color, activebackground = color)

[((213, 220), 'tkinter.Tk', 'tk.Tk', ([], {}), '()\n', (218, 220), True, 'import tkinter as tk\n'), ((338, 357), 'tkinter.Frame', 'tk.Frame', (['self.root'], {}), '(self.root)\n', (346, 357), True, 'import tkinter as tk\n'), ((402, 439), 'tkinter.Label', 'tk.Label', (['self.config_frame'], {'width': '(20)'}), '(self.config_frame, width=20)\n', (410, 439), True, 'import tkinter as tk\n'), ((549, 586), 'tkinter.Label', 'tk.Label', (['self.config_frame'], {'width': '(40)'}), '(self.config_frame, width=40)\n', (557, 586), True, 'import tkinter as tk\n'), ((701, 738), 'tkinter.Label', 'tk.Label', (['self.config_frame'], {'width': '(10)'}), '(self.config_frame, width=10)\n', (709, 738), True, 'import tkinter as tk\n'), ((2041, 2060), 'tkinter.Frame', 'tk.Frame', (['self.root'], {}), '(self.root)\n', (2049, 2060), True, 'import tkinter as tk\n'), ((2105, 2148), 'tkinter.Label', 'tk.Label', (['self.value_config_frame'], {'width': '(20)'}), '(self.value_config_frame, width=20)\n', (2113, 2148), True, 'import tkinter as tk\n'), ((2258, 2301), 'tkinter.Label', 'tk.Label', (['self.value_config_frame'], {'width': '(40)'}), '(self.value_config_frame, width=40)\n', (2266, 2301), True, 'import tkinter as tk\n'), ((2415, 2458), 'tkinter.Label', 'tk.Label', (['self.value_config_frame'], {'width': '(10)'}), '(self.value_config_frame, width=10)\n', (2423, 2458), True, 'import tkinter as tk\n'), ((2560, 2603), 'tkinter.Label', 'tk.Label', (['self.value_config_frame'], {'width': '(10)'}), '(self.value_config_frame, width=10)\n', (2568, 2603), True, 'import tkinter as tk\n'), ((4299, 4319), 'tkinter.Button', 'tk.Button', (['self.root'], {}), '(self.root)\n', (4308, 4319), True, 'import tkinter as tk\n'), ((913, 950), 'tkinter.Entry', 'tk.Entry', (['self.config_frame'], {'width': '(20)'}), '(self.config_frame, width=20)\n', (921, 950), True, 'import tkinter as tk\n'), ((1233, 1270), 'tkinter.Entry', 'tk.Entry', (['self.config_frame'], {'width': '(40)'}), '(self.config_frame, width=40)\n', (1241, 1270), True, 'import tkinter as tk\n'), ((1550, 1598), 'tkinter.Button', 'tk.Button', (['self.config_frame'], {'text': '"""ON"""', 'width': '(5)'}), "(self.config_frame, text='ON', width=5)\n", (1559, 1598), True, 'import tkinter as tk\n'), ((2785, 2828), 'tkinter.Entry', 'tk.Entry', (['self.value_config_frame'], {'width': '(20)'}), '(self.value_config_frame, width=20)\n', (2793, 2828), True, 'import tkinter as tk\n'), ((3115, 3158), 'tkinter.Entry', 'tk.Entry', (['self.value_config_frame'], {'width': '(40)'}), '(self.value_config_frame, width=40)\n', (3123, 3158), True, 'import tkinter as tk\n'), ((3444, 3487), 'tkinter.Entry', 'tk.Entry', (['self.value_config_frame'], {'width': '(10)'}), '(self.value_config_frame, width=10)\n', (3452, 3487), True, 'import tkinter as tk\n'), ((3766, 3820), 'tkinter.Button', 'tk.Button', (['self.value_config_frame'], {'text': '"""ON"""', 'width': '(5)'}), "(self.value_config_frame, text='ON', width=5)\n", (3775, 3820), True, 'import tkinter as tk\n')]