"""Potions-Tab: Health- und Mana-Potion als deklarierte Regelkarten."""
import customtkinter

from src.repositories.actionBar.slotIcons import (
    HEALTH_POTION_IDS, MANA_POTION_IDS, SLOT_ITEMS,
)

from .ruleCard import HealingRuleCard


def _hotkeyField(cfg, setHotkey):
    return {
        'type': 'hotkey', 'name': 'hotkey', 'label': 'Hotkey:',
        'get': lambda context: cfg(context)['hotkey'],
        'set': setHotkey,
    }


def _slotField(cfg, setSlot):
    return {
        'type': 'slot', 'name': 'slot', 'label': 'Slot:',
        'get': lambda context: cfg(context).get('slot') or '',
        'set': setSlot,
    }


def _itemField(cfg, potionKey, itemIds):
    namesById = {itemId: SLOT_ITEMS[itemId] for itemId in itemIds}
    idsByName = {name: itemId for itemId, name in namesById.items()}
    return {
        'type': 'combo', 'name': 'item', 'label': 'Trank:',
        'values': list(namesById.values()),
        'placeholder': 'Trank wählen …',
        'get': lambda context: namesById.get(cfg(context).get('item'), ''),
        'set': lambda context, value: context.setHealthPotionItemByKey(
            potionKey, idsByName.get(value)),
        'subtext': lambda value: 'Wird per „Hotkeys syncen“ automatisch zugeordnet',
    }


def healthPotionSpec(potionKey, title):
    def cfg(context):
        return context.context['healing']['potions'][potionKey]

    return {
        'title': title,
        'toggle': {
            'get': lambda context: cfg(context)['enabled'],
            'set': lambda context, value: context.toggleHealingPotionsByKey(
                potionKey, value),
        },
        'fields': [
            _itemField(cfg, potionKey, HEALTH_POTION_IDS),
            _hotkeyField(cfg, lambda context, key: (
                context.setHealthPotionHotkeyByKey(potionKey, key))),
            _slotField(cfg, lambda context, value: (
                context.setHealthPotionSlotByKey(potionKey, value))),
            {
                'type': 'slider', 'name': 'hp',
                'label': 'HP % less than or equal:',
                'get': lambda context: cfg(
                    context)['hpPercentageLessThanOrEqual'],
                'set': lambda context, value: (
                    context.setHealthPotionHpPercentageLessThanOrEqual(
                        potionKey, value)),
            },
            {
                'type': 'slider', 'name': 'mana',
                'label': 'Mana % greater than or equal:',
                'get': lambda context: cfg(
                    context)['manaPercentageGreaterThanOrEqual'],
                'set': lambda context, value: (
                    context.setHealthPotionManaPercentageGreaterThanOrEqual(
                        potionKey, value)),
            },
        ],
    }


def manaPotionSpec(potionKey, title):
    def cfg(context):
        return context.context['healing']['potions'][potionKey]

    return {
        'title': title,
        'toggle': {
            'get': lambda context: cfg(context)['enabled'],
            'set': lambda context, value: context.toggleHealingPotionsByKey(
                potionKey, value),
        },
        'fields': [
            _itemField(cfg, potionKey, MANA_POTION_IDS),
            _hotkeyField(cfg, lambda context, key: (
                context.setHealthPotionHotkeyByKey(potionKey, key))),
            _slotField(cfg, lambda context, value: (
                context.setHealthPotionSlotByKey(potionKey, value))),
            {
                'type': 'slider', 'name': 'mana',
                'label': 'Mana % less than or equal:',
                'get': lambda context: cfg(
                    context)['manaPercentageLessThanOrEqual'],
                'set': lambda context, value: (
                    context.setHealthPotionManaPercentageLessThanOrEqual(
                        potionKey, value)),
            },
        ],
    }


class PotionsTab(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.healthPotionCard = HealingRuleCard(
            self, context, healthPotionSpec(
                'firstHealthPotion', 'Health potion'))
        self.healthPotionCard.grid(
            column=0, row=0, padx=(10, 5), pady=10, sticky='new')

        self.manaPotionCard = HealingRuleCard(
            self, context, manaPotionSpec('firstManaPotion', 'Mana potion'))
        self.manaPotionCard.grid(
            column=1, row=0, padx=(5, 10), pady=10, sticky='new')
