import tkinter as tk
from tkinter import ttk, messagebox
from PIL import Image, ImageTk
import basic.basic_process as bp
import basic.image_enhance as ie
import basic.image_split as isp
import basic.image_sharp as ish
import basic.image_smooth as ism
import basic.digital_morphology as dm
import basic.noise_filter as nf
import advanced.face.face_recog as fr
import advanced.license.license_recog as gui
import advanced.track.animal_track as at
import os

# 创建按钮的辅助函数
def create_button(parent, text, command):
    return tk.Button(parent, text=text, bg='#add8e6', fg='black', font=('Helvetica', 14), width=20, command=command)

# 创建主窗口
def create_main_window():
    root = tk.Tk()
    root.title("数字图像处理项目")
    root.geometry("800x600")

    # 设置背景图片
    background_image = Image.open("background.jpg")
    background_image = background_image.resize((1400, 800), Image.LANCZOS)
    bg_image_tk = ImageTk.PhotoImage(background_image)

    # 显示背景图片
    background_label = tk.Label(root, image=bg_image_tk)
    background_label.place(x=0, y=0, relwidth=1, relheight=1)

    # 按钮框架
    frame_buttons = tk.Frame(root)
    frame_buttons.place(relx=0.5, rely=0.6, anchor=tk.CENTER)

    # 开始按钮
    start_button = create_button(frame_buttons, "开始", lambda: open_new_window(root, "main"))
    start_button.pack()

    root.mainloop()

