import logging
import ttkbootstrap as ttk
from ..controller import Controller
from ..geometry import geometry

logger = logging.getLogger(__name__)

class AppearanceTab(ttk.Frame):
    def __init__(self, app):
        self.app = app
        self.c: Controller = self.app.c

        super().__init__(
            self.app.notebook,
            padding=(geometry.whp(4), geometry.whp(4))
        )
        self.create_widgets()

    def create_widgets(self):
        self.columnconfigure(0, weight=0)
        self.columnconfigure(1, weight=1)

        ttk.Label(
            self,
            text="Theme",
        ).grid(row=0, column=0, sticky="w", padx=(0, geometry.whp(6)), pady=10)

        theme_frame = ttk.Frame(self)
        theme_frame.grid(row=0, column=1, sticky="w")

        for col, theme in enumerate(["Light", "Dark", "Slate"]):
            ttk.Radiobutton(
                theme_frame,
                text=theme,
                value=theme.lower(),
                variable=self.c.theme_var,
            ).grid(
                row=0,
                column=col,
                padx=geometry.whp(2),
                sticky="w",
            )

        ttk.Label(
            self,
            text="Font size",
        ).grid(row=1, column=0, sticky="w", padx=(0, geometry.whp(6)), pady=10)

        font_frame = ttk.Frame(self)
        font_frame.grid(row=1, column=1, sticky="w")

        for col, size in enumerate(["Smaller", "Normal", "Larger"]):
            ttk.Radiobutton(
                font_frame,
                text=size,
                value=size.lower(),
                variable=self.c.font_size_var,
            ).grid(
                row=0,
                column=col,
                padx=geometry.whp(2),
                sticky="w",
            )

        ttk.Label(
            self,
            text="Window size",
        ).grid(row=2, column=0, sticky="w", padx=(0, geometry.whp(6)), pady=10)

        window_frame = ttk.Frame(self)
        window_frame.grid(row=2, column=1, sticky="w")

        for col, size in enumerate(["Normal", "Large"]):
            ttk.Radiobutton(
                window_frame,
                text=size,
                value=size.lower(),
                variable=self.c.window_size_var,
            ).grid(
                row=0,
                column=col,
                padx=geometry.whp(2),
                sticky="w",
            )



        buttons_frame = ttk.Frame(self)
        buttons_frame.grid(
            row=6,
            column=0,
            columnspan=2,
            sticky="w",
            pady=(20, 0)
        )


        BTN_WIDTH = 10

        save_btn = ttk.Button(
            buttons_frame,
            text="Save",
            width=BTN_WIDTH,
            padding=(0, geometry.whp(1)),
            command=self.c.save_appearance_settings
        )
        save_btn.grid(row=0, column=0, padx=(0, geometry.whp(4)))

        reset_btn = ttk.Button(
            buttons_frame,
            text="Reset",
            width=BTN_WIDTH,
            padding=(0, geometry.whp(1)),
            bootstyle="outline",
            command=lambda: self.c.apply_settings("appearance"),
        )
        reset_btn.grid(row=0, column=1)

        for i in range(3):
            self.grid_rowconfigure(i, pad=geometry.whp(3))