package jesse.keeblarcraft.GuiMgr;

import jesse.keeblarcraft.CustomBlocks.BlockEntities.FactionBlockEntity;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.screen.ArrayPropertyDelegate;
import net.minecraft.screen.PropertyDelegate;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.slot.Slot;

public class FactionBlockScreenHandler extends ScreenHandler {
    private final Inventory inventory;
    private final PropertyDelegate propertyDelegate;
    public final FactionBlockEntity blockEntity;
    public int factionPower = 0;

    public FactionBlockScreenHandler(int syncId, PlayerInventory inventory, PacketByteBuf buf) {
        this(syncId, inventory, inventory.player.getWorld().getBlockEntity(buf.readBlockPos()), new ArrayPropertyDelegate(7));
    }

    public FactionBlockScreenHandler(int syncId, PlayerInventory playerInventory, BlockEntity blockEntity, PropertyDelegate arrayPropertyDelegate) {
        super(ScreenHandlerRegistration.FACTION_BLOCK_SCREEN_HANDLER, syncId);
        checkSize((Inventory) blockEntity, 7);
        this.inventory = (Inventory) blockEntity;
        inventory.onOpen(playerInventory.player);
        this.propertyDelegate = arrayPropertyDelegate;
        this.blockEntity = (FactionBlockEntity) blockEntity;

        // Need a better way of storing these coordinates...
        this.addSlot(new Slot(inventory, 0, -1, 2)); // top row
        this.addSlot(new Slot(inventory, 2, 35, 2));
        this.addSlot(new Slot(inventory, 4, 71, 2));

        this.addSlot(new Slot(inventory, 1, 17, 36));
        this.addSlot(new Slot(inventory, 3, 54, 36));
        this.addSlot(new Slot(inventory, 5, 90, 36));

        this.addSlot(new Slot(inventory, 6, 148, 20));

        addPlayerInventory(playerInventory);
        addPlayerHotbar(playerInventory);

        // Need to reference Kaupendim tutorial again; but we could theoretically just add the player inventory
        // right here so that they can drag items in and whatnot (I assume). I am unsure if I am taking that
        // direction with this block RIGHT NOW - so for now I am NOT doing that

        // Remainder stuff
        addProperties(arrayPropertyDelegate);
    }

    public Integer GetFactionPower() {
        factionPower = this.propertyDelegate.get(0);
        return factionPower;
    }

    // This is just for SHIFT+CLICK moving
    @Override
    public ItemStack quickMove(PlayerEntity player, int invSlot) {
        ItemStack newStack = ItemStack.EMPTY;
        Slot slot = this.slots.get(invSlot);
        if (slot != null && slot.hasStack()) {
            ItemStack originalStack = slot.getStack();
            newStack = originalStack.copy();

            // TODO: This is from the YT video and it looks atrocious. Refactor this later
            if ( (invSlot < this.inventory.size() && !this.insertItem(originalStack, this.inventory.size(), this.slots.size(), true) ) ||
                (!this.insertItem(originalStack, 0, this.inventory.size(), false))) {
                return ItemStack.EMPTY;
            }

            if (originalStack.isEmpty()) {
                slot.setStack(ItemStack.EMPTY);
            } else {
                slot.markDirty();;
            }
        }
        return newStack;
    }

    // From Kaupenjoe video
    private void addPlayerInventory(PlayerInventory playerInventory) {
        int spacing = -8;
        for (int i = 0; i < 3; ++i) { // Rows
            for (int l = 0; l < 9; ++l) { // Columns
                int index = l + i * 9 + 9; // l = col, i*9 = the row to be on (scaling by 9 bc slots are 1-(9*3) in amount), +9 = where on that row to be!
                int x = l * 22 + spacing; // Texture draw position on image
                int y = 89 + i * 22; // Texture draw position on image
                this.addSlot(new Slot(playerInventory, index, x, y));
            }
        }
    }

    // From Kaupenjoe video
    private void addPlayerHotbar(PlayerInventory playerInventory) {
        int spacing = -8;
        for (int i = 0; i < 9; ++i) {
            int index = i; // Index of hotbar (only 9 slots long in vanilla)
            // int x = (8 + i * 18) + spacing; // Texture draw position
            int x = i * 22 + spacing; // Texture draw position
            int y = 162; // Texture draw position
            this.addSlot(new Slot(playerInventory, index, x, y));
        }
    }

    @Override
    public boolean canUse(PlayerEntity player) {
        return this.inventory.canPlayerUse(player);
    }
}
