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

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.utils.BauMemberUpdateEvent;
import de.steamwar.bausystem.utils.bossbar.BossBarService;
import de.steamwar.command.SWCommand;
import de.steamwar.linkage.Linked;
import de.steamwar.linkage.LinkedInstance;
import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerQuitEvent;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

@Linked
public class KillcheckerCommand extends SWCommand implements Listener {

    private Map<Region, KillcheckerVisualizer> visualizers = new HashMap<>();

    @LinkedInstance
    public BossBarService bossBarService;

    public KillcheckerCommand() {
        super("killchecker");
        addDefaultHelpMessage("KILLCHECKER_INFO");
        addDefaultHelpMessage("KILLCHECKER_INFO2");
    }

    @Register(value = "enable", description = "KILLCHECKER_HELP_ENABLE")
    public void genericCommand(@Validator Player player, @OptionalValue("-outline") @StaticValue(value = {"-area", "-outline"}, allowISE = true) boolean onlyOutline) {
        Region region = Region.getRegion(player.getLocation());
        if (region.getBuildArea().isEmpty()) {
            BauSystem.MESSAGE.send("KILLCHECKER_NO_BUILD", player);
            return;
        }
        KillcheckerVisualizer killcheckerVisualizer = visualizers.computeIfAbsent(region, region1 -> new KillcheckerVisualizer(region1, bossBarService));
        killcheckerVisualizer.recalc();
        killcheckerVisualizer.show(player, onlyOutline);
        BauSystem.MESSAGE.send("KILLCHECKER_ENABLE", player);
    }

    @Register(value = "disable", description = "KILLCHECKER_HELP_DISABLE")
    public void disableCommand(Player player) {
        Region region = Region.getRegion(player.getLocation());
        KillcheckerVisualizer killcheckerVisualizer = visualizers.get(region);
        if (killcheckerVisualizer != null) {
            if (killcheckerVisualizer.hide(player)) {
                visualizers.remove(region);
            }
        }
        BauSystem.MESSAGE.send("KILLCHECKER_DISABLE", player);
    }

    @EventHandler
    public void onBauMemberUpdate(BauMemberUpdateEvent event) {
        event.getNewSpectator().forEach(this::hide);
    }

    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent event) {
        hide(event.getPlayer());
    }

    private void hide(Player player) {
        new HashSet<>(visualizers.entrySet()).forEach(regionKillcheckerVisualizerEntry -> {
            if (regionKillcheckerVisualizerEntry.getValue().hide(player)) {
                visualizers.remove(regionKillcheckerVisualizerEntry.getKey());
            }
        });
    }

    private void recalc(Block block) {
        Region region = Region.getRegion(block.getLocation());
        KillcheckerVisualizer killcheckerVisualizer = visualizers.get(region);
        if (killcheckerVisualizer != null) {
            killcheckerVisualizer.recalc();
        }
    }

    @EventHandler
    public void onBlockPlace(BlockPlaceEvent event) {
        recalc(event.getBlock());
    }

    @EventHandler
    public void onBlockBreak(BlockBreakEvent event) {
        Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
            recalc(event.getBlock());
        }, 1);
    }
}
