package com.acethewildfire.acesbs.screen.custom;

import com.acethewildfire.acesbs.block.entity.custom.EntropicEvisceratorEntity;
import com.acethewildfire.acesbs.item.ModItems;
import com.acethewildfire.acesbs.screen.ModScreenHandlers;
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.screen.ArrayPropertyDelegate;
import net.minecraft.screen.PropertyDelegate;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.slot.Slot;
import net.minecraft.util.math.BlockPos;

public class EntropicEvisceratorScreenHandler extends ScreenHandler {
    private final Inventory inventory;
    private final PropertyDelegate propertyDelegate;
    public final EntropicEvisceratorEntity blockEntity;

    public EntropicEvisceratorScreenHandler(int syncId, PlayerInventory playerInventory, BlockPos pos) {
        this(syncId, playerInventory, playerInventory.player.getWorld().getBlockEntity(pos), new ArrayPropertyDelegate(4));
    }

    public EntropicEvisceratorScreenHandler(int syncId, PlayerInventory playerInventory, BlockEntity blockEntity, PropertyDelegate arrayPropertyDelegate) {
        super(ModScreenHandlers.ENTROPIC_EVISCERATOR_SCREEN_HANDLER, syncId);
        this.inventory = ((Inventory) blockEntity);
        this.blockEntity = ((EntropicEvisceratorEntity) blockEntity);
        this.propertyDelegate = arrayPropertyDelegate;

        this.addSlot(new Slot(inventory, 0, 80, 17)); //Item
        this.addSlot(new Slot(inventory, 1, 8, 17)); //Fuel
        this.addSlot(new Slot(inventory, 2, 80, 53)); // Output

        addPlayerInventory(playerInventory);
        addPlayerHotbar(playerInventory);

        addProperties(arrayPropertyDelegate);
    }

    public boolean isCrafting() {
        return propertyDelegate.get(2) > 0;
    }

    public boolean isCooking() {
        return propertyDelegate.get(0) > 0;
    }

//    public int getScaledArrowProgress() {
//        int progress = this.propertyDelegate.get(2);
//        int maxProgress = this.propertyDelegate.get(3); // Max Progress
//        int arrowPixelSize = 24; // This is the width in pixels of your arrow
//
//        return maxProgress != 0 && progress != 0 ? progress * arrowPixelSize / maxProgress : 0;
//    }

    public int getScaledEviscerateProgress() {
        int progress = this.propertyDelegate.get(2);
        int maxProgress = this.propertyDelegate.get(3); // Max Progress
        int evisceratePixelSize = 16; // This is the width in pixels of your bar

        return maxProgress != 0 && progress != 0 ? progress * evisceratePixelSize / maxProgress : 0;
    }

    @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();
            if (invSlot == 2) {
                if (!this.insertItem(originalStack, 3, 39, true)) {
                    return ItemStack.EMPTY;
                }

                slot.onQuickTransfer(originalStack, newStack);
            } else if (invSlot != 1 && invSlot != 0) {
                if (this.isEviscerateable(originalStack)) {
                    if (!this.insertItem(originalStack, 0, 1, false)) {
                        return ItemStack.EMPTY;
                    }
                } else if (this.isFuel(originalStack)) {
                    if (!this.insertItem(originalStack, 1, 2, false)) {
                        return ItemStack.EMPTY;
                    }
                } else if (invSlot >= 3 && invSlot < 30) {
                    if (!this.insertItem(originalStack, 30, 39, false)) {
                        return ItemStack.EMPTY;
                    }
                } else if (invSlot >= 30 && invSlot < 39 && !this.insertItem(originalStack, 3, 30, false)) {
                    return ItemStack.EMPTY;
                }
            } else if (!this.insertItem(originalStack, 3, 39, false)) {
                return ItemStack.EMPTY;
            }

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

            if (newStack.getCount() == originalStack.getCount()) {
                return ItemStack.EMPTY;
            }

            slot.onTakeItem(player, originalStack);
        }
        return newStack;
    }

    protected boolean isEviscerateable(ItemStack itemStack) {
        return itemStack.isOf(ModItems.RAW_ENTROPY) || itemStack.isOf(ModItems.RAW_ENDTROPY);
    }

    protected boolean isFuel(ItemStack itemStack) {
        return itemStack.isOf(ModItems.STABLE_ENTROPY);
    }

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

    private void addPlayerInventory(PlayerInventory playerInventory) {
        for (int i = 0; i < 3; ++i) {
            for (int l = 0; l < 9; ++l) {
                this.addSlot(new Slot(playerInventory, l + i * 9 + 9, 8 + l * 18, 84 + i * 18));
            }
        }
    }

    private void addPlayerHotbar(PlayerInventory playerInventory) {
        for (int i = 0; i < 9; ++i) {
            this.addSlot(new Slot(playerInventory, i, 8 + i * 18, 142));
        }
    }
}
