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

import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.extent.clipboard.io.BuiltInClipboardFormat;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardWriter;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.mask.Mask2D;
import com.sk89q.worldedit.function.operation.ForwardExtentCopy;
import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.math.transform.AffineTransform;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.regions.selector.CuboidRegionSelector;
import com.sk89q.worldedit.session.ClipboardHolder;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockTypes;
import de.steamwar.bausystem.region.Point;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Waterlogged;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;

import javax.annotation.Nullable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiPredicate;
import java.util.logging.Level;

public class FlatteningWrapper {
    public static final FlatteningWrapper impl = new FlatteningWrapper();

    public boolean isNoBook(ItemStack item) {
        return item.getType() != Material.WRITABLE_BOOK && item.getType() != Material.WRITTEN_BOOK;
    }

    private static final Set<Material> unpushable = new HashSet<>(Arrays.asList(Material.BARRIER, Material.BEACON, Material.COMMAND_BLOCK, Material.CHAIN_COMMAND_BLOCK, Material.REPEATING_COMMAND_BLOCK, Material.ENCHANTING_TABLE, Material.END_GATEWAY, Material.END_PORTAL, Material.ENDER_CHEST, Material.GRINDSTONE, Material.JIGSAW, Material.JUKEBOX, Material.NETHER_PORTAL, Material.OBSIDIAN, Material.STRUCTURE_VOID, Material.BARREL, Material.BEEHIVE, Material.BEE_NEST, Material.BLAST_FURNACE, Material.BREWING_STAND, Material.CHEST, Material.DAYLIGHT_DETECTOR, Material.DISPENSER, Material.DROPPER, Material.FURNACE, Material.HOPPER, Material.LECTERN, Material.SMOKER, Material.TRAPPED_CHEST));

    // TODO: FLOWER
    private static final Set<Material> breaking = new HashSet<>(Arrays.asList(Material.BAMBOO, Material.CACTUS, Material.CAKE, Material.CARVED_PUMPKIN, Material.CHORUS_FLOWER, Material.CHORUS_PLANT, Material.COBWEB, Material.COCOA, Material.DRAGON_EGG, Material.FIRE, Material.FLOWER_POT, Material.JACK_O_LANTERN, Material.LADDER, Material.LAVA, Material.LAVA, Material.LEVER, Material.LILY_PAD, Material.MELON, Material.NETHER_WART, Material.PUMPKIN, Material.COMPARATOR, Material.REDSTONE_WIRE, Material.REPEATER, Material.TORCH, Material.STRUCTURE_VOID, Material.SCAFFOLDING, Material.SEA_PICKLE, Material.SNOW, Material.SUGAR_CANE, Material.TORCH, Material.TRIPWIRE, Material.TRIPWIRE_HOOK, Material.TURTLE_EGG, Material.VINE, Material.WATER, Material.WHEAT));

    public boolean isUnpusheable(Material material) {
        if (unpushable.contains(material)) {
            return true;
        }
        String name = material.name();
        return name.contains("BANNER") || name.contains("SIGN");
    }

    public boolean isBreakingOnPush(Material material) {
        if (breaking.contains(material)) {
            return true;
        }
        String name = material.name();
        return name.contains("BED") || name.contains("BUTTON") || name.contains("CARPET") || (name.contains("DOOR") && !name.contains("TRAPDOOR")) || name.contains("HEAD") || name.contains("LEAVES") || name.contains("MUSHROOM") || name.contains("PRESSURE_PLATE") || name.contains("SHULKER_BOX");
    }

    public boolean isWorldEditCommand(String command) {
        if (command.startsWith("/")) {
            command = command.replaceFirst("/", "");
        }
        command = command.toLowerCase();
        return WorldEdit.getInstance().getPlatformManager().getPlatformCommandManager().getCommandManager().containsCommand(command);
    }

    private static final WorldEditPlugin WORLDEDIT_PLUGIN = Objects.requireNonNull((WorldEditPlugin) Bukkit.getPluginManager().getPlugin("WorldEdit"));
    private static final BukkitWorld BUKKITWORLD = new BukkitWorld(Bukkit.getWorlds().get(0));

    public void setSelection(Player p, Point minPoint, Point maxPoint) {
        WORLDEDIT_PLUGIN.getSession(p).setRegionSelector(BUKKITWORLD, new CuboidRegionSelector(BUKKITWORLD, minPoint.toBlockVector3(), maxPoint.toBlockVector3()));
    }

    public Clipboard loadSchematic(File file) {
        Clipboard clipboard;
        try (ClipboardReader reader = Objects.requireNonNull(ClipboardFormats.findByFile(file)).getReader(new FileInputStream(file))) {
            clipboard = reader.read();
        } catch (NullPointerException | IOException e) {
            throw new SecurityException("Bausystem schematic not found", e);
        }
        return clipboard;
    }

