package com.ghost.technic.server.card.effect.impl;

import com.ghost.technic.server.Player;
import com.ghost.technic.server.card.effect.CardAbility;
import com.ghost.technic.server.card.playable.PlayableCard;
import com.ghost.technic.server.card.playable.usable.impl.CreatureCard;
import com.ghost.technic.server.game.GameActionService;
import com.ghost.technic.server.game.event.EventType;
import com.ghost.technic.server.game.event.UsageType;
import com.ghost.technic.server.game.event.UsedEventData;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class MusthicMurmookAbility implements CardAbility {

    @Override
    public void register(GameActionService game, Player controller, PlayableCard source) {
        if (!(source instanceof CreatureCard musthicMurmook)) return;

        // Key cost static effect
        MusthicMurmookEffect effect = new MusthicMurmookEffect();
        effect.apply();
        game.getGameState().registerStaticEffect(musthicMurmook, effect);

        game.getGameState().getEventBus().subscribe(EventType.USED, event -> {
            UsedEventData usedData = event.get("usedEventData");
            PlayableCard playedCard = usedData.getCard();
            var usedType = usedData.getUsageType();

            if (playedCard.equals(musthicMurmook) && usedType.equals(UsageType.PLAY)) {
                List<CreatureCard> choices = game.getGameState().getAllCreaturesInPlay();
                CreatureCard target = controller.chooseCreature(choices, "Musthic Murmook: Deal 4 damage");

                if (target != null) {
                    game.dealDamage(target, 4);
                    log.info("Musthic Murmook deals 4 damage to {}", target.getName());
                }
            }
        });
    }
}

