# main.py
from functions import *
from funct_inlay import *
from funct_wp import *
from funct_grp import *
from funct_clp import *
from funct_gen import *

import tkinter as tk
from tkinter import ttk
import yaml
import shutil

folder_parsets = "cfg_parsets"
folder_pictures = "cfg_picture"
folder_output = "out"
file_def = "def.yml"
file_jobs = "jobs.yml"
name_newparset = "<<new par set>>"
name_newjob = "<<new job>>"
pic_sizelimit = 200


def setup_group(frame, group_name, data, input_vars, selected_params, group_widgets):
    group_data = data['groups'][group_name]
    picture_name = group_data['cfg']['picture_name']  # Picture name defined in def.yml
    picture_path = os.path.join(folder_pictures, picture_name)  # Full path to the picture


    # Dropdown for parameter sets
    param_values = get_existing_parameters(group_name, folder_parsets)
    param_values.append(name_newparset)
    param_var = selected_params[group_name]  # Bind the dropdown to the corresponding selected_param entry
    param_dropdown = ttk.Combobox(frame, textvariable=param_var, values=param_values)
    group_widgets[group_name] = {"param_dropdown": param_dropdown}  # Save for later reference
    source_path = f"{folder_pictures}/{data['general']['notavailablepic_name']}"

    # Handle missing picture case
    if not os.path.exists(picture_path):
        shutil.copy(source_path, picture_path)


    # Function to update the picture based on inputs or use the init picture
    def update_inlay_picture():
        grpnm = group_name
        all_group_vars = {group_name: {key: var.get() for key, var in group_vars.items()} for group_name, group_vars in
                          input_vars.items()}

        # Use the source_path if the selected parameter set is the new parameter set placeholder
        # print(param_dropdown.get())
        if param_dropdown.get() == name_newparset:
            picture_to_use = source_path
        else:
            picture_to_use = picture_path
            if grpnm == "inlay":
                generate_picture_inlay(all_group_vars, picture_to_use)
            elif grpnm == "wp":
                generate_picture_wp(all_group_vars, picture_to_use)
            elif grpnm == "grp":
                generate_picture_grp(all_group_vars, picture_to_use)
            elif grpnm == "clp":
                generate_picture_clp(all_group_vars, picture_to_use)

        group_image_tk = load_image(picture_to_use, pic_sizelimit)
        image_label.configure(image=group_image_tk)
        image_label.image = group_image_tk  # Keep a reference to avoid garbage collection


    # Function to load a parameter set
    def load_selected_param():
        selected_param = param_dropdown.get()
        if selected_param != name_newparset:
            param_data = load_yaml(group_name, selected_param, folder_parsets)
            for key, value in param_data.items():
                input_vars[group_name][key].set(value)
        update_inlay_picture()

    # Function to save the current parameter set
    def save_current_param():
        param_name = param_dropdown.get()
        if param_name not in param_values and param_name.strip():
            param_values.insert(-1, param_name)  # Insert before '<<new parameter set>>'
            param_dropdown['values'] = param_values
            param_dropdown.set(param_name)
        if name_newparset not in param_values:
            param_values.append(name_newparset)  # Ensure it's only appended once
        param_data = {key: var.get() for key, var in input_vars[group_name].items()}
        save_yaml(group_name, param_name, param_data, folder_parsets)
        update_inlay_picture()

    # Function to delete the current parameter set
    def delete_current_param():
        param_name = param_dropdown.get()
        if param_name in param_values and param_name != name_newparset:
            confirm = messagebox.askyesno("Delete Parameter", f"Are you sure you want to delete '{param_name}'?")
            if confirm:
                delete_yaml(group_name, param_name, folder_parsets)
                param_values.remove(param_name)
                if name_newparset not in param_values:
                    param_values.append(name_newparset)  # Ensure it's only appended once

                # If there are any parameter sets left, select the next one; otherwise, show name_newparset
                if param_values and param_values[0] != name_newparset:
                    param_dropdown['values'] = param_values
                    param_dropdown.set(param_values[0])  # Select the first remaining parameter set
                    load_selected_param()  # Load the values of the first remaining parameter set
                else:
                    # If no parameter sets left, show name_newparset
                    param_dropdown['values'] = [name_newparset]
                    param_dropdown.set(name_newparset)

                    # Clear the input fields when no parameter sets are left
                    for key in input_vars[group_name]:
                        input_vars[group_name][key].set('')

    # Detect and save a new parameter set if entered via dropdown (press Enter)
    def on_enter_new_param(event):
        selected_param = param_dropdown.get()
        if selected_param not in param_values and selected_param.strip():
            param_values.insert(-1, selected_param)
            param_dropdown['values'] = param_values
            save_current_param()
            param_dropdown.set(selected_param)
            if name_newparset not in param_values:
                param_values.append(name_newparset)  # Ensure it's only appended once
            update_inlay_picture()

    # Load and display the specific group picture
    group_image_tk = load_image(source_path, pic_sizelimit)
    image_label = ttk.Label(frame, image=group_image_tk)
    image_label.image = group_image_tk  # Keep a reference to avoid garbage collection
    image_label.grid(row=0, column=0, columnspan=2, padx=10, pady=10, sticky="")  # Center the image in the middle
    frame.grid_rowconfigure(0, minsize=230)

    # Add horizontal line
    ttk.Separator(frame, orient='horizontal').grid(row=1, column=0, columnspan=2, sticky="ew", pady=10)

    # Dropdown field and buttons for save/load/delete
    ttk.Label(frame, text="Parameter Set:").grid(row=2, column=0, padx=5, pady=5, sticky="w")
    param_dropdown.grid(row=2, column=1, padx=5, pady=5)
    # ttk.Button(frame, text="Load", command=load_selected_param).grid(row=3, column=0, padx=5, pady=5, sticky="w")
    ttk.Button(frame, text="Save", command=save_current_param).grid(row=3, column=0, padx=5, pady=5, sticky="w")
    ttk.Button(frame, text="Delete", command=delete_current_param).grid(row=3, column=1, padx=5, pady=5, sticky="w")

    # Add horizontal line
    ttk.Separator(frame, orient='horizontal').grid(row=4, column=0, columnspan=2, sticky="ew", pady=10)

    # Setup input fields below dropdown and buttons
    row = 5
    group_vars = {}
    for var_name, _ in group_data['parameter'].items():
        ttk.Label(frame, text=var_name).grid(row=row, column=0, padx=5, pady=5, sticky="e")
        var = tk.StringVar()
        group_vars[var_name] = var
        ttk.Entry(frame, textvariable=var).grid(row=row, column=1, padx=5, pady=5, sticky="w")
        row += 1
    input_vars[group_name] = group_vars

    # Automatically select and load the first parameter set
    if param_values and param_values[0] != name_newparset:
        param_var.set(param_values[0])
        load_selected_param()  # Directly load the first parameter set
    else:
        param_var.set(name_newparset)  # If no parameters available, set to default

    # Bind the dropdown selection event
    param_dropdown.bind("<<ComboboxSelected>>", lambda event: load_selected_param())
    # param_dropdown.event_generate("<<ComboboxSelected>>")  # Trigger the ComboboxSelected

    # Bind the Enter key to detect when a new parameter set is typed and saved
    param_dropdown.bind("<Return>", on_enter_new_param)

    # Automatically generate and display the picture at startup
    update_inlay_picture()



