package com.husj.block;

import com.husj.block.entity.DisassemblerBlockEntity;
import com.mojang.serialization.MapCodec;
import net.minecraft.block.*;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.ActionResult;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class DisassemblerBlock extends BlockWithEntity {

    public DisassemblerBlock(Settings settings) {
        super(settings);
    }

    @Override
    protected MapCodec<? extends BlockWithEntity> getCodec() {
        return null;
    }

    @Override
    public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
        return new DisassemblerBlockEntity(pos, state);
    }

    @Override
    public BlockRenderType getRenderType(BlockState state) {
        return BlockRenderType.MODEL;
    }

    @Override
    public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
        if (!world.isClient()) {
            // 获取玩家主手的物品
            net.minecraft.item.ItemStack heldItem = player.getMainHandStack();

            // 检查是否可以拆解
            if (!heldItem.isEmpty() && com.husj.recipe.DisassemblerRecipes.hasRecipe(heldItem)) {

                // 检查是否为下界合金装备（特殊处理）
                if (isNetheriteItem(heldItem)) {
                    disassembleNetheriteItem(world, pos, player, heldItem);
                    return ActionResult.SUCCESS;
                }

                // 普通拆解逻辑
                net.minecraft.item.ItemStack result = com.husj.recipe.DisassemblerRecipes.getRecipeResult(heldItem);

                if (!result.isEmpty()) {
                    // 在方块上方生成掉落物
                    spawnItemDrop(world, pos, result.copy());

                    // 消耗玩家手中的物品
                    if (!player.isCreative()) {
                        heldItem.decrement(1);
                    }

                    // 播放音效
                    world.playSound(null, pos, net.minecraft.sound.SoundEvents.BLOCK_ANVIL_USE,
                            net.minecraft.sound.SoundCategory.BLOCKS, 1.0F, 1.0F);

                    return ActionResult.SUCCESS;
                }
            }
        }

        return ActionResult.CONSUME;
    }

    private boolean isNetheriteItem(net.minecraft.item.ItemStack stack) {
        net.minecraft.item.Item item = stack.getItem();
        return item == net.minecraft.item.Items.NETHERITE_SWORD ||
                item == net.minecraft.item.Items.NETHERITE_PICKAXE ||
                item == net.minecraft.item.Items.NETHERITE_AXE ||
                item == net.minecraft.item.Items.NETHERITE_SHOVEL ||
                item == net.minecraft.item.Items.NETHERITE_HOE ||
                item == net.minecraft.item.Items.NETHERITE_HELMET ||
                item == net.minecraft.item.Items.NETHERITE_CHESTPLATE ||
                item == net.minecraft.item.Items.NETHERITE_LEGGINGS ||
                item == net.minecraft.item.Items.NETHERITE_BOOTS;
    }

    private void disassembleNetheriteItem(World world, BlockPos pos, PlayerEntity player,
            net.minecraft.item.ItemStack heldItem) {
        net.minecraft.item.Item item = heldItem.getItem();

        // 1. 下界合金升级模板
        spawnItemDrop(world, pos,
                new net.minecraft.item.ItemStack(net.minecraft.item.Items.NETHERITE_UPGRADE_SMITHING_TEMPLATE));

        // 2. 对应的钻石工具/装备
        net.minecraft.item.Item diamondItem = getDiamondEquivalent(item);
        if (diamondItem != null) {
            spawnItemDrop(world, pos, new net.minecraft.item.ItemStack(diamondItem));
        }

        // 3. 下界合金锭
        spawnItemDrop(world, pos, new net.minecraft.item.ItemStack(net.minecraft.item.Items.NETHERITE_INGOT));

        // 消耗玩家手中的物品
        if (!player.isCreative()) {
            heldItem.decrement(1);
        }

        // 播放音效
        world.playSound(null, pos, net.minecraft.sound.SoundEvents.BLOCK_ANVIL_USE,
                net.minecraft.sound.SoundCategory.BLOCKS, 1.0F, 1.0F);
    }

    private net.minecraft.item.Item getDiamondEquivalent(net.minecraft.item.Item netheriteItem) {
        if (netheriteItem == net.minecraft.item.Items.NETHERITE_SWORD)
            return net.minecraft.item.Items.DIAMOND_SWORD;
        if (netheriteItem == net.minecraft.item.Items.NETHERITE_PICKAXE)
            return net.minecraft.item.Items.DIAMOND_PICKAXE;
        if (netheriteItem == net.minecraft.item.Items.NETHERITE_AXE)
            return net.minecraft.item.Items.DIAMOND_AXE;
        if (netheriteItem == net.minecraft.item.Items.NETHERITE_SHOVEL)
            return net.minecraft.item.Items.DIAMOND_SHOVEL;
        if (netheriteItem == net.minecraft.item.Items.NETHERITE_HOE)
            return net.minecraft.item.Items.DIAMOND_HOE;
        if (netheriteItem == net.minecraft.item.Items.NETHERITE_HELMET)
            return net.minecraft.item.Items.DIAMOND_HELMET;
        if (netheriteItem == net.minecraft.item.Items.NETHERITE_CHESTPLATE)
            return net.minecraft.item.Items.DIAMOND_CHESTPLATE;
        if (netheriteItem == net.minecraft.item.Items.NETHERITE_LEGGINGS)
            return net.minecraft.item.Items.DIAMOND_LEGGINGS;
        if (netheriteItem == net.minecraft.item.Items.NETHERITE_BOOTS)
            return net.minecraft.item.Items.DIAMOND_BOOTS;
        return null;
    }

    private void spawnItemDrop(World world, BlockPos pos, net.minecraft.item.ItemStack stack) {
        net.minecraft.entity.ItemEntity itemEntity = new net.minecraft.entity.ItemEntity(
                world,
                pos.getX() + 0.5,
                pos.getY() + 1.0,
                pos.getZ() + 0.5,
                stack);
        itemEntity.setToDefaultPickupDelay();
        world.spawnEntity(itemEntity);
    }

}
