"""Healing-Seite: Presets oben, vier Tabs mit Regelkarten darunter.

Die Vocations-Presets setzen Schwellen und Aktivierung in einem Klick;
danach werden die Tabs neu aufgebaut, damit alle Werte sichtbar stimmen.
"""
import customtkinter

from src.ui import theme

from .foodTab import FoodTab
from .highPriorityTab import HighPriorityTab
from .potionsTab import PotionsTab
from .spellsTab import SpellsTab

PRESETS = (
    ('ritter', 'Ritter', 'GHP 65 % · Exura 70 % · Gran 45 %'),
    ('paladin', 'Paladin', 'GHP 70 % · Exura 60 % · Gran 40 %'),
    ('magier', 'Magier', 'UHP 55 % · Mana 30 % · Exura 65 %'),
)


class HealingPage(customtkinter.CTkFrame):
    def __init__(self, master, context):
        super().__init__(master, fg_color='transparent')
        self.context = context

        presetBar = customtkinter.CTkFrame(self, fg_color='transparent')
        presetBar.pack(fill='x', pady=(0, 8))
        customtkinter.CTkLabel(
            presetBar, text='Vorlage:', font=theme.font(12, 'bold'),
            text_color=theme.MUTED,
        ).pack(side='left', padx=(2, 8))
        for key, label, hint in PRESETS:
            button = customtkinter.CTkButton(
                presetBar, text=f'{label}  ·  {hint}', height=32,
                corner_radius=8, font=theme.font(11),
                fg_color=theme.ELEVATED, hover_color=theme.ACCENT_SOFT,
                text_color=theme.TEXT,
                command=lambda k=key: self._applyPreset(k),
            )
            button.pack(side='left', padx=3)
        syncButton = customtkinter.CTkButton(
            presetBar, text='Hotkeys syncen', height=32,
            corner_radius=8, font=theme.font(11, 'bold'),
            fg_color=theme.ACCENT, hover_color=theme.ACCENT_HOVER,
            text_color=theme.TEXT,
            command=self._syncHotkeys,
        )
        syncButton.pack(side='left', padx=(12, 3))
        self.presetStatus = customtkinter.CTkLabel(
            presetBar, text='', font=theme.font(11),
            text_color=theme.SUCCESS,
        )
        self.presetStatus.pack(side='left', padx=8)

        self.tabControl = customtkinter.CTkTabview(
            self, fg_color=theme.BG,
            segmented_button_selected_color=theme.ACCENT,
            segmented_button_selected_hover_color=theme.ACCENT_HOVER,
            segmented_button_unselected_hover_color=theme.ACCENT_HOVER)
        self.tabControl.pack(expand=1, fill='both')
        for name in ('High Priority', 'Potions', 'Spells', 'Food'):
            self.tabControl.add(name)
        self._buildTabs()

    def _buildTabs(self):
        builders = (
            ('High Priority', HighPriorityTab),
            ('Potions', PotionsTab),
            ('Spells', SpellsTab),
            ('Food', FoodTab),
        )
        for name, tabClass in builders:
            tabFrame = self.tabControl.tab(name)
            for child in tabFrame.winfo_children():
                child.destroy()
            tabClass(tabFrame, self.context).pack(expand=1, fill='both')

    def _applyPreset(self, key):
        self.context.applyHealingPreset(key)
        self._buildTabs()
        label = next(label for k, label, _h in PRESETS if k == key)
        self.presetStatus.configure(text=f'{label} übernommen.')
        self.after(2500, lambda: self.presetStatus.configure(text=''))

    def _syncHotkeys(self):
        ok, message = self.context.syncActionBarHotkeys()
        self._buildTabs()
        self.presetStatus.configure(
            text=message,
            text_color=theme.SUCCESS if ok else theme.DANGER,
        )
        clearAfter = 6000 if ok else 4000
        self.after(clearAfter, lambda: self.presetStatus.configure(
            text='', text_color=theme.SUCCESS))
