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

import com.sk89q.worldedit.extent.clipboard.Clipboard;
import de.steamwar.core.Core;
import de.steamwar.core.WorldEditWrapper;
import de.steamwar.sql.internal.SqlTypeMapper;
import de.steamwar.sql.internal.Statement;
import org.bukkit.entity.Player;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.sql.Blob;
import java.sql.PreparedStatement;
import java.util.zip.GZIPInputStream;

public class SchematicData {

    public static Clipboard clipboardFromStream(InputStream is, NodeData.SchematicFormat schemFormat) {
        try {
            return WorldEditWrapper.impl.getClipboard(is, schemFormat);
        } catch (IOException e) {
            throw new SecurityException("Could not read schem", e);
        }
    }

    private final NodeData data;

    public SchematicData(SchematicNode node) {
        this.data = NodeData.get(node);
        if(node.isDir())
            throw new SecurityException("Node is Directory");
    }

    public Clipboard load() throws IOException, NoClipboardException {
        return WorldEditWrapper.impl.getClipboard(data.schemData(), data.getNodeFormat());
    }

    public void loadToPlayer(Player player) throws IOException, NoClipboardException {
        WorldEditWrapper.impl.setPlayerClipboard(player, data.schemData(), data.getNodeFormat());
    }

    public void saveFromPlayer(Player player) throws IOException, NoClipboardException {
        data.saveFromStream(WorldEditWrapper.impl.getPlayerClipboard(player), WorldEditWrapper.impl.getNativeFormat());
    }

    @Deprecated
    public void saveFromBytes(byte[] bytes, NodeData.SchematicFormat newFormat) {
        data.saveFromStream(new ByteArrayInputStream(bytes), newFormat);
    }
}