def setup_jobs(frame, data, selected_job, selected_params, input_vars, group_widgets):
    # Load existing jobs from the jobs.yml file
    job_values = get_existing_parameters("jobs", folder_parsets)
    job_values.append(name_newjob)

    # Function to load selected job and update each group
    def load_selected_job():
        job_name = selected_job.get()
        if job_name != name_newjob:
            job_data = load_yaml("jobs", job_name, folder_parsets)
            # Update the parameter sets for each group according to the job
            for group_name, param_set in job_data.items():
                if group_name in selected_params:
                    # Check if the parameter set exists for the group
                    param_values = get_existing_parameters(group_name, folder_parsets)
                    if param_set not in param_values:
                        # If parameter set is missing, set to <<new par set>> and clear fields
                        selected_params[group_name].set(name_newparset)
                        clear_input_fields(group_name)
                    else:
                        selected_params[group_name].set(param_set)
                        # Manually trigger the parameter dropdown change for the group
                        param_dropdown = group_widgets[group_name]["param_dropdown"]
                        param_dropdown.set(param_set)  # Update dropdown to show selected set
                        param_dropdown.event_generate("<<ComboboxSelected>>")

    # Function to clear the input fields when a parameter set is missing
    def clear_input_fields(group_name):
        for key in input_vars[group_name]:
            input_vars[group_name][key].set('')  # Clear the input fields

    # Function to load the selected parameter set for a group and update inputs
    def load_selected_param_for_group(group_name, param_set):
        try:
            param_data = load_yaml(group_name, param_set, folder_parsets)
            for key, value in param_data.items():
                input_vars[group_name][key].set(value)
            # Trigger the picture update by generating the ComboboxSelected event
            param_dropdown = group_widgets[group_name]["param_dropdown"]
            param_dropdown.event_generate("<<ComboboxSelected>>")
        except FileNotFoundError:
            # Handle missing parameter set gracefully
            selected_params[group_name].set(name_newparset)
            clear_input_fields(group_name)

    # Function to save the current job
    def save_current_job():
        job_name = selected_job.get()
        if job_name not in job_values and job_name.strip():
            job_values.insert(-1, job_name)  # Insert before '<<new job>>'
            selected_job.set(job_name)
        job_data = {group_name: selected_params[group_name].get() for group_name in data['groups']}
        save_yaml("jobs", job_name, job_data, folder_parsets)

        # Update job dropdown after saving a new job
        job_dropdown['values'] = job_values
        job_dropdown.set(job_name)

    # Function to delete the selected job
    def delete_current_job():
        job_name = selected_job.get()
        if job_name in job_values and job_name != name_newjob:
            confirm = messagebox.askyesno("Delete Job", f"Are you sure you want to delete '{job_name}'?")
            if confirm:
                delete_yaml("jobs", job_name, folder_parsets)
                job_values.remove(job_name)
                selected_job.set(job_values[0] if job_values else name_newjob)
                # Update job dropdown after deletion
                job_dropdown['values'] = job_values
                load_selected_job()

    # Job dropdown and buttons for save/load/delete
    ttk.Label(frame, text="Job:").grid(row=0, column=0, padx=5, pady=5, sticky="w")
    job_dropdown = ttk.Combobox(frame, textvariable=selected_job, values=job_values)
    job_dropdown.grid(row=0, column=1, padx=5, pady=5)
    ttk.Button(frame, text="Save", command=save_current_job).grid(row=1, column=0, padx=5, pady=5, sticky="w")
    ttk.Button(frame, text="Delete", command=delete_current_job).grid(row=1, column=1, padx=5, pady=5, sticky="w")

    # Bind job selection to loading the selected job and updating the groups
    job_dropdown.bind("<<ComboboxSelected>>", lambda event: load_selected_job())
    job_dropdown.bind("<Return>", lambda event: save_current_job())

    # After setting up all UI components
    if job_values and job_values[0] != name_newjob:
        selected_job.set(job_values[0])  # Select the first job automatically
        load_selected_job()  # Load the first job's parameters and update the corresponding parameter sets

    return load_selected_job



