/*
 *   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.Color;
import de.steamwar.bausystem.world.Welt;
import de.steamwar.bausystem.world.regions.GlobalRegion;
import de.steamwar.bausystem.world.regions.Region;
import de.steamwar.bausystem.world.regions.RegionExtensionType;
import de.steamwar.bausystem.world.regions.RegionType;
import de.steamwar.command.SWCommand;
import de.steamwar.sql.SchematicNode;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.io.IOException;
import java.util.logging.Level;

public class CommandRegion extends SWCommand {

    public CommandRegion() {
        super("region", "rg");
    }

    @Register
    public void genericCommand(Player player) {
        genericHelp(player);
    }

    @Register(help = true)
    public void genericHelp(Player player, String... args) {
        player.sendMessage("§8/§eregion undo §8- §7Mache die letzten 20 /testblock oder /reset rückgängig");
        player.sendMessage("§8/§eregion redo §8- §7Wiederhole die letzten 20 §8/§7rg undo");
        player.sendMessage("§8/§eregion restore §8- §7Setzte die Region zurück, ohne das Gebaute zu löschen");
        player.sendMessage("§8/§eregion §8[§7RegionsTyp§8] §8- §7Wähle einen RegionsTyp aus");
        player.sendMessage("§8/§eregion §8[§7RegionsTyp§8] §8[§7Extension§8] §8- §7Wähle einen RegionsTyp aus mit oder ohne Extension");
        player.sendMessage("§8/§eregion color §8[§7Color§8] §8- §7Ändere die Regions Farbe");
    }

    @Register("undo")
    public void undoCommand(Player p) {
        if(!permissionCheck(p)) return;
        Region region = Region.getRegion(p.getLocation());
        if(checkGlobalRegion(region, p)) return;

        if (region.undo()) {
            p.sendMessage(BauSystem.PREFIX + "Letzte Aktion rückgangig gemacht");
        } else {
            p.sendMessage(BauSystem.PREFIX + "§cNichts zum rückgängig machen");
        }
    }

    @Register("redo")
    public void redoCommand(Player p) {
        if (!permissionCheck(p)) {
            return;
        }
        Region region = Region.getRegion(p.getLocation());
        if (checkGlobalRegion(region, p)) {
            return;
        }

        if (region.redo()) {
            p.sendMessage(BauSystem.PREFIX + "Letzte Aktion wiederhohlt");
        } else {
            p.sendMessage(BauSystem.PREFIX + "§cNichts zum wiederhohlen");
        }
    }

    @Register
    public void baurahmenCommand(Player p, RegionType regionType) {
        CommandSelect.getInstance().baurahmenCommand(p, regionType, RegionExtensionType.NORMAL);
    }

    @Register
    public void baurahmenCommand(Player p, RegionType regionType, RegionExtensionType regionExtensionType) {
        CommandSelect.getInstance().baurahmenCommand(p, regionType, regionExtensionType);
    }

    @Register("restore")
    public void genericRestoreCommand(Player p) {
        if (!permissionCheck(p)) return;
        Region region = Region.getRegion(p.getLocation());
        if(checkGlobalRegion(region, p)) return;

        if (region == null) return;
        try {
            region.reset(null, true);
            p.sendMessage(BauSystem.PREFIX + "§7Region zurückgesetzt");
        } catch (IOException e) {
            p.sendMessage(BauSystem.PREFIX + "§cFehler beim Zurücksetzen der Region");
            Bukkit.getLogger().log(Level.WARNING, "Failed testblock", e);
        }
    }

    @Register("restore")
    public void schematicRestoreCommand(Player p, SchematicNode schem) {
        if (!permissionCheck(p)) return;
        Region region = Region.getRegion(p.getLocation());
        if(checkGlobalRegion(region, p)) return;

        if (region == null) return;
        try {
            region.reset(schem, true);
            p.sendMessage(BauSystem.PREFIX + "§7Region zurückgesetzt");
        } catch (IOException e) {
            p.sendMessage(BauSystem.PREFIX + "§cFehler beim Zurücksetzen der Region");
            Bukkit.getLogger().log(Level.WARNING, "Failed reset", e);
        }
    }

    @Register("color")
    public void colorCommand(Player p, Color color) {
        CommandColor.getInstance().genericColor(p, color);
    }

    static boolean checkGlobalRegion(Region region, Player p) {
        if (GlobalRegion.isGlobalRegion(region)) {
            p.sendMessage(BauSystem.PREFIX + "§cDu bist in keiner Region");
            return true;
        }
        return false;
    }

    private boolean permissionCheck(Player player) {
        if (Welt.noPermission(player, Permission.WORLDEDIT)) {
            player.sendMessage(BauSystem.PREFIX + "§cDu darfst hier nicht die Region verändern");
            return false;
        }
        return true;
    }
}
