package hu.jgj52.pvpcore.Utils;

import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader;
import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.session.ClipboardHolder;
import com.sk89q.worldedit.world.block.BlockTypes;
import hu.jgj52.pvpcore.Main;
import org.bukkit.Location;
import org.bukkit.World;

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

public class SchematicManager {
    private final Main plugin;

    public SchematicManager(Main plugin) {
        this.plugin = plugin;
    }

    public boolean placeSchematic(World world, int x, int y, int z, String schematicName, boolean air) {
        File schematicFile = new File(plugin.getDataFolder() + File.separator + "schematics", schematicName + ".schem");

        if (!schematicFile.exists()) {
            plugin.getLogger().warning("Schematic file not found: " + schematicFile.getAbsolutePath());
            return false;
        }

        ClipboardFormat format = ClipboardFormats.findByFile(schematicFile);
        if (format == null) {
            plugin.getLogger().warning("Unknown schematic format for file: " + schematicFile.getAbsolutePath());
            return false;
        }

        try (FileInputStream fis = new FileInputStream(schematicFile);
             ClipboardReader reader = format.getReader(fis)) {

            Clipboard clipboard = reader.read();
            com.sk89q.worldedit.world.World weWorld = BukkitAdapter.adapt(world);

            try (EditSession editSession = WorldEdit.getInstance().newEditSession(weWorld)) {
                Operation operation = new ClipboardHolder(clipboard)
                        .createPaste(editSession)
                        .to(BlockVector3.at(x, y, z))
                        .ignoreAirBlocks(air)
                        .build();

                Operations.complete(operation);
                editSession.flushSession();
                return true;
            }
        } catch (IOException e) {
            plugin.getLogger().severe("Error reading schematic file: " + e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            plugin.getLogger().severe("Error placing schematic: " + e.getMessage());
            e.printStackTrace();
        }

        return false;
    }

    public boolean removeSchematic(World world, Location loc1, Location loc2) {
        try {
            com.sk89q.worldedit.world.World weWorld = BukkitAdapter.adapt(world);

            int minX = Math.min(loc1.getBlockX(), loc2.getBlockX());
            int maxX = Math.max(loc1.getBlockX(), loc2.getBlockX());
            int minY = Math.min(loc1.getBlockY(), loc2.getBlockY());
            int maxY = Math.max(loc1.getBlockY(), loc2.getBlockY());
            int minZ = Math.min(loc1.getBlockZ(), loc2.getBlockZ());
            int maxZ = Math.max(loc1.getBlockZ(), loc2.getBlockZ());

            BlockVector3 min = BlockVector3.at(minX, minY, minZ);
            BlockVector3 max = BlockVector3.at(maxX, maxY, maxZ);
            CuboidRegion region = new CuboidRegion(weWorld, min, max);

            try (EditSession editSession = WorldEdit.getInstance().newEditSession(weWorld)) {
                editSession.setBlocks(region, BlockTypes.AIR.getDefaultState());
                editSession.flushSession();
                return true;
            }
        } catch (Exception e) {
            plugin.getLogger().severe("Error removing schematic: " + e.getMessage());
            e.printStackTrace();
            return false;
        }
    }
}