import customtkinter
import tkinter as tk
from tkinter import filedialog
import SvgCreatorModel

class App(customtkinter.CTk):
    def __init__(self):
        super().__init__()

        self.title("Exporter")
        self.geometry("377x170")
        customtkinter.set_default_color_theme("dark-blue")

        self.entry_svg_folder = customtkinter.CTkEntry(self, width=350, placeholder_text="SVG-Ordner Pfad")
        self.entry_svg_folder.grid(row=0, column=0, pady=10)
        self.button_svg_folder = customtkinter.CTkButton(self, text="...", command=self.button_find_svg, width=10)
        self.button_svg_folder.grid(row=0, column=1)

        self.entry_excel_file = customtkinter.CTkEntry(self, width=350, placeholder_text="EXCEL-Datei Pfad")
        self.entry_excel_file.grid(row=1, column=0)
        self.button_excel_file = customtkinter.CTkButton(self, text="...", width=10, command=self.button_find_excel)
        self.button_excel_file.grid(row=1, column=1)

        self.entry_output = customtkinter.CTkEntry(self, width=350, placeholder_text="OUTPUT-Ordner Pfad")
        self.entry_output.grid(row=2, column=0)
        self.button_output = customtkinter.CTkButton(self, text="...", width=10, command=self.button_find_output)
        self.button_output.grid(row=2, column=1, pady=10)

        self.button_run = customtkinter.CTkButton(self, text="start", width=100, state="disabled", command=self.run)
        self.button_run.grid(row=3, column=0)

    def button_find_svg(self):
        file_path = filedialog.askdirectory(title="Select a folder")
        if file_path:
            self.entry_svg_folder.delete(0, tk.END)
            self.entry_svg_folder.insert(0, file_path)
            self.check_inputs()

    def button_find_output(self):
        file_path = filedialog.askdirectory(title="Select a folder")
        if file_path:
            self.entry_output.delete(0, tk.END)
            self.entry_output.insert(0, file_path)
            self.check_inputs()

    def button_find_excel(self):
        file_path = filedialog.askopenfilename(title="Select a file", filetypes=[("Excel Files", "*.xlsx *.xls")])
        if file_path:
            self.entry_excel_file.delete(0, tk.END)
            self.entry_excel_file.insert(0, file_path)
            self.check_inputs()

    def check_inputs(self):
        if all([
            self.entry_output.get(),
            self.entry_excel_file.get(),
            self.entry_svg_folder.get()
        ]):
            self.button_run.configure(state="normal")

    def run(self):
        obj = SvgCreatorModel.SvgCreatorModel(self.entry_svg_folder.get(), self.entry_excel_file.get(), self.entry_output.get())
        self.withdraw()  # Hide the main window
        try:
            obj.extract_excel()
        except Exception as e:
            pass

        self.deiconify()  # Show the main window


if __name__ == "__main__":
    app = App()
    app.mainloop()