def setup_evaluation(root, folder_pictures, data, folder_output, input_vars, selected_params):
    # Create a frame for the EVALUATION section
    frame = ttk.LabelFrame(root, text="EVALUATION", padding=(10, 10))
    frame.grid(row=2, column=0, columnspan=5, padx=10, pady=10, sticky="nsew")

    # Load and display the specific initial picture
    picture_path = f"{folder_pictures}/{data['general']['generatorpic_name']}"
    group_image_tk = load_image(picture_path, 200)  # Assuming a function load_image exists and works as expected
    image_label = ttk.Label(frame, image=group_image_tk)
    image_label.image = group_image_tk  # Keep a reference to avoid garbage collection
    image_label.grid(row=0, column=0, rowspan=2, padx=10, pady=10)

    # Function to generate configuration file based on current parameters
    def btn_gen():
        generate_config_spf(folder_output, input_vars, selected_params)
    def btn_gen_custom1():
        generate_config_spf_custom1(folder_output, input_vars, selected_params)
    def btn_gen_custom2():
        generate_config_spf_custom1(folder_output, input_vars, selected_params, filetransfer=True)

    # Place buttons in the vertical frame for operations
    button_frame = ttk.Frame(frame)
    button_frame.grid(row=0, column=1, padx=10, pady=10, sticky="n")
    button1 = ttk.Button(button_frame, text="GEN CFG_DMUAUT.SPF FILE", width=45, command=btn_gen)
    button1.grid(row=0, column=0, padx=10, pady=5)
    button2 = ttk.Button(button_frame, text="GEN CFG_INLAY.SPF FILE", width=45, command=btn_gen_custom1)
    button2.grid(row=1, column=0, padx=5, pady=5)
    button3 = ttk.Button(button_frame, text="GEN+TRANSFER CFG_INLAY.SPF FILE", width=45, command=btn_gen_custom2)
    button3.grid(row=1, column=1, padx=5, pady=5)

    root.grid_columnconfigure(0, weight=1)




def main():
    # Tkinter root setup
    root = tk.Tk()
    root.title("DMU50")

    # Load object definitions from YAML
    with open(file_def, 'r') as file:
        data = yaml.safe_load(file)

    input_vars = {}
    selected_params = {}  # Dictionary to track selected parameters for each group
    selected_job = tk.StringVar()
    group_widgets = {}  # Dictionary to store widgets related to groups

    def setup_ui():
        # ---- Setup Jobs UI
        frame = ttk.LabelFrame(root, text="Jobs", padding=(10, 10))
        frame.grid(row=0, column=0, columnspan=5, padx=10, pady=10, sticky="nsew")
        load_selected_job = setup_jobs(frame, data, selected_job, selected_params, input_vars, group_widgets)

        # ---- Create the UI for each group
        for col_idx, group_name in enumerate(data['groups']):
            frame_group = ttk.LabelFrame(root, text=group_name, padding=(10, 10))
            frame_group.grid(row=1, column=col_idx, padx=10, pady=10, sticky="nsew")

            selected_params[group_name] = tk.StringVar()
            setup_group(frame_group, group_name, data, input_vars, selected_params, group_widgets)

            root.grid_columnconfigure(list(range(col_idx + 1)), weight=1)
            root.grid_rowconfigure(0, weight=1)

        # ---- Setup the EVALUATION section
        setup_evaluation(root, folder_pictures, data, folder_output, input_vars, selected_params)

        # After the UI is fully set up, load the selected job to update parameter lists
        load_selected_job()  # Ensure this is called to load the job parameters

        # Show the window after setup is complete
        root.deiconify()

    # Initially hide the root window
    root.withdraw()

    # Delay UI setup by 1000 milliseconds (1 second)
    root.after(300, setup_ui)

    # Start the Tkinter event loop
    root.mainloop()


if __name__ == "__main__":
    main()

