package com.dairymoose.modernlife.tileentities;

import com.dairymoose.modernlife.blocks.StoveBlock;
import com.dairymoose.modernlife.core.CustomBlocks;
import com.mojang.datafixers.types.Type;
import java.util.Optional;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.Connection;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.Container;
import net.minecraft.world.ContainerHelper;
import net.minecraft.world.Containers;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CampfireCookingRecipe;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/* loaded from: outputsrg.jar:com/dairymoose/modernlife/tileentities/StoveBlockEntity.class */
public class StoveBlockEntity extends BlockEntity implements Container {
    public static final BlockEntityType<StoveBlockEntity> STOVE = BlockEntityType.Builder.of(StoveBlockEntity::new, new Block[]{(Block) CustomBlocks.BLOCK_STOVE.get()}).build((Type) null);
    private static final Logger LOGGER = LogManager.getLogger();
    public static final int STOVE_CONTAINER_SIZE = 8;
    private NonNullList<ItemStack> stoveStack;
    private int[] cookProgress;
    private int[] cookTime;
    public int tickCounter;

    public int getMaxStackSize() {
        return 1;
    }

    public boolean canPlaceItem(int flag, ItemStack itemStack) {
        Optional<CampfireCookingRecipe> optional = this.level.getRecipeManager().getRecipeFor(RecipeType.CAMPFIRE_COOKING, new SimpleContainer(new ItemStack[]{itemStack}), this.level);
        if (isEmpty() && optional.isPresent()) {
            return true;
        }
        return false;
    }

    public CompoundTag getUpdateTag() {
        CompoundTag nbt = super.getUpdateTag();
        ContainerHelper.saveAllItems(nbt, this.stoveStack);
        return nbt;
    }

    public void handleUpdateTag(CompoundTag tag) {
        ContainerHelper.loadAllItems(tag, this.stoveStack);
        super.handleUpdateTag(tag);
    }

    public void load(CompoundTag p_230337_2_) {
        super.load(p_230337_2_);
        ContainerHelper.loadAllItems(p_230337_2_, this.stoveStack);
        if (p_230337_2_.contains("CookProgress")) {
            int[] data = p_230337_2_.getIntArray("CookProgress");
            System.arraycopy(data, 0, this.cookProgress, 0, Math.min(this.cookProgress.length, data.length));
        }
        if (p_230337_2_.contains("CookTime")) {
            int[] data2 = p_230337_2_.getIntArray("CookTime");
            System.arraycopy(data2, 0, this.cookTime, 0, Math.min(this.cookTime.length, data2.length));
        }
    }

    /* renamed from: getUpdatePacket */
    public ClientboundBlockEntityDataPacket m248getUpdatePacket() {
        return ClientboundBlockEntityDataPacket.create(this);
    }

    public void onDataPacket(Connection conn, ClientboundBlockEntityDataPacket pkt) {
        CompoundTag tag = pkt.getTag();
        if (tag != null) {
            this.stoveStack.clear();
            ContainerHelper.loadAllItems(tag, this.stoveStack);
        }
    }

    protected void saveAdditional(CompoundTag p_189515_1_) {
        ContainerHelper.saveAllItems(p_189515_1_, this.stoveStack);
        p_189515_1_.putIntArray("CookProgress", this.cookProgress);
        p_189515_1_.putIntArray("CookTime", this.cookTime);
    }

    public boolean isFull() {
        int count = 0;
        for (int i = 0; i < this.stoveStack.size(); i++) {
            if (!((ItemStack) this.stoveStack.get(i)).isEmpty()) {
                count++;
            }
        }
        return count == this.stoveStack.size();
    }

    public int getNextFreeSlot() {
        for (int i = 0; i < this.stoveStack.size(); i++) {
            if (((ItemStack) this.stoveStack.get(i)).isEmpty()) {
                return i;
            }
        }
        return -1;
    }

    public int getNextOccupiedSlot() {
        for (int i = 0; i < this.stoveStack.size(); i++) {
            if (!((ItemStack) this.stoveStack.get(i)).isEmpty()) {
                return i;
            }
        }
        return -1;
    }

    public void m_6211_() {
        for (int i = 0; i < this.stoveStack.size(); i++) {
            this.stoveStack.set(i, ItemStack.EMPTY);
        }
    }

    public int getContainerSize() {
        return 8;
    }

    public boolean isEmpty() {
        for (int i = 0; i < this.stoveStack.size(); i++) {
            if (!((ItemStack) this.stoveStack.get(i)).isEmpty()) {
                return false;
            }
        }
        return true;
    }

    public ItemStack getItem(int var1) {
        return (ItemStack) this.stoveStack.get(var1);
    }

    public ItemStack removeItem(int var1, int var2) {
        return ContainerHelper.removeItem(this.stoveStack, var1, var2);
    }

