package com.acethewildfire.acesbs.block.entity.custom;

import com.acethewildfire.acesbs.block.custom.EntropicStabilizer;
import com.acethewildfire.acesbs.block.entity.ImplementedInventory;
import com.acethewildfire.acesbs.block.entity.ModBlockEntities;
import com.acethewildfire.acesbs.screen.custom.EntropicStabilizerScreenHandler;
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory;
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.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.listener.ClientPlayPacketListener;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.BlockEntityUpdateS2CPacket;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.MathHelper;
import org.jetbrains.annotations.Nullable;

public class EntropicStabilizerEntity extends BlockEntity implements ImplementedInventory, ExtendedScreenHandlerFactory<BlockPos> {
    private final DefaultedList<ItemStack> inventory = DefaultedList.ofSize(3, ItemStack.EMPTY);
    private Direction facing;

    public EntropicStabilizerEntity(BlockPos pos, BlockState state) {
        super(ModBlockEntities.E_STABILIZER_BE, pos, state);
        this.facing = state.get(EntropicStabilizer.FACING);
    }

    public Direction getFacing() {
        return facing;
    }

    public void setFacing(Direction newFacing) {
        this.facing = newFacing;
        // sync to blockstate for rendering
        if (world != null && !world.isClient) {
            world.setBlockState(pos, getCachedState().with(EntropicStabilizer.FACING, newFacing), 3);
            markDirty();
        }
    }

    @Override
    protected void writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) {
        super.writeNbt(nbt, registryLookup);
        Inventories.writeNbt(nbt, inventory, registryLookup);
        nbt.putInt("Facing", facing.getId());
    }

    @Override
    protected void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) {
        super.readNbt(nbt, registryLookup);
        Inventories.readNbt(nbt, inventory, registryLookup);
        this.facing = Direction.byId(nbt.getInt("Facing"));
    }

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

    @Override
    public BlockPos getScreenOpeningData(ServerPlayerEntity serverPlayerEntity) {
        return this.pos;
    }

    @Override
    public Text getDisplayName() {
        return Text.literal("Entropic Stabilizer");
    }

    @Override
    public @Nullable ScreenHandler createMenu(int syncId, PlayerInventory playerInventory, PlayerEntity player) {
        return new EntropicStabilizerScreenHandler(syncId, playerInventory, this.pos);
    }

    @Nullable
    @Override
    public Packet<ClientPlayPacketListener> toUpdatePacket() {
        return BlockEntityUpdateS2CPacket.create(this);
    }

    @Override
    public NbtCompound toInitialChunkDataNbt(RegistryWrapper.WrapperLookup registryLookup) {
        return createNbt(registryLookup);
    }

//    public float getCookProgress() {
//        int i = this.propertyDelegate.get(2);
//        int j = this.propertyDelegate.get(3);
//        return j != 0 && i != 0 ? MathHelper.clamp((float)i / (float)j, 0.0F, 1.0F) : 0.0F;
//    }
//
//    public float getFuelProgress() {
//        int i = this.propertyDelegate.get(1);
//        if (i == 0) {
//            i = 200;
//        }
//
//        return MathHelper.clamp((float)this.propertyDelegate.get(0) / (float)i, 0.0F, 1.0F);
//    }

    public boolean canInsertIntoSlot(int index) {
        return index != 1;
    }
}
