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

import com.comphenix.tinyprotocol.TinyProtocol;
import de.steamwar.fightsystem.ArenaMode;
import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.FightSystem;
import de.steamwar.fightsystem.fight.Fight;
import de.steamwar.fightsystem.states.FightState;
import de.steamwar.fightsystem.states.StateDependentCommand;
import de.steamwar.linkage.Linked;
import de.steamwar.sql.SWException;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

import java.io.StringWriter;
import java.util.Arrays;
import java.util.logging.Level;

@Linked
public class TechhiderbugCommand implements CommandExecutor {

    public TechhiderbugCommand() {
        new StateDependentCommand(ArenaMode.All, FightState.All, "techhiderbug", this);
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String alias, String[] args) {
        StringWriter writer = new StringWriter();

        try {
            writer.append("ArenaMode: ").append(Config.mode.name()).append('\n');
            writer.append("FightState: ").append(FightState.getFightState().name()).append('\n');
            writer.append("TechHider enabled: ").append(FightState.getStateDependentFeatures().get(FightSystem.getTechHider()).toString()).append('\n');

            writer.append("Arena region: ").append(Config.ArenaRegion.toString()).append('\n');
            writer.append("Team regions: ");
            Fight.teams().forEach(t -> writer.append(t.getName()).append(':').append(t.getExtendRegion().toString()).append(' '));
            writer.append('\n');

            writer.append("HullHider regions: ");
            FightSystem.getHullHider().getHullMap().forEach((t, h) -> writer.append(t.getName()).append(':').append(h.getRegion().toString()).append(' '));
            writer.append('\n');

            writer.append("Hidden regions: ");
            FightSystem.getTechHider().getHiddenRegion().forEach((p, r) -> writer.append(p.getName()).append(':').append(r.toString()).append(' '));
            writer.append('\n');

            writer.append("TinyProtocol: ");
            writer.append(TinyProtocol.instance.toString()).append('\n');

            writer.append('\n').append("Netty pipelines:\n");
            Bukkit.getOnlinePlayers().forEach(p -> writer.append(p.getName()).append(": ").append(String.join(" ", TinyProtocol.instance.getPlayerInterceptors().get(p).getChannel().pipeline().names())).append('\n'));
        } catch (Exception e) {
            writer.append("Error while generating bug report: ").append(e.getMessage()).append('\n');
            Bukkit.getLogger().log(Level.SEVERE, "Error while generating bug report", e);
        }

        SWException.log("Techhider-Bug reported by " + sender.getName() + ": " + Arrays.toString(args), writer.toString());
        return false;
    }
}
