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

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.config.BauServer;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.region.RegionSystem;
import de.steamwar.bausystem.region.RegionUtils;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.utils.PasteBuilder;
import de.steamwar.command.SWCommand;
import de.steamwar.linkage.Linked;
import de.steamwar.linkage.LinkedInstance;
import de.steamwar.sql.Punishment;
import de.steamwar.sql.SchematicNode;
import de.steamwar.sql.SteamwarUser;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.logging.Level;

@Linked
public class ResetCommand extends SWCommand {

    @LinkedInstance
    public BauServer bauServer;

    public ResetCommand() {
        super("reset");
    }

    @Register(description = "REGION_RESET_HELP_RESET")
    public void genericResetCommand(@Validator Player p) {
        Region region = regionCheck(p);
        if (region == null) return;
        try {
            PasteBuilder pasteBuilder = new PasteBuilder(new PasteBuilder.FileProvider(region.getArea().getResetFile()))
                    .color(region.getRegionData().get(Flag.COLOR).getWithDefault());
            region.getArea().reset(pasteBuilder, false);
            region.getRegionData().clear();
            RegionUtils.message(region, "REGION_RESET_RESETED");
        } catch (SecurityException e) {
            BauSystem.MESSAGE.send("REGION_RESET_ERROR", p);
            Bukkit.getLogger().log(Level.WARNING, "Failed testblock", e);
        }
    }

    @Register(description = "REGION_RESET_HELP_SCHEMATIC")
    public void schematicResetCommand(@Validator Player p, SchematicNode node) {
        Region region = regionCheck(p);
        if (region == null) return;

        if (!p.getUniqueId().equals(bauServer.getOwner())) {
            if (Punishment.isPunished(SteamwarUser.get(bauServer.getOwner()), Punishment.PunishmentType.NoSchemReceiving, punishment -> BauSystem.MESSAGE.send("REGION_TB_NO_SCHEMRECEIVING", p, punishment.getEndTime()))) {
                return;
            }
            if (Punishment.isPunished(SteamwarUser.get(p.getUniqueId()), Punishment.PunishmentType.NoSchemSharing, punishment -> BauSystem.MESSAGE.send("REGION_TB_NO_SCHEMSHARING", p, punishment.getEndTime()))) {
                return;
            }
        }

        if (node.isDir()) {
            BauSystem.MESSAGE.send("ONLY_SCHEMS", p);
            return;
        }
        try {
            PasteBuilder pasteBuilder = new PasteBuilder(new PasteBuilder.SchematicProvider(node))
                    .color(region.getRegionData().get(Flag.COLOR).getWithDefault());
            region.getArea().reset(pasteBuilder, true);
            RegionUtils.message(region, "REGION_RESET_RESETED");
        } catch (SecurityException e) {
            BauSystem.MESSAGE.send("REGION_RESET_ERROR", p);
            Bukkit.getLogger().log(Level.WARNING, "Failed reset", e);
        }
    }

    private Region regionCheck(Player player) {
        Region region = Region.getRegion(player.getLocation());
        if (region == RegionSystem.INSTANCE.getGlobalRegion()) {
            BauSystem.MESSAGE.send("REGION_RESET_NO_REGION", player);
            return null;
        }
        return region;
    }
}
