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

import com.sk89q.worldedit.EmptyClipboardException;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.session.ClipboardHolder;
import de.steamwar.sql.NoClipboardException;
import com.sk89q.worldedit.math.transform.Transform;
import com.sk89q.worldedit.regions.Region;
import de.steamwar.sql.NodeData;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;

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

public interface WorldEditWrapper {
    WorldEditWrapper impl = VersionDependent.getVersionImpl(Core.getInstance());

    InputStream getPlayerClipboard(Player player);
    void setPlayerClipboard(Player player, InputStream is, NodeData.SchematicFormat schemFormat);
    Clipboard getClipboard(InputStream is, NodeData.SchematicFormat schemFormat) throws IOException;

    Vector getOrigin(Clipboard clipboard);
    Vector getMinimum(Region region);
    Vector getMaximum(Region region);
    Vector applyTransform(Vector vector, Transform transform);

    NodeData.SchematicFormat getNativeFormat();

    static WorldEditPlugin getWorldEditPlugin() {
        return (WorldEditPlugin) Bukkit.getPluginManager().getPlugin("WorldEdit");
    }

    static InputStream getPlayerClipboard(Player player, SchematicWriter consumer) {
        ClipboardHolder clipboardHolder;
        try {
            clipboardHolder = WorldEditWrapper.getWorldEditPlugin().getSession(player).getClipboard();
        } catch (EmptyClipboardException e) {
            throw new NoClipboardException();
        }

        Clipboard clipboard = clipboardHolder.getClipboard();
        if(clipboard == null)
            throw new NoClipboardException();

        PipedOutputStream outputStream = new PipedOutputStream();
        PipedInputStream inputStream;
        try {
            inputStream = new PipedInputStream(outputStream, 4096);
        } catch (IOException e) {
            throw new SecurityException("Could not init piped input stream", e);
        }

        new Thread(() -> {
            try {
                consumer.write(outputStream, clipboard, clipboardHolder);
            } catch (Exception e) {
                Core.getInstance().getLogger().log(Level.SEVERE, "Could not write schematic", e);
            }
            try {
                outputStream.close();
            } catch (IOException e) {
                Core.getInstance().getLogger().log(Level.SEVERE, "Could not close schem writer", e);
            }
        }, "SchemWriter").start();

        return inputStream;
    }

    interface SchematicWriter {
        void write(OutputStream outputStream, Clipboard clipboard, ClipboardHolder holder) throws IOException;
    }
}
