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

import de.steamwar.bausystem.world.Detoloader;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;

import java.util.List;
import java.util.stream.Collectors;

public class FlatteningWrapper12 implements FlatteningWrapper.IFlatteningWrapper {

    @Override
    public boolean tntPlaceActionPerform(Location location) {
        Material m = location.getBlock().getType();
        if (m != Material.AIR && m != Material.STATIONARY_WATER && m != Material.WATER)
            return false;

        location.getBlock().setType(Material.TNT);
        return true;
    }

    @Override
    @SuppressWarnings("deprecation")
    public boolean setRedstone(Location location, boolean active) {
        Block block = location.getBlock();
        Material material = block.getType();
        if (material == Material.LEVER || material == Material.STONE_BUTTON || material == Material.WOOD_BUTTON) {
            if (active)
                block.setData((byte) (block.getData() | 8));
            else
                block.setData((byte) (block.getData() & -9));
        } else if (material == Material.STONE_PLATE || material == Material.WOOD_PLATE) {
            if (active)
                block.setData((byte) 1);
            else
                block.setData((byte) 0);
        } else if (material == Material.TRIPWIRE) {
            if (active) {
                ArmorStand armorStand = (ArmorStand) Bukkit.getWorlds().get(0).spawnEntity(location, EntityType.ARMOR_STAND);
                armorStand.setVisible(false);
                armorStand.setBasePlate(false);
                armorStand.addScoreboardTag("detonator-" + location.getBlockX() + location.getBlockY() + location.getBlockZ());
            } else {
                List<Entity> entityList = Bukkit.getWorlds().get(0).getEntitiesByClasses(ArmorStand.class).stream().filter(entity ->
                                entity.getScoreboardTags().contains("detonator-" + location.getBlockX() + location.getBlockY() + location.getBlockZ()))
                        .limit(1)
                        .collect(Collectors.toList());
                if (entityList.isEmpty()) return false;
                entityList.get(0).remove();
            }
        } else {
            return false;
        }
        block.getState().update(true);
        return true;
    }

    @Override
    @SuppressWarnings("deprecation")
    public Detoloader onPlayerInteractLoader(PlayerInteractEvent event) {
        Block block = event.getClickedBlock();
        Material material = block.getType();
        if (material == Material.LEVER) {
            if ((block.getData() & 8) == 8) {
                return new Detoloader("Hebel", 0).setActive(false);
            } else {
                return new Detoloader("Hebel", 0).setActive(true);
            }
        } else if (material == Material.STONE_BUTTON) {
            return new Detoloader("Knopf", Detoloader.STONE_BUTTON);
        } else if (material == Material.WOOD_BUTTON) {
            return new Detoloader("Knopf", Detoloader.WOODEN_BUTTON);
        } else if (material == Material.NOTE_BLOCK) {
            return new Detoloader("Noteblock", Detoloader.NOTE_BLOCK);
        } else if (material == Material.STONE_PLATE || material == Material.WOOD_PLATE) {
            return new Detoloader("Druckplatte", Detoloader.PRESSURE_PLATE);
        } else if (material == Material.TRIPWIRE) {
            return new Detoloader("Tripwire", Detoloader.TRIPWIRE);
        }
        return new Detoloader("§eUnbekannter Block betätigt (nicht aufgenommen)", -1).setAddBack(false);
    }

    @Override
    @SuppressWarnings("deprecation")
    public boolean getLever(Block block) {
        return (block.getData() & 8) == 8;
    }

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

    @Override
    public boolean inWater(World world, Vector tntPosition) {
        Material material = world.getBlockAt(tntPosition.getBlockX(), tntPosition.getBlockY(), tntPosition.getBlockZ()).getType();
        return material == Material.WATER || material == Material.STATIONARY_WATER;
    }

    @Override
    public Material getTraceShowMaterial() {
        return Material.CONCRETE;
    }

    @Override
    public Material getTraceHideMaterial() {
        return Material.CONCRETE;
    }

    @Override
    public Material getTraceXZMaterial() {
        return Material.STEP;
    }

    @Override
    public void giveStick(Player player) {
        player.sendMessage(BauSystem.PREFIX + "§cDen Debugstick gibt es nicht in der 1.12.");
    }
}
