package com.dairymoose.modernlife.tileentities;

import com.dairymoose.modernlife.core.CustomBlocks;
import com.mojang.datafixers.types.Type;
import net.minecraft.core.BlockPos;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.Container;
import net.minecraft.world.ContainerHelper;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.ItemLike;
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/RefrigeratorBlockEntity.class */
public class RefrigeratorBlockEntity extends BlockEntity implements Container {
    public static final BlockEntityType<RefrigeratorBlockEntity> REFRIGERATOR = BlockEntityType.Builder.of(RefrigeratorBlockEntity::new, new Block[]{(Block) CustomBlocks.BLOCK_REFRIGERATOR.get()}).build((Type) null);
    private static final Logger LOGGER = LogManager.getLogger();
    public static final int REFRIGERATOR_CONTAINER_SIZE = 36;
    private int tickCount;
    private NonNullList<ItemStack> items;
    private int[] iceCubeProgress;

    public NonNullList<ItemStack> getItems() {
        return this.items;
    }

    public RefrigeratorBlockEntity(BlockPos pos, BlockState state) {
        super(REFRIGERATOR, pos, state);
        this.tickCount = 0;
        this.items = NonNullList.withSize(36, ItemStack.EMPTY);
        this.iceCubeProgress = new int[36];
    }

    public void load(CompoundTag p_230337_2_) {
        super.load(p_230337_2_);
        ContainerHelper.loadAllItems(p_230337_2_, this.items);
    }

    protected void saveAdditional(CompoundTag p_189515_1_) {
        ContainerHelper.saveAllItems(p_189515_1_, this.items);
    }

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

    public int getContainerSize() {
        return 36;
    }

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

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

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

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

    public void setItem(int var1, ItemStack var2) {
        this.items.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;
    }

    int getNextFreeSlot() {
        for (int i = 0; i < getContainerSize(); i++) {
            if (getItem(i).isEmpty()) {
                return i;
            }
        }
        return -1;
    }

    int getNextIceSlot() {
        for (int i = 0; i < getContainerSize(); i++) {
            if (getItem(i).getItem() == Items.ICE && getItem(i).getCount() < getItem(i).getMaxStackSize()) {
                return i;
            }
        }
        return -1;
    }

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

    public void tick() {
        if (this.tickCount % 100 == 0) {
            for (int i = 0; i < getContainerSize(); i++) {
                ItemStack itemStack = getItem(i);
                if (itemStack.getItem() == Items.WATER_BUCKET) {
                    int iceSlot = getNextIceSlot();
                    if (iceSlot != -1) {
                        int[] iArr = this.iceCubeProgress;
                        int i2 = i;
                        iArr[i2] = iArr[i2] + 1;
                        if (this.iceCubeProgress[i] >= 24) {
                            setItem(i, new ItemStack(new ItemLike() { // from class: com.dairymoose.modernlife.tileentities.RefrigeratorBlockEntity.1
                                C01601() {
                                }

                                public Item asItem() {
                                    return Items.BUCKET;
                                }
                            }));
                            getItem(iceSlot).grow(1);
                            this.iceCubeProgress[i] = 0;
                        }
                    } else {
                        int free = getNextFreeSlot();
                        if (free != -1) {
                            int[] iArr2 = this.iceCubeProgress;
                            int i3 = i;
                            iArr2[i3] = iArr2[i3] + 1;
                            if (this.iceCubeProgress[i] >= 24) {
                                setItem(i, new ItemStack(new ItemLike() { // from class: com.dairymoose.modernlife.tileentities.RefrigeratorBlockEntity.2
                                    C01612() {
                                    }

                                    public Item asItem() {
                                        return Items.BUCKET;
                                    }
                                }));
                                setItem(free, new ItemStack(new ItemLike() { // from class: com.dairymoose.modernlife.tileentities.RefrigeratorBlockEntity.3
                                    C01623() {
                                    }

                                    public Item asItem() {
                                        return Items.ICE;
                                    }
                                }));
                                this.iceCubeProgress[i] = 0;
                            }
                        }
                    }
                } else {
                    this.iceCubeProgress[i] = 0;
                }
            }
        }
        this.tickCount++;
    }

    /* renamed from: com.dairymoose.modernlife.tileentities.RefrigeratorBlockEntity$1 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/tileentities/RefrigeratorBlockEntity$1.class */
    public class C01601 implements ItemLike {
        C01601() {
        }

        public Item asItem() {
            return Items.BUCKET;
        }
    }

    /* renamed from: com.dairymoose.modernlife.tileentities.RefrigeratorBlockEntity$2 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/tileentities/RefrigeratorBlockEntity$2.class */
    public class C01612 implements ItemLike {
        C01612() {
        }

        public Item asItem() {
            return Items.BUCKET;
        }
    }

    /* renamed from: com.dairymoose.modernlife.tileentities.RefrigeratorBlockEntity$3 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/tileentities/RefrigeratorBlockEntity$3.class */
    public class C01623 implements ItemLike {
        C01623() {
        }

        public Item asItem() {
            return Items.ICE;
        }
    }
}
