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

import de.steamwar.bausystem.world.regions.*;
import de.steamwar.command.SWCommand;
import java.util.concurrent.atomic.AtomicLong;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;

public class CommandKillAll extends SWCommand {

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

    public CommandKillAll() {
        super("killall", "removeall");
    }

    @Register(help = true)
    public void genericHelp(Player player, String... args) {
        player.sendMessage("§8/§ekillall §8- §7Entferne alle Entities aus deiner Region");
        player.sendMessage("§8/§ekillall §8[§7Global§8/Local§7] §8- §7Entferne alle Entities aus deiner Region oder global");
    }

    @Register
    public void genericCommand(Player player) {
        genericCommand(player, RegionSelectionType.LOCAL);
    }

    @Register
    public void genericCommand(Player player, RegionSelectionType regionSelectionType) {
        Region region = Region.getRegion(player.getLocation());
        AtomicLong removedEntities = new AtomicLong();
        if (regionSelectionType == RegionSelectionType.GLOBAL || GlobalRegion.isGlobalRegion(region)) {
            WORLD.getEntities()
                 .stream()
                 .filter(e -> !(e instanceof Player))
                 .forEach(entity -> {
                     entity.remove();
                     removedEntities.getAndIncrement();
                 });
            RegionUtils.actionBar(GlobalRegion.getInstance(), "§a" + removedEntities.get() + " Entities aus der Welt entfernt");
        } else {
            WORLD.getEntities()
                 .stream()
                 .filter(e -> !(e instanceof Player))
                 .filter(e -> region.inRegion(e.getLocation(), RegionType.NORMAL, RegionExtensionType.NORMAL))
                 .forEach(entity -> {
                     entity.remove();
                     removedEntities.getAndIncrement();
                 });
            RegionUtils.actionBar(region, "§a" + removedEntities.get() + " Entities aus der Region entfernt");
        }
    }

}
