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

import com.ghost.technic.server.Player;
import com.ghost.technic.server.board.Flank;
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.GameState;
import com.ghost.technic.server.game.event.AmberChangeType;
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.Set;

@Slf4j
public class DexusTriggeredAbility implements CardAbility {

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

        GameState gameState = gameActionService.getGameState();

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

            if (usedType.equals(UsageType.PLAY)) {
                Player playedBy = event.get("player");
                Set<Flank> flanks = event.get("flanks");

                if (playedBy.equals(gameState.getOwnerOf(dexus))) return;

                if (flanks.contains(Flank.RIGHT)) {
                    gameActionService.adjustAmber(playedBy, -1, AmberChangeType.LOST, dexus);
                }
            }
        });
    }
}

