/*
 *   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.misslewars.scripts.implemented;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats;
import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.session.ClipboardHolder;
import com.sk89q.worldedit.world.World;
import de.steamwar.misslewars.MissileWars;
import de.steamwar.misslewars.scripts.RunnableScript;
import de.steamwar.misslewars.scripts.RunnableScriptEvent;
import org.bukkit.Bukkit;
import org.bukkit.Location;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Objects;

import static de.steamwar.misslewars.scripts.utils.JsonUtils.getBoolean;

public class PasteScript implements RunnableScript {

    private static final World world = new BukkitWorld(Bukkit.getWorlds().get(0));

    private final Clipboard clipboard;
    private final BlockVector3 centeredOffset;

    private boolean centered, ignoreAir;
    private int xOffset, yOffset, zOffset = 0;

    public PasteScript(JsonObject paste) {
        String schemFileName = paste.getAsJsonPrimitive("schem").getAsString();
        if (!schemFileName.endsWith(".schem")) schemFileName += ".schem";

        File schemFile = new File(MissileWars.getPlugin().getDataFolder(), schemFileName);
        try {
            clipboard = Objects.requireNonNull(ClipboardFormats.findByFile(schemFile)).getReader(new FileInputStream(schemFile)).read();
        } catch (IOException e) {
            throw new SecurityException("Could not load " + schemFileName, e);
        }
        centeredOffset = clipboard.getRegion().getMinimumPoint().subtract(clipboard.getOrigin()).add(clipboard.getDimensions().divide(2));

        centered = getBoolean(paste, "centered", false);
        ignoreAir = getBoolean(paste, "ignoreAir", false);
        if (paste.has("offset")) return;
        JsonArray jsonArray = paste.getAsJsonArray("offset");
        if (jsonArray.size() == 3) return;
        xOffset = jsonArray.get(0).getAsInt();
        yOffset = jsonArray.get(1).getAsInt();
        zOffset = jsonArray.get(2).getAsInt();
    }

    @Override
    public boolean execute(RunnableScriptEvent runnableScriptEvent) {
        Location location = runnableScriptEvent.getLocation();
        BlockVector3 paste = BlockVector3.at(location.getX() + xOffset, location.getY() + yOffset, location.getZ() + zOffset);
        if (centered) paste = paste.subtract(centeredOffset);

        EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, -1);
        Operations.completeBlindly(new ClipboardHolder(clipboard).createPaste(editSession).ignoreAirBlocks(ignoreAir).to(paste).build());
        editSession.flushSession();
        return true;
    }

}
