import customtkinter
import os
from PIL import Image


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

        self.title("image_example.py")
        self.geometry("700x450")

        # set grid layout 1x2
        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(1, weight=1)

        # load images with light and dark mode image
        current_path = os.path.dirname(os.path.realpath(__file__))
        self.home_image = customtkinter.CTkImage(light_image=Image.open(current_path + "/test_images/home_dark.png"),
                                                 dark_image=Image.open(current_path + "/test_images/home_light.png"), size=(20, 20))
        self.chat_image = customtkinter.CTkImage(light_image=Image.open(current_path + "/test_images/chat_dark.png"),
                                                 dark_image=Image.open(current_path + "/test_images/chat_light.png"), size=(20, 20))
        self.add_user_image = customtkinter.CTkImage(light_image=Image.open(current_path + "/test_images/add_user_dark.png"),
                                                     dark_image=Image.open(current_path + "/test_images/add_user_light.png"), size=(20, 20))

        # create navigation frame
        self.navigation_frame = customtkinter.CTkFrame(self, corner_radius=0)
        self.navigation_frame.grid(row=0, column=0, sticky="nsew")
        self.navigation_frame.grid_rowconfigure(4, weight=1)

        self.navigation_frame_label = customtkinter.CTkLabel(self.navigation_frame, text="Image Example",
                                                             font=customtkinter.CTkFont(size=18, weight="bold"))
        self.navigation_frame_label.grid(row=0, column=0, padx=20, pady=20)

        self.home_button = customtkinter.CTkButton(self.navigation_frame, corner_radius=0, height=40, border_spacing=10, text="Home",
                                                   fg_color="transparent", text_color=("gray10", "gray90"), hover_color=("gray70", "gray30"),
                                                   image=self.home_image, anchor="w", command=self.home_button_event)
        self.home_button.grid(row=1, column=0, sticky="ew")
        self.chat_button = customtkinter.CTkButton(self.navigation_frame, corner_radius=0, height=40, border_spacing=10, text="New Chat",
                                                   fg_color="transparent", text_color=("gray10", "gray90"), hover_color=("gray70", "gray30"),
                                                   image=self.chat_image, anchor="w", command=self.chat_button_event)
        self.chat_button.grid(row=2, column=0, sticky="ew")
        self.add_user_button = customtkinter.CTkButton(self.navigation_frame, corner_radius=0, height=40, border_spacing=10, text="Add User",
                                                       fg_color="transparent", text_color=("gray10", "gray90"), hover_color=("gray70", "gray30"),
                                                       image=self.add_user_image, anchor="w", command=self.add_user_button_event)
        self.add_user_button.grid(row=3, column=0, sticky="ew")

        self.appearance_mode_menu = customtkinter.CTkOptionMenu(self.navigation_frame, values=["Light", "Dark", "System"],
                                                                command=self.change_appearance_mode_event)
        self.appearance_mode_menu.grid(row=6, column=0, padx=20, pady=20, sticky="s")

        # create home frame
        self.home_frame = customtkinter.CTkFrame(self, corner_radius=0, fg_color="transparent")
        self.home_frame.grid(row=0, column=1, sticky="nsew")

        # create chat frame
        self.chat_frame = customtkinter.CTkFrame(self, corner_radius=0, fg_color="transparent")
        self.chat_frame.grid(row=0, column=1, sticky="nsew")

        # create add user frame
        self.add_user_frame = customtkinter.CTkFrame(self, corner_radius=0, fg_color="transparent")
        self.add_user_frame.grid(row=0, column=1, sticky="nsew")

    def select_frame_by_name(self, name):
        # set button color for selected button
        self.home_button.configure(fg_color=("gray75", "gray25") if name == "home" else "transparent")
        self.chat_button.configure(fg_color=("gray75", "gray25") if name == "chat" else "transparent")
        self.add_user_button.configure(fg_color=("gray75", "gray25") if name == "add_user" else "transparent")

        # show selected frame
        if name == "home":
            self.home_frame.grid(row=0, column=1, sticky="nsew")
        else:
            self.home_frame.grid_forget()
        if name == "home":
            self.home_frame.grid(row=0, column=1, sticky="nsew")
        else:
            self.home_frame.grid_forget()
        if name == "home":
            self.home_frame.grid(row=0, column=1, sticky="nsew")
        else:
            self.home_frame.grid_forget()
        self.chat_button.configure(fg_color="transparent")
        self.add_user_button.configure(fg_color="transparent")

    def home_button_event(self):
        self.select_frame_by_name("home")

    def chat_button_event(self):
        self.select_frame_by_name("chat")

    def add_user_button_event(self):
        self.select_frame_by_name("add_user")

    def change_appearance_mode_event(self, new_appearance_mode):
        customtkinter.set_appearance_mode(new_appearance_mode)


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

