/*
 * Copyright (c) 2026 盐灯水母
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

package com.remains_cave.item.menu;

import com.remains_cave.item.ModDataComponents;
import com.remains_cave.item.component.QuiverContent;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.items.ItemStackHandler;
import net.neoforged.neoforge.items.SlotItemHandler;
import org.jetbrains.annotations.NotNull;

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

public class QuiverMenu extends AbstractContainerMenu {
    private final ItemStack quiverStack;
    private final ItemStackHandler quiverInventory;

    // 客户端构造（无 quiverStack）
    public QuiverMenu(int containerId, Inventory playerInventory) {
        this(containerId, playerInventory, ItemStack.EMPTY);
    }

    // 服务端构造
    public QuiverMenu(int containerId, Inventory playerInventory, ItemStack quiverStack) {
        super(ModMenus.QUIVER_MENU.get(), containerId);
        this.quiverStack = quiverStack;
        this.quiverInventory = new ItemStackHandler(QuiverContent.MAX_SLOTS) {
            @Override
            protected void onContentsChanged(int slot) {
                if (!quiverStack.isEmpty()) {
                    List<ItemStack> arrows = new ArrayList<>();
                    for (int i = 0; i < QuiverContent.MAX_SLOTS; i++) {
                        arrows.add(quiverInventory.getStackInSlot(i));
                    }
                    QuiverContent content = new QuiverContent(arrows);
                    quiverStack.set(ModDataComponents.QUIVER_CONTENT.get(), content);
                }
            }
        };

        // 从箭袋的 DataComponent 加载初始内容
        if (!quiverStack.isEmpty()) {
            QuiverContent content = quiverStack.get(ModDataComponents.QUIVER_CONTENT.get());
            if (content != null) {
                List<ItemStack> arrows = content.arrows();
                for (int i = 0; i < arrows.size() && i < QuiverContent.MAX_SLOTS; i++) {
                    quiverInventory.setStackInSlot(i, arrows.get(i).copy()); // 确保拷贝，避免引用问题
                }
            }
        }

        // 添加箭袋的 3 个槽位
        for (int i = 0; i < QuiverContent.MAX_SLOTS; i++) {
            addSlot(new SlotItemHandler(quiverInventory, i, 62 + i * 18, 20) {
                @Override
                public boolean mayPlace(@NotNull ItemStack stack) {
                    // 只允许箭
                    return stack.is(net.minecraft.tags.ItemTags.ARROWS);
                }
            });
        }

        // 添加玩家背包槽位 (3行9列)
        for (int row = 0; row < 3; ++row) {
            for (int col = 0; col < 9; ++col) {
                addSlot(new Slot(playerInventory, col + row * 9 + 9, 8 + col * 18, 51 + row * 18));
            }
        }
        // 添加玩家快捷栏槽位
        for (int col = 0; col < 9; ++col) {
            addSlot(new Slot(playerInventory, col, 8 + col * 18, 109));
        }
    }

    @Override
    public @NotNull ItemStack quickMoveStack(@NotNull Player player, int index) {
        Slot slot = slots.get(index);
        if (!slot.hasItem()) return ItemStack.EMPTY;

        ItemStack stack = slot.getItem();
        ItemStack original = stack.copy();

        if (index < QuiverContent.MAX_SLOTS) {
            // 从箭袋槽位移出到玩家背包
            if (!moveItemStackTo(stack, QuiverContent.MAX_SLOTS, slots.size(), true)) {
                return ItemStack.EMPTY;
            }
        } else {
            // 从玩家背包移到箭袋槽位
            if (!moveItemStackTo(stack, 0, QuiverContent.MAX_SLOTS, false)) {
                return ItemStack.EMPTY;
            }
        }

        if (stack.isEmpty()) {
            slot.set(ItemStack.EMPTY);
        } else {
            slot.setChanged();
        }

        return original;
    }

    @Override
    public boolean stillValid(@NotNull Player player) {
        return true;
    }
}