/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.fightsystem.winconditions;

import de.steamwar.fightsystem.FightSystem;
import de.steamwar.fightsystem.fight.Fight;
import de.steamwar.fightsystem.fight.FightPlayer;
import de.steamwar.fightsystem.fight.FightTeam;
import de.steamwar.fightsystem.states.FightState;
import de.steamwar.fightsystem.states.OneShotStateDependent;
import de.steamwar.fightsystem.states.StateDependentListener;
import de.steamwar.linkage.Linked;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerQuitEvent;

import java.util.*;

@Linked
public class WinconditionAmongUs extends Wincondition implements Listener {

    private Map<FightTeam, FightPlayer> imposter = new HashMap<>();

    private final Random random = new Random();

    public WinconditionAmongUs() {
        super("AmongUs");
        new OneShotStateDependent(Winconditions.AMONG_US, FightState.Ingame, () -> {
            imposter.put(Fight.getRedTeam(), sendMessageAndReturnImposter(Fight.getRedTeam().getPlayers()));
            imposter.put(Fight.getBlueTeam(), sendMessageAndReturnImposter(Fight.getBlueTeam().getPlayers()));
        });
        new StateDependentListener(Winconditions.AMONG_US, FightState.Ingame, this);
    }

    private FightPlayer sendMessageAndReturnImposter(Collection<FightPlayer> fightPlayers) {
        List<FightPlayer> fightPlayerList = new ArrayList<>(fightPlayers);
        FightPlayer imposter = fightPlayerList.get(random.nextInt(fightPlayerList.size()));
        for (FightPlayer fightPlayer : fightPlayerList) {
            if (fightPlayer == imposter) {
                FightSystem.getMessage().send("AMONG_US_IMPOSTER_MESSAGE", fightPlayer.getEntity());
            } else {
                FightSystem.getMessage().send("AMONG_US_IMPOSTER_AMONG_MESSAGE", fightPlayer.getEntity());
            }
        }
        return imposter;
    }

    @EventHandler
    public void handlePlayerDeath(PlayerDeathEvent event) {
        handleDeath(event.getEntity().getPlayer());
    }

    @EventHandler
    public void handlePlayerQuit(PlayerQuitEvent event) {
        handleDeath(event.getPlayer());
    }

    private void handleDeath(Player player) {
        FightTeam team = isTarget(player);
        if (team == null) return;

        FightPlayer current = team.getFightPlayer(player);
        if (current == imposter.get(team)) {
            win(team, "WIN_IMPOSTER_DEAD", team.getPrefix());
        } else if (team.getAlivePlayers() <= 2 && imposter.get(team).isLiving()) {
            win(Fight.getOpposite(team), "WIN_CREWMATE_DEAD", Fight.getOpposite(team).getPrefix());
        }
    }
}