    public ItemStack removeItemNoUpdate(int var1) {
        return ContainerHelper.takeItem(this.stoveStack, var1);
    }

    public void setItem(int var1, ItemStack itemStack) {
        if (itemStack.isEmpty()) {
            setItemInternal(var1, itemStack);
            return;
        }
        Optional<CampfireCookingRecipe> optional = this.level.getRecipeManager().getRecipeFor(RecipeType.CAMPFIRE_COOKING, new SimpleContainer(new ItemStack[]{itemStack}), this.level);
        if (optional.isPresent()) {
            ItemStack oneItem = itemStack.split(1);
            cookItem(oneItem, StoveBlock.calculateTime(optional));
        }
        if (!this.level.isClientSide) {
            getLevel().sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 2);
        }
    }

    private void setItemInternal(int var1, ItemStack var2) {
        this.stoveStack.set(var1, var2);
    }

    public boolean stillValid(Player p_70300_1_) {
        return this.level.getBlockEntity(this.worldPosition) == this && p_70300_1_.distanceToSqr(((double) this.worldPosition.getX()) + 0.5d, ((double) this.worldPosition.getY()) + 0.5d, ((double) this.worldPosition.getZ()) + 0.5d) <= 64.0d;
    }

    public StoveBlockEntity(BlockPos pos, BlockState state) {
        super(STOVE, pos, state);
        this.stoveStack = NonNullList.withSize(8, ItemStack.EMPTY);
        this.cookProgress = new int[8];
        this.cookTime = new int[8];
        this.tickCounter = 0;
    }

    public void cookItem(ItemStack itemStack, int cookTime) {
        int freeSlot = getNextFreeSlot();
        if (freeSlot != -1) {
            this.cookProgress[freeSlot] = 0;
            this.cookTime[freeSlot] = cookTime;
            setItemInternal(freeSlot, itemStack.copy());
        }
    }

    public static void tick(Level level, BlockPos pos, BlockState state, StoveBlockEntity entity) {
        entity.tick();
    }

    public void tick() {
        if (!this.level.isClientSide && !isEmpty()) {
            for (int i = 0; i < 8; i++) {
                if (this.cookTime[i] > 0) {
                    if (((Boolean) getBlockState().getValue(StoveBlock.OPEN_DOOR)).booleanValue()) {
                        int[] iArr = this.cookProgress;
                        int i2 = i;
                        iArr[i2] = iArr[i2] - 1;
                        if (this.cookProgress[i] < 0) {
                            this.cookProgress[i] = 0;
                        }
                    } else {
                        int[] iArr2 = this.cookProgress;
                        int i3 = i;
                        iArr2[i3] = iArr2[i3] + 1;
                        if (this.cookProgress[i] >= this.cookTime[i]) {
                            this.cookTime[i] = 0;
                            ItemStack toCook = getItem(i);
                            SimpleContainer simpleContainer = new SimpleContainer(new ItemStack[]{toCook});
                            ItemStack lvt_4_1_ = (ItemStack) this.level.getRecipeManager().getRecipeFor(RecipeType.CAMPFIRE_COOKING, simpleContainer, this.level).map(p_213979_1_ -> {
                                return p_213979_1_.m_5874_(simpleContainer);
                            }).orElse(toCook);
                            BlockPos pos = getBlockPos();
                            double xMod = 0.0d;
                            double zMod = 0.0d;
                            Direction stoveFacing = getBlockState().getValue(StoveBlock.FACING);
                            if (stoveFacing == Direction.NORTH) {
                                zMod = 0.0d + 0.6d;
                            } else if (stoveFacing == Direction.EAST) {
                                xMod = 0.0d - 0.6d;
                            } else if (stoveFacing == Direction.WEST) {
                                xMod = 0.0d + 0.6d;
                            } else if (stoveFacing == Direction.SOUTH) {
                                zMod = 0.0d - 0.6d;
                            }
                            Containers.dropItemStack(this.level, pos.getX() + 0.5d + xMod, pos.getY() + 0.5d, pos.getZ() + 0.5d + zMod, lvt_4_1_);
                            setItemInternal(i, ItemStack.EMPTY);
                            BlockState oldState = getBlockState();
                            BlockState newState = oldState;
                            if (isEmpty()) {
                                newState = (BlockState) oldState.setValue(StoveBlock.OPEN_DOOR, true);
                                getLevel().setBlockAndUpdate(pos, newState);
                            }
                            getLevel().sendBlockUpdated(pos, oldState, newState, 2);
                        }
                    }
                }
            }
        }
        if (this.level.isClientSide) {
            if (isEmpty()) {
                this.tickCounter = 0;
            } else if (!((Boolean) getBlockState().getValue(StoveBlock.OPEN_DOOR)).booleanValue()) {
                this.tickCounter++;
            }
        }
    }
}
