/*
 *   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.misslewars;

import de.steamwar.network.NetworkSender;
import de.steamwar.network.packets.common.FightInfoPacket;
import de.steamwar.sql.SteamwarUser;
import org.bukkit.Bukkit;
import org.bukkit.World;

import java.util.List;
import java.util.stream.Collectors;

public class FightInfoPacketSender implements Runnable {

    private final World world = Bukkit.getWorlds().get(0);

    private final String serverName = Bukkit.getServer().getName();
    private final String gameMode = "missilewars";
    private final String worldName = world.getName();
    private final String blueName = Config.TeamBlueColor + Config.TeamBlueName;
    private final String redName = Config.TeamRedColor + Config.TeamRedName;
    private final int blueLeader = Config.BlueLeader != null ? SteamwarUser.get(Config.BlueLeader).getId() : 0;
    private final int redLeader = Config.RedLeader != null ? SteamwarUser.get(Config.RedLeader).getId() : 0;

    @Override
    public void run() {
        if (Config.test()) {
            return;
        }

        int countdown;
        if (FightScoreboard.getStartTime() == 0) {
            countdown = 0;
        } else {
            countdown = (int) ((System.currentTimeMillis() - FightScoreboard.getStartTime()) / 1000);
        }

        List<Integer> bluePlayers = MissileWars.getBlueTeam().getPlayers().stream()
                .map(player -> SteamwarUser.get(player.getUniqueId()).getId())
                .collect(Collectors.toList());
        List<Integer> redPlayers = MissileWars.getRedTeam().getPlayers().stream()
                .map(player -> SteamwarUser.get(player.getUniqueId()).getId())
                .collect(Collectors.toList());
        List<Integer> spectatorPlayers = Bukkit.getOnlinePlayers().stream()
                .map(player -> SteamwarUser.get(player.getUniqueId()).getId())
                .filter(integer -> !(bluePlayers.contains(integer) || redPlayers.contains(integer)))
                .collect(Collectors.toList());

        NetworkSender.send(new FightInfoPacket(serverName, gameMode, worldName, blueName, redName, MissileWars.getFightState().name().toLowerCase(), countdown, blueLeader, redLeader, 0, 0, bluePlayers, redPlayers, spectatorPlayers));
    }
}