    public EditSession paste(PasteBuilder pasteBuilder) {
        try (EditSession e = WorldEdit.getInstance().getEditSessionFactory().getEditSession(new BukkitWorld(Bukkit.getWorlds().get(0)), -1)) {
            Clipboard clipboard = pasteBuilder.getClipboard();

            if (!pasteBuilder.getMappers().isEmpty()) {
                BlockVector3 minimum = clipboard.getRegion().getMinimumPoint();
                for (int x = 0; x < clipboard.getDimensions().getX(); x++) {
                    for (int y = 0; y < clipboard.getDimensions().getY(); y++) {
                        for (int z = 0; z < clipboard.getDimensions().getZ(); z++) {
                            BlockVector3 pos = minimum.add(x, y, z);
                            pasteBuilder.getMappers().forEach(mapper -> mapper.accept(clipboard, pos));
                        }
                    }
                }
            }

            AtomicReference<BlockVector3> pastePoint = new AtomicReference<>();
            if (!pasteBuilder.getPredicates().isEmpty()) {
                e.setMask(new Mask() {
                    @Override
                    public boolean test(BlockVector3 blockVector3) {
                        BaseBlock block = clipboard.getFullBlock(blockVector3.subtract(pastePoint.get()).add(clipboard.getRegion().getMinimumPoint()));
                        String blockName = block.getBlockType().toString().toLowerCase();
                        for (BiPredicate<BaseBlock, String> predicate : pasteBuilder.getPredicates()) {
                            if (!predicate.test(block, blockName)) return false;
                        }
                        return true;
                    }

                    public Mask copy() {
                        return this;
                    }

                    @Nullable
                    @Override
                    public Mask2D toMask2D() {
                        return null;
                    }
                });
            }

            ClipboardHolder ch = new ClipboardHolder(clipboard);
            BlockVector3 dimensions = clipboard.getDimensions();
            BlockVector3 v = BlockVector3.at(pasteBuilder.getPastPoint().getX(), pasteBuilder.getPastPoint().getY(), pasteBuilder.getPastPoint().getZ());
            BlockVector3 offset = clipboard.getRegion().getMinimumPoint().subtract(clipboard.getOrigin());
            if (pasteBuilder.isRotate()) {
                ch.setTransform(new AffineTransform().rotateY(180));
                v = v.add(dimensions.getX() / 2, 0, dimensions.getZ() / 2).subtract(offset.multiply(-1, 1, -1)).subtract(0, 0, 1);
            } else {
                v = v.subtract(dimensions.getX() / 2, 0, dimensions.getZ() / 2).subtract(offset);
            }
            pastePoint.set(v);

            if (pasteBuilder.isReset()) {
                e.setBlocks((Region) new CuboidRegion(pasteBuilder.getMinPoint().toBlockVector3(), pasteBuilder.getMaxPoint().toBlockVector3()), Objects.requireNonNull(BlockTypes.AIR).getDefaultState().toBaseBlock());
                if (pasteBuilder.getWaterLevel() != 0) {
                    e.setBlocks((Region) new CuboidRegion(pasteBuilder.getMinPoint().toBlockVector3(), pasteBuilder.getMaxPoint().toBlockVector3().withY(pasteBuilder.getWaterLevel())), Objects.requireNonNull(BlockTypes.WATER).getDefaultState().toBaseBlock());
                }
            }
            Operations.completeBlindly(ch.createPaste(e).to(v).ignoreAirBlocks(pasteBuilder.isIgnoreAir()).build());
            return e;
        } catch (WorldEditException e) {
            throw new SecurityException(e.getMessage(), e);
        }
    }

    public Clipboard copy(Point minPoint, Point maxPoint, Point copyPoint) {
        BukkitWorld bukkitWorld = new BukkitWorld(Bukkit.getWorlds().get(0));
        CuboidRegion region = new CuboidRegion(bukkitWorld, minPoint.toBlockVector3(), maxPoint.toBlockVector3());
        BlockArrayClipboard clipboard = new BlockArrayClipboard(region);
        try (EditSession e = WorldEdit.getInstance().getEditSessionFactory().getEditSession(bukkitWorld, -1)) {
            ForwardExtentCopy copy = new ForwardExtentCopy(
                    e, region, clipboard, region.getMinimumPoint()
            );

            copy.setCopyingEntities(false);
            copy.setCopyingBiomes(false);

            Operations.complete(copy);
            clipboard.setOrigin(copyPoint.toBlockVector3());
            return clipboard;
        } catch (WorldEditException e) {
            Bukkit.getLogger().log(Level.SEVERE, e.getMessage(), e);
            return null;
        }
    }

    public boolean backup(Point minPoint, Point maxPoint, File file) {
        Clipboard clipboard = copy(minPoint, maxPoint, minPoint);
        try (ClipboardWriter writer = BuiltInClipboardFormat.SPONGE_SCHEMATIC.getWriter(new FileOutputStream(file))) {
            writer.write(clipboard);
            return true;
        } catch (IOException e) {
            Bukkit.getLogger().log(Level.SEVERE, e.getMessage(), e);
            return false;
        }
    }

    public boolean inWater(org.bukkit.World world, Vector tntPosition) {
        Block block = world.getBlockAt(tntPosition.getBlockX(), tntPosition.getBlockY(), tntPosition.getBlockZ());
        if (block.getType() == Material.WATER) return true;

        BlockData data = block.getBlockData();
        if (!(data instanceof Waterlogged)) return false;

        return ((Waterlogged) data).isWaterlogged();
    }
}
