import logging
import platform
from tkinter import font
import ttkbootstrap as ttk
from .geometry import geometry
from .base_window import BaseWindow
from .tabs import (
    AboutTab,
    AppearanceTab,
    AttributionTab,
    ConvertTab,
    SettingsTab,
    TextboxTab,
)
from .controller import Controller
from .defaults import THEMES, FONT_SIZE_MAP

logger = logging.getLogger(__name__)

if platform.system() == "Windows":
    try:
        import ctypes
        ctypes.windll.shcore.SetProcessDpiAwareness(1)
    except Exception:
        pass

class JimletApp(BaseWindow):

    def __init__(self, title="Jimlet TTS Converter"):
        super().__init__(title)
        self.c: Controller = Controller(self)

        self.set_geometry()
        self.root.resizable(False, False)

        self.init_style()

        self.set_font_size()
        self.set_icon()
        self.create_widgets()

        self.settings_tab.apply_settings()

    def init_style(self):

        try:
            tname = self.c.settings["appearance"]["theme"]
            theme = THEMES[tname]
        except:
            theme = "litera"

        self.style = ttk.Style(theme=theme)


        entry_bg = self.style.lookup("TEntry", "fieldbackground")

        if not entry_bg:
            entry_bg = self.style.lookup("TEntry", "background")

        self.style.configure(
            "EntryLike.TCombobox",
            fieldbackground=entry_bg,
            background=entry_bg,
        )

        self.style.map(
            "EntryLike.TCombobox",
            fieldbackground=[("readonly", entry_bg)],
            background=[("readonly", entry_bg)],
        )


        self.style.configure(
            "Primary.Action.TButton",
            width=10,
            padding=(0, geometry.whp(1)),
        )

        self.style.configure(
            "Outline.Secondary.Action.TButton",
            width=10,
            padding=(0, geometry.whp(1)),
        )

        self.style.configure(
            "Red.TFrame",
            background="red"
        )

    def set_font_size(self):

        try:
            fs_name = self.c.settings["appearance"]["font_size"]
            fs = FONT_SIZE_MAP[fs_name]
        except:
            fs = 13

        font.nametofont("TkDefaultFont").configure(size=fs)
        font.nametofont("TkTextFont").configure(size=fs)
        font.nametofont("TkFixedFont").configure(size=fs)
        font.nametofont("TkMenuFont").configure(size=fs)
        font.nametofont("TkHeadingFont").configure(size=fs)

    def create_widgets(self):

        self.root.columnconfigure(0, weight=1)
        self.root.rowconfigure(0, weight=1)

        self.notebook = ttk.Notebook(self.root)
        self.notebook.grid(row=0, column=0, sticky="nsew")

        self.convert_tab = ConvertTab(self)
        self.settings_tab = SettingsTab(self)
        self.about_tab = AboutTab(self)
        self.attribution_tab = AttributionTab(self)
        self.appearance_tab = AppearanceTab(self)
        self.textbox_tab = TextboxTab(self)

        self.notebook.add(self.convert_tab, text="Convert")
        self.notebook.add(self.settings_tab, text="Settings")
        self.notebook.add(self.textbox_tab, text="Textbox")
        self.notebook.add(self.appearance_tab, text="Appearance")
        self.notebook.add(self.about_tab, text="About")
        self.notebook.add(self.attribution_tab, text="Attribution")


