package com.dairymoose.modernlife.tileentities;

import com.dairymoose.modernlife.blocks.MicrowaveBlock;
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/MicrowaveBlockEntity.class */
public class MicrowaveBlockEntity extends BlockEntity implements Container {
    public static final BlockEntityType<MicrowaveBlockEntity> MICROWAVE = BlockEntityType.Builder.of(MicrowaveBlockEntity::new, new Block[]{(Block) CustomBlocks.BLOCK_MICROWAVE.get()}).build((Type) null);
    private static final Logger LOGGER = LogManager.getLogger();
    public static final int MICROWAVE_CONTAINER_SIZE = 1;
    private NonNullList<ItemStack> microwaveStack;
    private int cookProgress;
    private int cookTime;
    public int tickCounter;

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

    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 void handleUpdateTag(CompoundTag tag) {
        ContainerHelper.loadAllItems(tag, this.microwaveStack);
        getItem(0);
        super.handleUpdateTag(tag);
    }

    public void load(CompoundTag p_230337_2_) {
        super.load(p_230337_2_);
        ContainerHelper.loadAllItems(p_230337_2_, this.microwaveStack);
        if (p_230337_2_.contains("CookProgress")) {
            this.cookProgress = p_230337_2_.getInt("CookProgress");
        }
        if (p_230337_2_.contains("CookTime")) {
            this.cookTime = p_230337_2_.getInt("CookTime");
        }
    }

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

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

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

    public void m_6211_() {
        this.microwaveStack.set(0, ItemStack.EMPTY);
    }

    public int getContainerSize() {
        return 1;
    }

    public boolean isEmpty() {
        return ((ItemStack) this.microwaveStack.get(0)).isEmpty();
    }

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

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

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

    public void setItem(int var1, ItemStack item) {
        this.microwaveStack.set(var1, item);
        if (!this.level.isClientSide) {
            getLevel().sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 2);
        }
    }

    private void setItemInternal(int var1, ItemStack item) {
        this.microwaveStack.set(var1, item);
    }

    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 MicrowaveBlockEntity(BlockPos pos, BlockState state) {
        super(MICROWAVE, pos, state);
        this.microwaveStack = NonNullList.withSize(1, ItemStack.EMPTY);
        this.cookProgress = 0;
        this.cookTime = 0;
        this.tickCounter = 0;
    }

    public void cookItem(ItemStack itemStack, int cookTime) {
        this.cookProgress = 0;
        this.cookTime = cookTime;
        setItem(0, itemStack.copy());
    }

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

    public void tick() {
        if (!this.level.isClientSide && !isEmpty() && this.cookTime == 0) {
            ItemStack itemStack = getItem(0);
            Optional<CampfireCookingRecipe> optional = this.level.getRecipeManager().getRecipeFor(RecipeType.CAMPFIRE_COOKING, new SimpleContainer(new ItemStack[]{itemStack}), this.level);
            this.cookTime = MicrowaveBlock.calculateTime(optional);
            this.cookProgress = 0;
        }
        if (!this.level.isClientSide && !isEmpty() && this.cookTime > 0) {
            if (((Boolean) getBlockState().getValue(MicrowaveBlock.OPEN_DOOR)).booleanValue()) {
                this.cookProgress--;
                if (this.cookProgress < 0) {
                    this.cookProgress = 0;
                }
            } else {
                this.cookProgress++;
                if (this.cookProgress >= this.cookTime) {
                    this.cookTime = 0;
                    ItemStack toCook = getItem(0);
                    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 microwaveFacing = getBlockState().getValue(StoveBlock.FACING);
                    if (microwaveFacing == Direction.NORTH) {
                        zMod = 0.0d + 0.6d;
                    } else if (microwaveFacing == Direction.EAST) {
                        xMod = 0.0d - 0.6d;
                    } else if (microwaveFacing == Direction.WEST) {
                        xMod = 0.0d + 0.6d;
                    } else if (microwaveFacing == 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_);
                    setItem(0, ItemStack.EMPTY);
                    BlockState oldState = getBlockState();
                    BlockState newState = (BlockState) oldState.setValue(MicrowaveBlock.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(MicrowaveBlock.OPEN_DOOR)).booleanValue()) {
                this.tickCounter++;
            }
        }
    }
}
