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.impl.UpgradeCard;
import com.ghost.technic.server.card.playable.usable.impl.CreatureCard;
import com.ghost.technic.server.game.GameActionService;
import com.ghost.technic.server.game.GameState;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class WayOfTheBearAbility implements CardAbility {

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

        CreatureCard target = findAttachedCreature(game.getGameState(), upgrade);
        if (target == null) return;

        var effect = new WayOfTheBearEffect(target);
        effect.apply();

        game.getGameState().registerStaticEffect(upgrade, effect);
    }

    private CreatureCard findAttachedCreature(GameState gameState, UpgradeCard upgrade) {
        for (Player player : List.of(gameState.getPlayerOne(), gameState.getPlayerTwo())) {
            for (CreatureCard creature : player.getPlayArea().getBattleline().getCreatures()) {
                if (creature.getUpgrades().contains(upgrade)) return creature;
            }
        }
        return null;
    }
}
