# V2.1.1d Beta
from customtkinter import (
    CTk,
    CTkLabel,
    CTkOptionMenu,
    CTkImage,
    CTkFont
)
# =============== Custom Widgets ===============
class OptionMenu(CTkOptionMenu):
    def __init__(self, master: any,
        width: int = 140,
        height: int = 28,
        corner_radius: int | None = None,
        bg_color: str | tuple[str, str] = "transparent",
        fg_color: str | tuple[str, str] | None = None,
        button_color: str | tuple[str, str] | None = None,
        button_hover_color: str | tuple[str, str] | None = None,
        text_color: str | tuple[str, str] | None = None,
        text_color_disabled: str | tuple[str, str] | None = None,
        dropdown_fg_color: str | tuple[str, str] | None = None,
        dropdown_hover_color: str | tuple[str, str] | None = None,
        dropdown_text_color: str | tuple[str, str] | None = None,
        font: tuple | CTkFont | None = None,
        dropdown_font: tuple | CTkFont | None = None,
        values: list | None = None, variable: CTk | None = None,
        state: str = "normal",
        hover: bool = True, command: callable = None,
        dynamic_resizing: bool = True,
        anchor: str = "w",
        **kwargs
        ):

        """
        Option Menu With Custom Icon for `customtkinter`.
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        Args:
            Same as class `customtkinter.CTkOptionMenu`
        
        Methods:
            - setDropdownIcon(image: CTkImage, right_dictance: int = 5) <- Sets the icon to the Option Button
            - Same as class `customtkinter.CTkOptionMenu`

        """

        super().__init__(master, width, height, corner_radius, bg_color, fg_color, button_color, button_hover_color, text_color, text_color_disabled, dropdown_fg_color, dropdown_hover_color, dropdown_text_color, font, dropdown_font, values, variable, state, hover, command, dynamic_resizing, anchor, **kwargs)

        # Setting icon image reference
        self.image_label: CTkLabel | None = None

    def setDropdownIcon(self, image: CTkImage, right_dictance: int = 5):
        """Sets the icon or image to the dropdown menu."""

        # Creating image label to be placed on "internal" Canvas.
        self.image_label = CTkLabel(self, text="", image=image)

        # Deleting existing drawing
        self._canvas.delete("dropdown_arrow")

        # Getting current background color
        color = self._canvas.itemcget("inner_parts_right", "fill")
        self.image_label.configure(fg_color=color, bg_color=color)

        # Getting information to place the image
        grid_info = self._text_label.grid_info()
        grid_info["padx"], grid_info["sticky"] = right_dictance, "e"
        self.image_label.grid(**grid_info)

        # Adding events for clicks and hovers
        self.image_label.bind("<Button-1>", self._clicked)
        self.image_label.bind("<Enter>", self._on_enter)
        self.image_label.bind("<Leave>", self._on_leave)

    def _on_enter(self, event=0):
        super()._on_enter(event)
        
        if self.image_label:
            if self._state == "normal":
                color = self._apply_appearance_mode(self._button_hover_color)
            else:
                color = self._apply_appearance_mode(self._button_color)
            
            self.image_label.configure(fg_color=color, bg_color=color)

    def _on_leave(self, event=0):
        super()._on_leave(event)

        if self.image_label:
            color = self._apply_appearance_mode(self._button_color)
            self.image_label.configure(fg_color=color, bg_color=color)
# ==============================================