/*
 *   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.tracer.show.ShowModeParameterType;
import de.steamwar.bausystem.world.regions.Region;
import de.steamwar.bausystem.world.Welt;
import de.steamwar.bausystem.world.regions.RegionExtensionType;
import de.steamwar.command.SWCommand;
import de.steamwar.command.SWCommandUtils;
import de.steamwar.command.TypeMapper;
import de.steamwar.sql.SchematicNode;
import de.steamwar.sql.SteamwarUser;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;

public class CommandTestblock extends SWCommand {

    public CommandTestblock() {
        super("testblock", "tb");
    }

    @Register(help = true)
    public void genericHelp(Player p, String... args) {
        p.sendMessage("§8/§etestblock §8- §7Setzte den Testblock zurück");
        p.sendMessage("§8/§etestblock §8[§7Schematic§8] §8- §7Setzte den Testblock mit einer Schematic zurück");
    }

    @Register
    public void genericTestblockCommand(Player p) {
        genericTestblockCommand(p, RegionExtensionType.NORMAL);
    }

    @Register
    public void genericTestblockCommand(Player p, RegionExtensionType regionExtensionType) {
        if (!permissionCheck(p)) return;
        Region region = regionCheck(p);
        if (region == null) return;
        try {
            region.resetTestblock(null, regionExtensionType == RegionExtensionType.EXTENSION);
            p.sendMessage(BauSystem.PREFIX + "§7Testblock zurückgesetzt");
        } catch (IOException e) {
            p.sendMessage(BauSystem.PREFIX + "§cFehler beim Zurücksetzen des Testblocks");
            Bukkit.getLogger().log(Level.WARNING, "Failed testblock", e);
        }
    }


    @Register
    public void schematicTestblockCommand(Player p, SchematicNode schem) {
        schematicTestblockCommand(p, schem, RegionExtensionType.NORMAL);
    }

    @Register
    public void schematicTestblockCommand(Player p, RegionExtensionType regionExtensionType, SchematicNode schem) {
        schematicTestblockCommand(p, schem, regionExtensionType);
    }

    @Register
    public void schematicTestblockCommand(Player p, SchematicNode schem, RegionExtensionType regionExtensionType) {
        if (!permissionCheck(p)) return;
        Region region = regionCheck(p);
        if (region == null) return;
        try {
            region.resetTestblock(schem, regionExtensionType == RegionExtensionType.EXTENSION);
            p.sendMessage(BauSystem.PREFIX + "§7Testblock zurückgesetzt");
        } catch (IOException e) {
            p.sendMessage(BauSystem.PREFIX + "§cFehler beim Zurücksetzen des Testblocks");
            Bukkit.getLogger().log(Level.WARNING, "Failed testblock", e);
        }
    }

    @ClassMapper(value = RegionExtensionType.class, local = true)
    private TypeMapper<RegionExtensionType> regionExtensionTypeTypeMapper() {
        Map<String, RegionExtensionType> showModeParameterTypesMap = new HashMap<>();
        showModeParameterTypesMap.put("-normal", RegionExtensionType.NORMAL);
        showModeParameterTypesMap.put("-n", RegionExtensionType.NORMAL);
        showModeParameterTypesMap.put("-extension", RegionExtensionType.EXTENSION);
        showModeParameterTypesMap.put("-e", RegionExtensionType.EXTENSION);

        List<String> tabCompletes = new ArrayList<>(showModeParameterTypesMap.keySet());
        return SWCommandUtils.createMapper(s -> showModeParameterTypesMap.getOrDefault(s, null), s -> tabCompletes);
    }

    private boolean permissionCheck(Player player) {
        if (Welt.noPermission(player, Permission.WORLDEDIT)) {
            player.sendMessage(BauSystem.PREFIX + "§cDu darfst hier nicht den Testblock zurücksetzen");
            return false;
        }
        return true;
    }

    private Region regionCheck(Player player) {
        Region region = Region.getRegion(player.getLocation());
        if (!region.hasTestblock()) {
            player.sendMessage(BauSystem.PREFIX + "§cDu befindest dich derzeit in keiner Region");
            return null;
        }
        return region;
    }
}