# 打开新窗口
def open_new_window(current_window, window_type):
    current_window.withdraw()

    new_window = tk.Toplevel(current_window)
    new_window.geometry("800x600")

    if window_type == "main":
        new_window.title("数字图像处理项目")

        frame_buttons = tk.Frame(new_window)
        frame_buttons.place(relx=0.5, rely=0.5, anchor=tk.CENTER)

        # 基础功能按钮
        basic_button = create_button(frame_buttons, "基础功能", lambda: open_new_window(new_window, "basic"))
        basic_button.pack(pady=10)

        # 拓展功能按钮
        advanced_button = create_button(frame_buttons, "拓展功能", lambda: open_new_window(new_window, "advanced"))
        advanced_button.pack(pady=10)

    elif window_type == "basic":
        new_window.title("基础功能")

        frame_buttons = tk.Frame(new_window)
        frame_buttons.place(relx=0.5, rely=0.5, anchor=tk.CENTER)

        # 基础功能按钮
        button_names = ["基本处理", "图像增强", "图像分割", "图像平滑", "图像锐化", "数字形态", "图像恢复"]
        for name in button_names:
            button = create_button(frame_buttons, name, lambda name=name: open_new_window(new_window, name))
            button.pack(pady=5)

    elif window_type == "基本处理":
        new_window.title("基本处理")

        frame_buttons = tk.Frame(new_window)
        frame_buttons.place(relx=0.5, rely=0.5, anchor=tk.CENTER)

        # 基本处理按钮
        button_names = ["选择图片", "仿射变换", "翻转", "算术变换"]
        functions = [bp.select_image, bp.affine_tra, bp.turn, bp.arithmetic]

        for name, func in zip(button_names, functions):
            button = create_button(frame_buttons, name, lambda func=func: func(new_window))
            button.pack(pady=5)

    elif window_type == "图像增强":
        new_window.title("图像增强")

        frame_buttons = tk.Frame(new_window)
        frame_buttons.place(relx=0.5, rely=0.5, anchor=tk.CENTER)

        # 图像增强按钮
        button_names = ["选择图片", "线性变换", "对数变换", "直方图均衡化", "直方图正规化"]
        functions = [ie.select_image, ie.line_tra, ie.log_tra, ie.equal, ie.regul]

        for name, func in zip(button_names, functions):
            button = create_button(frame_buttons, name, lambda func=func: func(new_window))
            button.pack(pady=5)

    elif window_type == "图像分割":
        new_window.title("图像分割")

        frame_buttons = tk.Frame(new_window)
        frame_buttons.place(relx=0.5, rely=0.5, anchor=tk.CENTER)

        # 图像分割按钮
        button_names = ["选择图片", "阈值化", "边缘检测", "线条变化检测"]
        functions = [isp.select_image, isp.threshold, isp.verge, isp.line_chan]

        for name, func in zip(button_names, functions):
            button = create_button(frame_buttons, name, lambda func=func: func(new_window))
            button.pack(pady=5)

    elif window_type == "图像平滑":
        new_window.title("图像平滑")

        frame_buttons = tk.Frame(new_window)
        frame_buttons.place(relx=0.5, rely=0.5, anchor=tk.CENTER)

        # 图像平滑按钮
        button_names = ["选择图片", "频域平滑", "空域平滑"]
        functions = [ism.select_image, ism.freq_smo, ism.air_smo]

        for name, func in zip(button_names, functions):
            button = create_button(frame_buttons, name, lambda func=func: func(new_window))
            button.pack(pady=5)

    elif window_type == "图像锐化":
        new_window.title("图像锐化")

        frame_buttons = tk.Frame(new_window)
        frame_buttons.place(relx=0.5, rely=0.5, anchor=tk.CENTER)

        # 图像锐化按钮
        button_names = ["选择图片", "频域锐化", "空域锐化"]
        functions = [ish.select_image, ish.freq_shar, ish.air_shar]

        for name, func in zip(button_names, functions):
            button = create_button(frame_buttons, name, lambda func=func: func(new_window))
            button.pack(pady=5)

    elif window_type == "数字形态":
        new_window.title("数字形态")

        frame_buttons = tk.Frame(new_window)
        frame_buttons.place(relx=0.5, rely=0.5, anchor=tk.CENTER)

        # 数字形态按钮
        button_names = ["选择图片", "腐蚀", "膨胀", "开运算", "闭运算"]
        functions = [dm.select_image, dm.corrode, dm.dilatation, dm.open_op, dm.close_op]

        for name, func in zip(button_names, functions):
            button = create_button(frame_buttons, name, lambda func=func: func(new_window))
            button.pack(pady=5)

    elif window_type == "图像恢复":
        new_window.title("图像恢复")

        frame_buttons = tk.Frame(new_window)
        frame_buttons.place(relx=0.5, rely=0.5, anchor=tk.CENTER)

        # 图像恢复按钮
        button_names = ["选择图片", "几何均值滤波", "排序统计类滤波", "选择性滤波"]
        functions = [nf.select_image, nf.geo_mean_filter, nf.sort_stat_filter, nf.selective_filter]

        for name, func in zip(button_names, functions):
            button = create_button(frame_buttons, name, lambda func=func: func(new_window))
            button.pack(pady=5)

    elif window_type == "advanced":
        new_window.title("拓展功能")

        frame_buttons = tk.Frame(new_window)
        frame_buttons.place(relx=0.5, rely=0.5, anchor=tk.CENTER)

        # 拓展功能按钮
        button_names = ["人脸识别", "车牌识别", "动物追踪"]
        for name in button_names:
            button = create_button(frame_buttons, name, lambda name=name: open_new_window(new_window, name))
            button.pack(pady=5)

    elif window_type == "人脸识别":
        new_window.title("人脸识别")

        frame_buttons = tk.Frame(new_window)
        frame_buttons.place(relx=0.5, rely=0.5, anchor=tk.CENTER)

        # 人脸识别按钮
        button_names = ["选择图片", "识别"]
        functions = [fr.select_image, fr.face_recog]

        for name, func in zip(button_names, functions):
            button = create_button(frame_buttons, name, lambda func=func: func(new_window))
            button.pack(pady=5)

    elif window_type == "车牌识别":
        new_window.title("车牌识别")

        frame_buttons = tk.Frame(new_window)
        frame_buttons.place(relx=0.5, rely=0.5, anchor=tk.CENTER)

        # 车牌识别按钮
        button_names = ["选择图片", "识别"]
        img_container = {'image': None, 'file_path': None}  # 添加文件路径存储
        functions = [gui.select_image, gui.detect_and_show_processed_image]

        for name, func in zip(button_names, functions):
            button = create_button(frame_buttons, name, lambda func=func: func(new_window, img_container))
            button.pack(pady=5)

    elif window_type == "动物追踪":
        new_window.title("动物追踪")

        frame_buttons = tk.Frame(new_window)
        frame_buttons.pack(expand=True, pady=50)  # 使用 expand=True 让框架在垂直方向上扩展，并添加垂直方向上的间距

        # 动物追踪按钮
        button = tk.Button(frame_buttons, text="选择视频并追踪", bg='#add8e6', fg='black', font=('Helvetica', 14),
                           width=20, command=at.select_file_and_run)
        button.pack(pady=5)

    # 返回按钮
    return_button = create_button(new_window, "返回", lambda: return_to_main_window(current_window, new_window))
    return_button.pack(pady=10, side=tk.BOTTOM)

    # 处理窗口关闭事件
    new_window.protocol("WM_DELETE_WINDOW", lambda: on_close(new_window))

# 返回主窗口
def return_to_main_window(current_window, new_window=None):
    if new_window and new_window.winfo_exists():
        new_window.destroy()
    current_window.deiconify()

# 关闭窗口事件处理
def on_close(root):
    if tk.messagebox.askokcancel("退出", "确定要退出程序吗？"):
        root.quit()

if __name__ == "__main__":
    create_main_window()
