from pygubu.api.v1 import register_widget, register_custom_property
from pygubu.i18n import _
from pygubu.plugins.tk.tkstdwidgets import TKToplevel as TKToplevelBO
from customtkinter import (
    CTk,
    CTkToplevel,
    set_appearance_mode,
    set_default_color_theme,
)

from ..customtkinter import _designer_tab_label, _plugin_uid
from .ctkbase import CTkBaseMixin


class CTkBO(TKToplevelBO):
    class_ = CTk
    properties = TKToplevelBO.properties + (
        "appearance_mode",
        "color_theme",
        "fg_color",
    )
    ro_properties = TKToplevelBO.ro_properties + ("background", "fg_color")

    def realize(self, parent):
        # Call realize from BuilderObject not Toplevel.
        return super(TKToplevelBO, self).realize(parent)

    def _set_property(self, target_widget, pname, value):
        if pname == "appearance_mode":
            set_appearance_mode(value)
        elif pname == "color_theme":
            set_default_color_theme(value)
        else:
            return super()._set_property(target_widget, pname, value)

    def _code_set_property(self, targetid, pname, value, code_bag):
        if pname == "appearance_mode":
            code_bag[pname] = (f'set_appearance_mode("{value}")',)
        elif pname == "color_theme":
            code_bag[pname] = (f'set_default_color_theme("{value}")',)
        else:
            super()._code_set_property(targetid, pname, value, code_bag)

    def code_imports(self):
        imports = ["CTk"]
        if "appearance_mode" in self.wmeta.properties:
            imports.append("set_appearance_mode")
        if "color_theme" in self.wmeta.properties:
            imports.append("set_default_color_theme")
        return [("customtkinter", name) for name in imports]


_builder_uid = f"{_plugin_uid}.CTk"
register_widget(
    _builder_uid,
    CTkBO,
    "CTk",
    ("ttk", _designer_tab_label),
    group=-1,
)


class CTkToplevelBO(TKToplevelBO):
    class_ = CTkToplevel
    properties = TKToplevelBO.properties + ("fg_color",)
    ro_properties = TKToplevelBO.ro_properties + ("background", "fg_color")

    def realize(self, parent):
        # Call realize from BuilderObject not Toplevel.
        return super(TKToplevelBO, self).realize(parent)

    def code_imports(self):
        return [("customtkinter", self.class_.__name__)]


_builder_uid = f"{_plugin_uid}.CTkToplevel"
register_widget(
    _builder_uid,
    CTkToplevelBO,
    "CTkToplevel",
    ("ttk", _designer_tab_label),
    group=-1,
)

@@ -1894,16 +1894,318 @@
 =-1,%0A)%0A%0A
+_maxsize_help = _(%22Set the maximum window size.%22)%0A_minsize_help = _(%22Set the minimum window size.%22)%0A%0Aregister_custom_property(%0A    _builder_uid,%0A    %22minsize%22,%0A    %22whentry%22,%0A    help=_minsize_help,%0A)%0Aregister_custom_property(%0A    _builder_uid,%0A    %22maxsize%22,%0A    %22whentry%22,%0A    help=_maxsize_help,%0A)%0A%0A
 %0Aclass C
@@ -2770,28 +2770,229 @@
 tab_label),%0A    group=-1,%0A)%0A
+%0Aregister_custom_property(%0A    _builder_uid,%0A    %22minsize%22,%0A    %22whentry%22,%0A    help=_minsize_help,%0A)%0Aregister_custom_property(%0A    _builder_uid,%0A    %22maxsize%22,%0A    %22whentry%22,%0A    help=_maxsize_help,%0A)%0A

Fix minsize, maxsize editors for customtkinter Ctk and Toplevel.