/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2024 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.core;

import com.fastasyncworldedit.core.extent.clipboard.io.FastSchematicReaderV2;
import com.sk89q.jnbt.NBTInputStream;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.extent.clipboard.io.BuiltInClipboardFormat;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardWriter;
import com.sk89q.worldedit.extent.clipboard.io.MCEditSchematicReader;
import com.sk89q.worldedit.extent.clipboard.io.sponge.SpongeSchematicV2Reader;
import com.sk89q.worldedit.extent.clipboard.io.sponge.SpongeSchematicV3Reader;
import com.sk89q.worldedit.math.Vector3;
import com.sk89q.worldedit.math.transform.Transform;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.session.ClipboardHolder;
import de.steamwar.sql.NodeData;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import org.enginehub.linbus.stream.LinBinaryIO;

import java.io.*;

public class WorldEditWrapper21 implements WorldEditWrapper {

    @Override
    public InputStream getPlayerClipboard(Player player) {
        return WorldEditWrapper.getPlayerClipboard(player, (outputStream, clipboard, clipboardHolder) -> {
            ClipboardWriter writer = BuiltInClipboardFormat.FAST_V3.getWriter(outputStream);
            writer.write(clipboard);
            writer.close();
        });
    }

    @Override
    public void setPlayerClipboard(Player player, InputStream is, NodeData.SchematicFormat schemFormat) {
        Clipboard clipboard = null;
        try {
            clipboard = getClipboard(is, schemFormat);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }

        if (clipboard == null)
            throw new SecurityException("Clipboard is null");

        Actor actor = WorldEditWrapper.getWorldEditPlugin().wrapCommandSender(player);
        WorldEditWrapper.getWorldEditPlugin().getWorldEdit().getSessionManager().get(actor).setClipboard(new ClipboardHolder(clipboard));
    }

    @Override
    @SuppressWarnings("removal")
    public Clipboard getClipboard(InputStream is, NodeData.SchematicFormat schemFormat) throws IOException {
        return switch (schemFormat) {
            case MCEDIT -> new MCEditSchematicReader(new NBTInputStream(is)).read();
            case SPONGE_V2 -> new SpongeSchematicV2Reader(LinBinaryIO.read(new DataInputStream(is))).read();
            case SPONGE_V3 -> new SpongeSchematicV3Reader(LinBinaryIO.read(new DataInputStream(is))).read();
        };
    }

    @Override
    public org.bukkit.util.Vector getOrigin(Clipboard clipboard) {
        return new org.bukkit.util.Vector(clipboard.getOrigin().x(), clipboard.getOrigin().y(), clipboard.getOrigin().z());
    }

    @Override
    public Vector getMinimum(Region region) {
        return new Vector(region.getMinimumPoint().x(), region.getMinimumPoint().y(), region.getMinimumPoint().z());
    }

    @Override
    public Vector getMaximum(Region region) {
        return new Vector(region.getMaximumPoint().x(), region.getMaximumPoint().y(), region.getMaximumPoint().z());
    }

    @Override
    public Vector applyTransform(Vector vector, Transform transform) {
        Vector3 v = Vector3.at(vector.getX(), vector.getY(), vector.getZ());
        v = transform.apply(v);
        return new org.bukkit.util.Vector(v.x(), v.y(), v.z());
    }

    @Override
    public NodeData.SchematicFormat getNativeFormat() {
        return NodeData.SchematicFormat.SPONGE_V3;
    }
}
