"""High-Priority-Tab: Notfall-Food und Ring-/Amulett-Wechsel."""
import customtkinter

from .ruleCard import HealingRuleCard


def _highPriorityCfg(context, key):
    return context.context['healing']['highPriority'][key]


def foodSpec(key, title, sliderLabel, sliderConfigKey, setSlider):
    def cfg(context):
        return _highPriorityCfg(context, key)

    return {
        'title': title,
        'toggle': {
            'get': lambda context: cfg(context)['enabled'],
            'set': lambda context, value: (
                context.toggleHealingHighPriorityByKey(key, value)),
        },
        'fields': [
            {
                'type': 'hotkey', 'name': 'hotkey', 'label': 'Hotkey:',
                'get': lambda context: cfg(context)['hotkey'],
                'set': lambda context, hotkey: (
                    context.setHotkeyHealingHighPriorityByKey(key, hotkey)),
            },
            {
                'type': 'slider', 'name': 'percentage', 'label': sliderLabel,
                'get': lambda context: cfg(context)[sliderConfigKey],
                'set': lambda context, value: setSlider(context, value),
            },
        ],
    }


def swapSpec(key, title, itemKeys, methods):
    """Deklaration fuer Ring-/Amulett-Wechsel (Tank- und Main-Variante)."""
    def cfg(context):
        return _highPriorityCfg(context, key)

    tankKey, mainKey = itemKeys
    return {
        'title': title,
        'toggle': {
            'get': lambda context: cfg(context)['enabled'],
            'set': lambda context, value: (
                context.toggleHealingHighPriorityByKey(key, value)),
        },
        'fields': [
            {
                'type': 'hotkey', 'name': 'tankHotkey', 'label': 'Tank Hotkey:',
                'get': lambda context: cfg(context)[tankKey]['hotkey'],
                'set': methods['tankHotkey'],
            },
            {
                'type': 'slot', 'name': 'tankSlot', 'label': 'Tank Slot:',
                'get': lambda context: cfg(context)[tankKey]['slot'],
                'set': methods['tankSlot'],
            },
            {
                'type': 'slider', 'name': 'tankHp',
                'label': 'Tank - HP % less than or equal:',
                'get': lambda context: cfg(
                    context)[tankKey]['hpPercentageLessThanOrEqual'],
                'set': methods['hpLessThanOrEqual'],
            },
            {
                'type': 'hotkey', 'name': 'mainHotkey', 'label': 'Main Hotkey:',
                'get': lambda context: cfg(context)[mainKey]['hotkey'],
                'set': methods['mainHotkey'],
            },
            {
                'type': 'slot', 'name': 'mainSlot', 'label': 'Main Slot:',
                'get': lambda context: cfg(context)[mainKey]['slot'],
                'set': methods['mainSlot'],
            },
            {
                'type': 'slider', 'name': 'mainHp',
                'label': 'Main - HP % greater than:',
                'get': lambda context: cfg(
                    context)[mainKey]['hpPercentageGreaterThan'],
                'set': methods['hpGreaterThan'],
            },
        ],
    }


def swapRingSpec():
    return swapSpec(
        'swapRing', 'Swap ring', ('tankRing', 'mainRing'), {
            'tankHotkey': lambda context, key: (
                context.setSwapTankRingHotkey(key)),
            'tankSlot': lambda context, value: (
                context.setSwapTankRingSlotByKey(value)),
            'hpLessThanOrEqual': lambda context, value: (
                context.setSwapRingHpPercentageLessThanOrEqual(value)),
            'mainHotkey': lambda context, key: (
                context.setSwapMainRingHotkey(key)),
            'mainSlot': lambda context, value: (
                context.setSwapMainRingSlotByKey(value)),
            'hpGreaterThan': lambda context, value: (
                context.setSwapRingHpPercentageGreaterThan(value)),
        })


def swapAmuletSpec():
    return swapSpec(
        'swapAmulet', 'Swap amulet', ('tankAmulet', 'mainAmulet'), {
            'tankHotkey': lambda context, key: (
                context.setSwapTankAmuletHotkey(key)),
            'tankSlot': lambda context, value: (
                context.setSwapTankAmuletSlotByKey(value)),
            'hpLessThanOrEqual': lambda context, value: (
                context.setSwapAmuletHpPercentageLessThanOrEqual(value)),
            'mainHotkey': lambda context, key: (
                context.setSwapMainAmuletHotkey(key)),
            'mainSlot': lambda context, value: (
                context.setSwapMainAmuletSlotByKey(value)),
            'hpGreaterThan': lambda context, value: (
                context.setSwapAmuletHpPercentageGreaterThan(value)),
        })


class HighPriorityTab(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.healthFoodCard = HealingRuleCard(
            self, context, foodSpec(
                'healthFood', 'Health food', 'HP % less than or equal:',
                'hpPercentageLessThanOrEqual',
                lambda context, value: (
                    context.setHealthFoodHpPercentageLessThanOrEqual(value))))
        self.healthFoodCard.grid(
            column=0, row=0, padx=(10, 5), pady=10, sticky='new')

        self.manaFoodCard = HealingRuleCard(
            self, context, foodSpec(
                'manaFood', 'Mana food', 'Mana % less than or equal:',
                'manaPercentageLessThanOrEqual',
                lambda context, value: (
                    context.setManaFoodHpPercentageLessThanOrEqual(value))))
        self.manaFoodCard.grid(
            column=1, row=0, padx=(5, 10), pady=10, sticky='new')

        self.swapRingCard = HealingRuleCard(self, context, swapRingSpec())
        self.swapRingCard.grid(
            column=0, row=1, padx=(10, 5), pady=(0, 10), sticky='new')

        self.swapAmuletCard = HealingRuleCard(self, context, swapAmuletSpec())
        self.swapAmuletCard.grid(
            column=1, row=1, padx=(5, 10), pady=(0, 10), sticky='new')
