/*
 *   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.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.world.regions.Region;
import de.steamwar.bausystem.world.Welt;
import de.steamwar.bausystem.world.regions.RegionExtensionType;
import de.steamwar.bausystem.world.regions.RegionType;
import de.steamwar.command.SWCommand;
import de.steamwar.command.SWCommandUtils;
import de.steamwar.command.TypeMapper;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityExplodeEvent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommandTNT extends SWCommand implements Listener {

    public enum TNTMode {
        ON("§aan"),
        ONLY_TB("§7Kein §eBaurahmen"),
        OFF("§caus");

        private String name;

        TNTMode(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }

    public CommandTNT() {
        super("tnt");
        Bukkit.getPluginManager().registerEvents(this, BauSystem.getPlugin());
    }

    @Register(help = true)
    public void genericHelp(Player p, String... args) {
        p.sendMessage("§8/§etnt §8- §7Ändere das TNT verhalten");
        p.sendMessage("§8/§etnt §8[§7Mode§8] §8- §7Setzte das TNT verhalten auf einen Modus");
    }

    @Register
    public void toggleCommand(Player p) {
        if (!permissionCheck(p)) return;
        Region region = Region.getRegion(p.getLocation());
        tntToggle(region, null, null);
    }

    @Register
    public void setCommand(Player p, TNTMode tntMode) {
        if (!permissionCheck(p)) return;
        Region region = Region.getRegion(p.getLocation());

        String requestedMessage = null;
        switch (tntMode) {
            case ON:
                requestedMessage = getEnableMessage();
                break;
            case OFF:
                requestedMessage = getDisableMessage();
                break;
            case ONLY_TB:
                requestedMessage = getTestblockEnableMessage();
                break;
        }
        tntToggle(region, tntMode, requestedMessage);
    }

    private boolean permissionCheck(Player p) {
        if (Welt.noPermission(p, Permission.WORLD)) {
            p.sendMessage(BauSystem.PREFIX + "§cDu darfst hier nicht TNT-Schaden (de-)aktivieren");
            return false;
        }
        return true;
    }

    @ClassMapper(value = TNTMode.class, local = true)
    public TypeMapper<TNTMode> tntModeTypeMapper() {
        Map<String, TNTMode> tntModeMap = new HashMap<>();
        tntModeMap.put("an", TNTMode.ON);
        tntModeMap.put("on", TNTMode.ON);
        tntModeMap.put("aus", TNTMode.OFF);
        tntModeMap.put("off", TNTMode.OFF);
        if (Region.buildAreaEnabled()) {
            tntModeMap.put("testblock", TNTMode.ONLY_TB);
            tntModeMap.put("tb", TNTMode.ONLY_TB);
        }
        List<String> tabCompletes = new ArrayList<>(tntModeMap.keySet());
        return SWCommandUtils.createMapper(s -> tntModeMap.getOrDefault(s, null), s -> tabCompletes);
    }

    private String getEnableMessage() {
        return "§aTNT-Schaden aktiviert";
    }

    private String getDisableMessage() {
        return "§cTNT-Schaden deaktiviert";
    }

    private String getTestblockEnableMessage() {
        return "§aTNT-Schaden außerhalb Baurahmen aktiviert";
    }

    private void tntToggle(Region region, TNTMode requestedMode, String requestedMessage) {
        if (requestedMode != null && region.hasTestblock()) {
            region.setTntMode(requestedMode);
            RegionUtils.actionBar(region, requestedMessage);
            return;
        }
        switch (region.getTntMode()) {
            case ON:
            case ONLY_TB:
                region.setTntMode(TNTMode.OFF);
                RegionUtils.actionBar(region, getDisableMessage());
                break;
            case OFF:
                if (Region.buildAreaEnabled() && region.hasTestblock()) {
                    region.setTntMode(TNTMode.ONLY_TB);
                    RegionUtils.actionBar(region, getTestblockEnableMessage());
                } else {
                    region.setTntMode(TNTMode.ON);
                    RegionUtils.actionBar(region, getEnableMessage());
                }
                break;
        }
    }

    @EventHandler
    public void onExplode(EntityExplodeEvent event) {
        event.blockList().removeIf(block -> {
            Region region = Region.getRegion(block.getLocation());
            if (region.getTntMode() == TNTMode.ON) return false;
            if (region.hasBuildRegion() && region.inRegion(block.getLocation(), RegionType.BUILD, RegionExtensionType.NORMAL)) {
                RegionUtils.actionBar(region, "§cEine Explosion hätte Blöcke im Baubereich zerstört");
                return true;
            }
            if (region.hasBuildRegion() && region.inRegion(block.getLocation(), RegionType.BUILD, RegionExtensionType.EXTENSION)) {
                RegionUtils.actionBar(region, "§cEine Explosion hätte Blöcke im Ausfahrbereich zerstört");
                return true;
            }
            return region.getTntMode() == TNTMode.OFF;
        });
    }
}
