import customtkinter

from src.ui import theme
from src.ui.widgets import FormModal, SearchableComboBox, positiveInt

HEALTH_POTIONS = [
    'Small Health Potion', 'Health Potion', 'Strong Health Potion',
    'Great Health Potion', 'Ultimate Health Potion', 'Supreme Health Potion',
]
MANA_POTIONS = [
    'Mana Potion', 'Strong Mana Potion', 'Great Mana Potion',
    'Ultimate Mana Potion',
]


class RefillModal(FormModal):
    """Potion-Nachkauf beim NPC konfigurieren."""

    def __init__(self, parent, onConfirm=lambda: {}, waypoint=None):
        super().__init__(parent, 'Refill beim NPC')
        self.onConfirm = onConfirm
        options = waypoint['options'] if waypoint is not None else {}

        self.healthSwitch = self._switch(
            0, 'Health Potion kaufen',
            options.get('healthPotionEnabled', False))
        self.healthPicker = self._picker(
            1, HEALTH_POTIONS, options.get('healthPotion', {}).get('item'))
        self.healthEntry = self._amount(
            2, options.get('healthPotion', {}).get('quantity'))

        self.houseSwitch = self._switch(
            3, 'Beim House-NPC kaufen',
            options.get('houseNpcEnabled', False))
        self.manaPicker = self._picker(
            4, MANA_POTIONS, options.get('manaPotion', {}).get('item'))
        self.manaEntry = self._amount(
            5, options.get('manaPotion', {}).get('quantity'))

    def _switch(self, row, label, initial):
        switch = customtkinter.CTkSwitch(
            self.body, text=label, progress_color=theme.ACCENT,
            font=theme.font(12),
        )
        switch.grid(row=row, column=0, columnspan=2, sticky='w', pady=3)
        if initial:
            switch.select()
        return switch

    def _picker(self, row, values, current):
        picker = SearchableComboBox(
            self.body, values=values, placeholder='Potion suchen …')
        picker.grid(row=row, column=0, columnspan=2, sticky='ew', pady=3)
        if current:
            picker.set(current)
        return picker

    def _amount(self, row, current):
        entry = customtkinter.CTkEntry(
            self.body, placeholder_text='Menge', validate='key',
            validatecommand=(self.register(positiveInt), '%P'),
        )
        entry.grid(row=row, column=0, columnspan=2, sticky='ew', pady=3)
        if current is not None:
            entry.insert(0, str(current))
        return entry

    def confirm(self):
        healthQty = self.healthEntry.get()
        manaQty = self.manaEntry.get()
        if self.healthSwitch.get() == 1 and not positiveInt(healthQty):
            return self.fail('Menge fuer Health Potion fehlt.')
        if not positiveInt(manaQty):
            return self.fail('Menge fuer Mana Potion fehlt.')
        if self.healthSwitch.get() == 1 and \
                self.healthPicker.get() not in HEALTH_POTIONS:
            return self.fail('Health Potion aus der Liste waehlen.')
        if self.manaPicker.get() not in MANA_POTIONS:
            return self.fail('Mana Potion aus der Liste waehlen.')
        self.onConfirm(None, {
            'healthPotionEnabled': self.healthSwitch.get() == 1,
            'houseNpcEnabled': self.houseSwitch.get() == 1,
            'healthPotion': {
                'item': self.healthPicker.get(),
                'quantity': int(healthQty or 0),
            },
            'manaPotion': {
                'item': self.manaPicker.get(),
                'quantity': int(manaQty or 0),
            },
        })
        self.destroy()
