
from dext.views import handler, validate_argument

from the_tale.common.utils.resources import Resource
from the_tale.common.utils.decorators import login_required
from the_tale.common.utils import api

from the_tale.game.abilities.deck import ABILITIES
from the_tale.game.abilities.relations import ABILITY_TYPE

from the_tale.game.heroes import logic as heroes_logic


def argument_to_ability(ability_type): return ABILITIES.get(ABILITY_TYPE(ability_type))


class AbilitiesResource(Resource):

    @login_required
    @validate_argument('ability', argument_to_ability, 'abilities', 'Неверный идентификатор способности')
    def initialize(self, ability=None, *argv, **kwargs):
        super(AbilitiesResource, self).initialize(*argv, **kwargs)
        self.ability = ability()

    @api.handler(versions=('1.0',))
    @validate_argument('battle', int, 'abilities', 'Неверный идентификатор сражения')
    @handler('#ability', 'api', 'use', method='post')
    def use(self, api_version, battle=None):
        task = self.ability.activate(heroes_logic.load_hero(account_id=self.account.id),
                                     data={'battle_id': battle})

        if task is None:
            return self.error('game.abilities.use.no_enough_energy', 'Недостаточно энергии')

        return self.processing(task.status_url)
