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

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.utils.BauMemberUpdateEvent;
import de.steamwar.command.SWCommand;
import de.steamwar.linkage.Linked;
import org.bukkit.Location;
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 DesignEndStoneCommand extends SWCommand implements Listener {

    public DesignEndStoneCommand() {
        super("designendstone");
    }

    private Map<Region, DesignEndStone> designEndStoneMap = new HashMap<>();

    @Register(description = "DESIGN_ENDSTONE_COMMAND_HELP")
    public void genericCommand(@Validator Player player) {
        Region region = Region.getRegion(player.getLocation());
        if (region.getBuildArea().isEmpty()) {
            BauSystem.MESSAGE.send("DESIGN_ENDSTONE_REGION_ERROR", player);
            return;
        }
        if (!region.getGameModeConfig().loaded) {
            BauSystem.MESSAGE.send("DESIGN_ENDSTONE_REGION_ERROR", player);
            return;
        }
        designEndStoneMap.computeIfAbsent(region, DesignEndStone::new).toggle(player);
    }

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

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

    private void disableDesignEndStone(Player player) {
        new HashSet<>(designEndStoneMap.entrySet()).forEach(regionDesignEndStoneEntry -> {
            if (regionDesignEndStoneEntry.getValue().removePlayer(player)) {
                designEndStoneMap.remove(regionDesignEndStoneEntry.getKey());
            }
        });
    }

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

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

    private void update(Location location) {
        Region region = Region.getRegion(location);
        DesignEndStone designEndStone = designEndStoneMap.get(region);
        if (designEndStone == null) {
            return;
        }
        designEndStone.calc();
    }
}
