/*
   This file is a part of the SteamWar software.

   Copyright (C) 2023  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.schematicsystem.autocheck;

import com.sk89q.jnbt.CompoundTag;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.block.BaseBlock;
import de.steamwar.schematicsystem.CheckSchemType;
import org.bukkit.Material;

import java.util.*;
import java.util.stream.Collectors;

public class AutoChecker15 implements AutoChecker.IAutoChecker {
    private static final Set<Material> INVENTORY = EnumSet.of(
            Material.BARREL,
            Material.BLAST_FURNACE,
            Material.BREWING_STAND,
            Material.CAMPFIRE,
            Material.CHEST,
            Material.DISPENSER,
            Material.DROPPER,
            Material.FURNACE,
            Material.HOPPER,
            Material.JUKEBOX,
            Material.SHULKER_BOX,
            Material.WHITE_SHULKER_BOX,
            Material.ORANGE_SHULKER_BOX,
            Material.MAGENTA_SHULKER_BOX,
            Material.LIGHT_BLUE_SHULKER_BOX,
            Material.YELLOW_SHULKER_BOX,
            Material.LIME_SHULKER_BOX,
            Material.PINK_SHULKER_BOX,
            Material.GRAY_SHULKER_BOX,
            Material.LIGHT_GRAY_SHULKER_BOX,
            Material.CYAN_SHULKER_BOX,
            Material.PURPLE_SHULKER_BOX,
            Material.BLUE_SHULKER_BOX,
            Material.BROWN_SHULKER_BOX,
            Material.GREEN_SHULKER_BOX,
            Material.RED_SHULKER_BOX,
            Material.BLACK_SHULKER_BOX,
            Material.SMOKER,
            Material.TRAPPED_CHEST);

    private static final Set<Material> FLOWERS = EnumSet.of(
            Material.CORNFLOWER,
            Material.POPPY,
            Material.FERN,
            Material.DANDELION,
            Material.BLUE_ORCHID,
            Material.ALLIUM,
            Material.AZURE_BLUET,
            Material.RED_TULIP,
            Material.ORANGE_TULIP,
            Material.WHITE_TULIP,
            Material.PINK_TULIP,
            Material.OXEYE_DAISY,
            Material.LILY_OF_THE_VALLEY,
            Material.WITHER_ROSE,
            Material.SUNFLOWER,
            Material.DIAMOND_HORSE_ARMOR,
            Material.IRON_HORSE_ARMOR,
            Material.GOLDEN_HORSE_ARMOR,
            Material.HONEY_BOTTLE);

    public AutoChecker.BlockScanResult scan(Clipboard clipboard) {
        AutoChecker.BlockScanResult result = new AutoChecker.BlockScanResult();
        BlockVector3 min = clipboard.getMinimumPoint();
        BlockVector3 max = clipboard.getMaximumPoint();
        for(int x = min.getBlockX(); x <= max.getBlockX(); x++){
            for(int y = min.getBlockY(); y <= max.getBlockY(); y++) {
                for (int z = min.getBlockZ(); z <= max.getBlockZ(); z++) {
                    final BaseBlock block = clipboard.getFullBlock(BlockVector3.at(x, y, z));
                    final Material material = Material.matchMaterial(block.getBlockType().getId());
                    if(material == null) {
                        continue;
                    }

                    result.getBlockCounts().merge(material, 1, Integer::sum);

                    if(INVENTORY.contains(material)) {
                        checkInventory(result, block, material, new BlockPos(x, y, z));
                    }

                    if(x == 0 || x == max.getBlockX() - 1 || y == max.getBlockY() - 1 || z == 0 || z == max.getBlockZ() - 1) {
                        result.getDesignBlocks().computeIfAbsent(material, m -> new ArrayList<>()).add(new BlockPos(x, y, z));
                    }
                }
            }
        }
        return result;
    }

    private static final Map<Material, Set<Material>> itemsInInv = new EnumMap<>(Material.class);

    static {
        itemsInInv.put(Material.BUCKET, EnumSet.of(Material.DISPENSER));
        itemsInInv.put(Material.TNT, EnumSet.of(Material.CHEST, Material.BARREL));
        itemsInInv.put(Material.FIRE_CHARGE, EnumSet.of(Material.DISPENSER));
        itemsInInv.put(Material.ARROW, EnumSet.of(Material.DISPENSER));
        FLOWERS.forEach(material -> itemsInInv.put(material, INVENTORY));
    }

    private void checkInventory(AutoChecker.BlockScanResult result, BaseBlock block, Material material, BlockPos pos) {
        CompoundTag nbt = block.getNbtData();
        if(nbt == null) {
            result.getDefunctNbt().add(pos);
            return;
        }


        if(material == Material.JUKEBOX && nbt.getValue().containsKey("RecordItem")){
            result.getRecords().add(pos);
            return;
        }

        List<CompoundTag> items = nbt.getList("Items", CompoundTag.class);
        if(items.isEmpty())
            return; //Leeres Inventar

        int counter = 0;
        for(CompoundTag item : items){
            if(!item.containsKey("id")){
                result.getDefunctNbt().add(pos);
                continue;
            }

            Material itemType = Material.matchMaterial(item.getString("id"));
            if(itemType == null) //Leere Slots
                continue;

            if (!itemsInInv.getOrDefault(itemType, EnumSet.noneOf(Material.class)).contains(material)) {
                result.getForbiddenItems().computeIfAbsent(pos, blockVector3 -> new HashSet<>()).add(itemType);
            } else if(material == Material.DISPENSER && (itemType == Material.ARROW || itemType == Material.FIRE_CHARGE)) {
                counter += item.getByte("Count");
            }
            if (item.containsKey("tag")) {
                result.getForbiddenNbt().computeIfAbsent(pos, blockVector3 -> new HashSet<>()).add(itemType);
            }
        }
        result.getDispenserItems().put(pos, counter);
    }

    @Override
    public AutoCheckerResult check(Clipboard clipboard, CheckSchemType type) {
        return AutoCheckerResult.builder()
                .type(type)
                .height(clipboard.getDimensions().getBlockY())
                .width(clipboard.getDimensions().getBlockX())
                .depth(clipboard.getDimensions().getBlockZ())
                .blockScanResult(scan(clipboard))
                .entities(clipboard.getEntities().stream().map(Entity::getLocation).map(blockVector3 -> new BlockPos(blockVector3.getBlockX(), blockVector3.getBlockY(), blockVector3.getBlockZ())).collect(Collectors.toList()))
                .build();
    }

    @Override
    public AutoCheckerResult sizeCheck(Clipboard clipboard, CheckSchemType type) {
        return AutoCheckerResult.builder()
                .type(type)
                .height(clipboard.getDimensions().getBlockY())
                .width(clipboard.getDimensions().getBlockX())
                .depth(clipboard.getDimensions().getBlockZ())
                .build();
    }
}
