import customtkinter

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

ATTACK_SPELLS = ['utito tempo', 'utamo tempo', 'exori', 'exori gran',
                 'exori mas', 'exori min', 'exeta amp res', 'exeta res']


class ComboSpellModal(FormModal):
    """Einen Spell der Kombo aendern (Name + Hotkey, live gespeichert)."""

    def __init__(self, parent, spell=None, context=None, index=None,
                 indexSecond=None):
        super().__init__(parent, 'Spell bearbeiten')
        self.context = context
        self.index = index
        self.indexSecond = indexSecond
        self.body.grid_columnconfigure(0, weight=1)

        customtkinter.CTkLabel(
            self.body, text='Spell', font=theme.font(12),
            text_color=theme.MUTED,
        ).grid(row=0, column=0, columnspan=2, sticky='w', pady=(4, 2))
        self.picker = SearchableComboBox(
            self.body, values=ATTACK_SPELLS, placeholder='Spell suchen …',
            command=lambda _v: self.onChangeSpell(),
        )
        self.picker.grid(row=1, column=0, columnspan=2, sticky='ew',
                         pady=(0, 6))
        if spell is not None:
            self.picker.set(spell['name'])

        customtkinter.CTkLabel(
            self.body, text='Hotkey (Taste drücken)', font=theme.font(12),
            text_color=theme.MUTED,
        ).grid(row=2, column=0, columnspan=2, sticky='w', pady=(4, 2))
        self.hotkeyEntry = HotkeyEntry(
            self.body, initial=spell['hotkey'] if spell is not None else '',
            on_change=self.onChangeHotkey,
        )
        self.hotkeyEntry.grid(row=3, column=0, columnspan=2, sticky='ew',
                              pady=(0, 6))

    def onChangeHotkey(self, key):
        self.context.setComboSpellHotkey(
            key, index=self.index, indexSecond=self.indexSecond)

    def onChangeSpell(self):
        self.context.setComboSpellName(
            self.picker.get(), index=self.index,
            indexSecond=self.indexSecond)

    def confirm(self):
        self.destroy()
