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;

public class BarristerJoyaAbility implements CardAbility {

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

        BarristerJoyaConstantAbility effect = new BarristerJoyaConstantAbility(joya);
        effect.apply();
        gameActionService.getGameState().registerStaticEffect(joya, effect);

        gameActionService.getGameState().getEventBus().subscribe(EventType.CREATURE_DESTROYED, event -> {
            CreatureCard destroyed = event.get("creature");
            if (destroyed.equals(joya)) {
                effect.remove();
                gameActionService.getGameState().unregisterStaticEffect(joya);
            }
        });
    }
}
