package com.husj.block.entity;

import com.husj.ModBlocks;
import com.husj.recipe.DisassemblerRecipes;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventories;
import net.minecraft.inventory.SimpleInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtList;
import net.minecraft.screen.NamedScreenHandlerFactory;
import net.minecraft.screen.PropertyDelegate;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.text.Text;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.registry.RegistryEntryLookup;
import net.minecraft.registry.RegistryWrapper;
import org.jetbrains.annotations.Nullable;

public class DisassemblerBlockEntity extends BlockEntity implements NamedScreenHandlerFactory, ImplementedInventory {
    private final DefaultedList<ItemStack> inventory = DefaultedList.ofSize(2, ItemStack.EMPTY);
    private static final int INPUT_SLOT = 0;
    private static final int OUTPUT_SLOT = 1;

    protected final PropertyDelegate propertyDelegate;
    private int progress = 0;
    private int maxProgress = 100;

    public DisassemblerBlockEntity(BlockPos pos, BlockState state) {
        super(ModBlocks.DISASSEMBLER_BLOCK_ENTITY, pos, state);
        this.propertyDelegate = new PropertyDelegate() {
            @Override
            public int get(int index) {
                return switch (index) {
                    case 0 -> DisassemblerBlockEntity.this.progress;
                    case 1 -> DisassemblerBlockEntity.this.maxProgress;
                    default -> 0;
                };
            }

            @Override
            public void set(int index, int value) {
                switch (index) {
                    case 0 -> DisassemblerBlockEntity.this.progress = value;
                    case 1 -> DisassemblerBlockEntity.this.maxProgress = value;
                }
            }

            @Override
            public int size() {
                return 2;
            }
        };
    }

    @Override
    public DefaultedList<ItemStack> getItems() {
        return inventory;
    }

    @Override
    public void markDirty() {
        if (this.world != null) {
            BlockState state = this.world.getBlockState(this.pos);
            this.world.markDirty(this.pos);
            this.world.updateListeners(this.pos, state, state, Block.NOTIFY_ALL);
        }
    }

    @Override
    public Text getDisplayName() {
        return Text.translatable("container.husj.disassembler");
    }

    @Nullable
    @Override
    public ScreenHandler createMenu(int syncId, PlayerInventory playerInventory, PlayerEntity player) {
        return new com.husj.screen.DisassemblerScreenHandler(syncId, playerInventory, this, this.propertyDelegate);
    }


    public static void tick(World world, BlockPos pos, BlockState state, DisassemblerBlockEntity blockEntity) {
        if (world.isClient()) {
            return;
        }

        if (hasRecipe(blockEntity)) {
            blockEntity.progress++;
            blockEntity.markDirty();
            if (blockEntity.progress >= blockEntity.maxProgress) {
                craftItem(blockEntity);
            }
        } else {
            blockEntity.resetProgress();
            blockEntity.markDirty();
        }
    }

    private void resetProgress() {
        this.progress = 0;
    }

    private static boolean hasRecipe(DisassemblerBlockEntity entity) {
        SimpleInventory inventory = new SimpleInventory(entity.size());
        for (int i = 0; i < entity.size(); i++) {
            inventory.setStack(i, entity.getStack(i));
        }

        // 检查输入槽是否有可拆解的工具
        ItemStack inputStack = entity.getStack(INPUT_SLOT);
        if (inputStack.isEmpty()) {
            return false;
        }

        // 检查输出槽是否为空
        ItemStack outputStack = entity.getStack(OUTPUT_SLOT);
        if (!outputStack.isEmpty()) {
            return false;
        }

        // 检查是否为可拆解的工具
        return DisassemblerRecipes.hasRecipe(inputStack);
    }

    private static void craftItem(DisassemblerBlockEntity entity) {
        SimpleInventory inventory = new SimpleInventory(entity.size());
        for (int i = 0; i < entity.size(); i++) {
            inventory.setStack(i, entity.getStack(i));
        }

        ItemStack inputStack = entity.getStack(INPUT_SLOT);
        ItemStack result = DisassemblerRecipes.getRecipeResult(inputStack);

        if (!result.isEmpty()) {
            entity.setStack(OUTPUT_SLOT, new ItemStack(result.getItem(), result.getCount()));
            entity.getStack(INPUT_SLOT).decrement(1);
            entity.resetProgress();
        }
    }
}