const _ = require('underscore');

const DrawCard = require('../../../drawcard.js');

class BloodOfMyBlood extends DrawCard {
    canPlay(player, card) {
        if(this !== card || player.phase !== 'challenge') {
            return false;
        }

        return super.canPlay(player, card);
    }

    play(player) {
        var bloodRiders = player.searchDrawDeck(card => {
            return card.getType() === 'character' && card.hasTrait('Bloodrider');
        });

        var buttons = _.map(bloodRiders, card => {
            return { text: card.name, method: 'cardSelected', arg: card.uuid, card: card.getSummary(true) };
        });
        buttons.push({ text: 'Done', method: 'doneSelecting' });

        this.game.promptWithMenu(this.controller, this, {
            activePrompt: {
                menuTitle: 'Select a card to put it in play',
                buttons: buttons
            },
            waitingPromptTitle: 'Waiting for opponent to use ' + this.name
        });

        return true;
    }

    cardSelected(player, cardId) {
        var card = player.findCardByUuid(player.drawDeck, cardId);

        if(!card) {
            return false;
        }

        player.playCard(card, true, true);
        player.shuffleDrawDeck();

        this.atEndOfPhase(ability => ({
            match: card,
            effect: ability.effects.returnToHandIfStillInPlay(true)
        }));        

        this.game.addMessage('{0} uses {1} to reveal {2} and put it into play', player, this, card);

        return true;
    }

        return true;
    }
}

BloodOfMyBlood.code = '04054';

module.exports = BloodOfMyBlood;
    doneSelecting(player) {
        player.shuffleDrawDeck();

        this.game.addMessage('{0} does not use {1} to put a card in play', player, this);
