/*
 *   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.features.attributescopy;

import de.steamwar.bausystem.BauSystem;
import de.steamwar.command.SWCommand;
import de.steamwar.linkage.Linked;
import org.bukkit.FluidCollisionMode;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.ArrayList;
import java.util.List;

@Linked
public class AttributesCopyCommand extends SWCommand {

    public AttributesCopyCommand() {
        super("copyattributes", "attributescopy", "ac");
    }

    @Register
    public void genericCommand(@Validator Player player) {
        Block block = player.getTargetBlockExact(8, FluidCollisionMode.ALWAYS);
        if (block == null) return;
        ItemStack mainHand = player.getInventory().getItemInMainHand();

        if (!(block.getType().isItem() && block.getType() == mainHand.getType() || isSame(block, mainHand))) {
            BauSystem.MESSAGE.send("ATTRIBUTES_CANT_COPY", player);
            return;
        }

        BlockData blockData = block.getBlockData();
        List<String> attributesToCopy = new ArrayList<>();
        if (block.getType() != mainHand.getType()) {
            attributesToCopy.add("§8-§7 Material§8:§7 " + block.getType().name());
        }
        AttributeUtils.copy(blockData, attributesToCopy);
        if (attributesToCopy.isEmpty()) {
            BauSystem.MESSAGE.send("ATTRIBUTES_NO_COPY", player);
            return;
        }
        ItemMeta itemMeta = mainHand.getItemMeta();
        List<String> lore = new ArrayList<>(attributesToCopy);
        lore.add(0, "§eAttributes§8:");
        itemMeta.setLore(lore);
        mainHand.setItemMeta(itemMeta);
        player.getInventory().setItemInMainHand(mainHand);
        BauSystem.MESSAGE.send("ATTRIBUTES_COPIED", player);
    }

    private boolean isSame(Block block, ItemStack itemStack) {
        if (itemStack.getType() == Material.REDSTONE && block.getType() == Material.REDSTONE_WIRE) return true;
        if (itemStack.getType() == Material.PLAYER_HEAD && block.getType() == Material.PLAYER_WALL_HEAD) return true;
        if (itemStack.getType() == Material.ZOMBIE_HEAD && block.getType() == Material.ZOMBIE_WALL_HEAD) return true;
        if (itemStack.getType() == Material.CREEPER_HEAD && block.getType() == Material.CREEPER_WALL_HEAD) return true;
        if (itemStack.getType() == Material.DRAGON_HEAD && block.getType() == Material.DRAGON_WALL_HEAD) return true;
        if (itemStack.getType() == Material.SKELETON_SKULL && block.getType() == Material.SKELETON_WALL_SKULL) {
            return true;
        }
        if (itemStack.getType() == Material.WITHER_SKELETON_SKULL && block.getType() == Material.WITHER_SKELETON_WALL_SKULL) {
            return true;
        }
        if (itemStack.getType() == Material.TORCH && block.getType() == Material.WALL_TORCH) return true;
        if (itemStack.getType() == Material.SOUL_TORCH && block.getType() == Material.SOUL_WALL_TORCH) return true;
        if (itemStack.getType() == Material.REDSTONE_TORCH && block.getType() == Material.REDSTONE_WALL_TORCH) {
            return true;
        }
        if (itemStack.getType() == Material.WHEAT_SEEDS && block.getType() == Material.WHEAT) return true;
        if (itemStack.getType().name().contains("_BANNER") && block.getType().name().contains("_WALL_BANNER")) {
            return true;
        }
        return false;
    }
}
