package com.trunksbomb.minetriad.blockentity;

import com.trunksbomb.minetriad.game.DuelSession;
import com.trunksbomb.minetriad.game.DuelSessionManager;
import com.trunksbomb.minetriad.game.MoveResult;
import com.trunksbomb.minetriad.item.CardItem;
import com.trunksbomb.minetriad.registry.TriadItems;

import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.Level;

public final class DuelTableAnimationResolver {
    private DuelTableAnimationResolver() {
    }

    static void handleAfterPlayerAnimation(Level level, BlockPos pos, DuelTableBlockEntity table, DuelSession session) {
        ServerPlayer player = getPlayer(level, session);
        if (player == null) {
            return;
        }

        if (session.isComplete()) {
            enterOverview(table, session, player);
            return;
        }

        if (session.isAiVsAi()) {
            startAiTurn(table, session, player);
            return;
        }

        MoveResult opponentMove = session.playOpponentTurn();
        if (!opponentMove.valid()) {
            player.displayClientMessage(Component.literal(opponentMove.errorMessage()).withStyle(ChatFormatting.RED), false);
            table.clearBoard();
            DuelSessionManager.end(player, true);
            return;
        }

        if (!table.setCard(opponentMove.playedCell().index(), CardItem.createCardStack(opponentMove.playedCardId(), TriadItems.TRIAD_CARD.get()), DuelTableBlockEntity.OWNER_SECOND)) {
            player.displayClientMessage(Component.literal("The Duel Table could not stage the opponent move.").withStyle(ChatFormatting.RED), false);
            table.clearBoard();
            DuelSessionManager.end(player, true);
            return;
        }

        session.beginOpponentAnimation();
        table.updateHudState(session);
        table.startMoveAnimation(opponentMove, DuelTableBlockEntity.OWNER_SECOND);
    }

    static void handleAfterOpponentAnimation(Level level, BlockPos pos, DuelTableBlockEntity table, DuelSession session) {
        ServerPlayer player = getPlayer(level, session);
        if (player == null) {
            return;
        }

        if (session.isComplete()) {
            enterOverview(table, session, player);
            return;
        }

        if (session.isAiVsAi()) {
            startAiTurn(table, session, player);
            return;
        }

        session.returnToPlaying();
        table.updateHudState(session);
    }

    public static void startAiTurn(DuelTableBlockEntity table, DuelSession session, ServerPlayer player) {
        MoveResult aiMove = session.playAiTurn();
        if (!aiMove.valid()) {
            player.displayClientMessage(Component.literal(aiMove.errorMessage()).withStyle(ChatFormatting.RED), false);
            table.clearBoard();
            DuelSessionManager.end(player, true);
            return;
        }

        int owner = session.isPlayerTurn() ? DuelTableBlockEntity.OWNER_SECOND : DuelTableBlockEntity.OWNER_FIRST;
        if (!table.setCard(aiMove.playedCell().index(), CardItem.createCardStack(aiMove.playedCardId(), TriadItems.TRIAD_CARD.get()), owner)) {
            player.displayClientMessage(Component.literal("The Duel Table could not stage the AI move.").withStyle(ChatFormatting.RED), false);
            table.clearBoard();
            DuelSessionManager.end(player, true);
            return;
        }

        if (owner == DuelTableBlockEntity.OWNER_FIRST) {
            session.beginPlayerAnimation();
        } else {
            session.beginOpponentAnimation();
        }
        table.updateHudState(session);
        table.startMoveAnimation(aiMove, owner);
    }

    private static void enterOverview(DuelTableBlockEntity table, DuelSession session, ServerPlayer player) {
        session.enterOverview();
        table.setRewardCards(session.opponentRewardCards().stream()
                .map(cardId -> CardItem.createCardStack(cardId, TriadItems.TRIAD_CARD.get()))
                .toList());
        table.removeBoardCardsOwnedBy(DuelTableBlockEntity.OWNER_SECOND);
        table.updateHudState(session);
        table.startOverviewSweepAnimation();
        player.displayClientMessage(session.resultSummary().copy().withStyle(ChatFormatting.AQUA), false);
    }

    private static ServerPlayer getPlayer(Level level, DuelSession session) {
        return level.getServer() == null ? null : level.getServer().getPlayerList().getPlayer(session.playerParticipantId());
    }
}
