"""Spells-Tab: Heil- und Utura-Zauber als deklarierte Regelkarten."""
import customtkinter

from src.wiki.spells import healingSpells, spells

from .ruleCard import HealingRuleCard

HEALING_SPELL_CHOICES = [s for s in healingSpells if s.startswith('exura')]


def _spellCfg(context, spellKey):
    return context.context['healing']['spells'][spellKey]


def _initialSpell(context, spellKey):
    profile = context.enabledProfile
    if profile is None:
        return None
    return profile['config']['healing']['spells'][spellKey]['spell']


def _manaSubtext(value):
    spell = spells.get(value)
    if spell is None or spell['manaNeeded'] is None:
        return ''
    return f"Kosten: {spell['manaNeeded']} Mana"


def spellSpec(spellKey, title, withSpellChoice):
    fields = []
    if withSpellChoice:
        fields.append({
            'type': 'combo', 'name': 'spell', 'label': 'Spell:',
            'values': HEALING_SPELL_CHOICES,
            'placeholder': 'Zauber suchen …',
            'get': lambda context: _initialSpell(context, spellKey),
            'set': lambda context, value: context.setSpellName(
                spellKey, value),
            'subtext': _manaSubtext,
        })
    fields += [
        {
            'type': 'hotkey', 'name': 'hotkey', 'label': 'Hotkey:',
            'get': lambda context: _spellCfg(context, spellKey)['hotkey'],
            'set': lambda context, key: context.setSpellHotkeyByKey(
                spellKey, key),
        },
        {
            'type': 'slider', 'name': 'hp',
            'label': 'HP % less than or equal:',
            'get': lambda context: _spellCfg(
                context, spellKey)['hpPercentageLessThanOrEqual'],
            'set': lambda context, value: (
                context.setSpellHpPercentageLessThanOrEqual(spellKey, value)),
        },
        {
            'type': 'slider', 'name': 'mana',
            'label': 'Mana % greater than or equal:',
            'get': lambda context: _spellCfg(
                context, spellKey)['manaPercentageGreaterThanOrEqual'],
            'set': lambda context, value: (
                context.setSpellManaPercentageGreaterThanOrEqual(
                    spellKey, value)),
        },
    ]
    return {
        'title': title,
        'toggle': {
            'get': lambda context: _spellCfg(context, spellKey)['enabled'],
            'set': lambda context, value: context.toggleSpellByKey(
                spellKey, value),
        },
        'fields': fields,
    }


class SpellsTab(customtkinter.CTkFrame):
    def __init__(self, parent, context):
        super().__init__(parent, fg_color='transparent')
        self.context = context
        self.grid_columnconfigure((0, 1), weight=1, uniform='cards')

        self.criticalHealingCard = HealingRuleCard(
            self, context, spellSpec(
                'criticalHealing', 'Critical healing', withSpellChoice=True))
        self.criticalHealingCard.grid(
            column=0, row=0, padx=(10, 5), pady=10, sticky='new')

        self.lightHealingCard = HealingRuleCard(
            self, context, spellSpec(
                'lightHealing', 'Light healing', withSpellChoice=True))
        self.lightHealingCard.grid(
            column=1, row=0, padx=(5, 10), pady=10, sticky='new')

        self.mediumHealingCard = HealingRuleCard(
            self, context, spellSpec(
                'mediumHealing', 'Medium healing', withSpellChoice=True))
        self.mediumHealingCard.grid(
            column=0, row=1, padx=(10, 5), pady=(0, 10), sticky='new')

        self.uturaCard = HealingRuleCard(
            self, context, spellSpec('utura', 'Utura', withSpellChoice=False))
        self.uturaCard.grid(
            column=1, row=1, padx=(5, 10), pady=(0, 10), sticky='new')

        self.uturaGranCard = HealingRuleCard(
            self, context, spellSpec(
                'uturaGran', 'Utura Gran', withSpellChoice=False))
        self.uturaGranCard.grid(
            column=0, row=2, padx=(10, 5), pady=(0, 10), sticky='new')
