package com.dairymoose.modernlife.tileentities;

import com.dairymoose.modernlife.core.CustomBlocks;
import com.dairymoose.modernlife.core.ModernLifeConfig;
import com.dairymoose.modernlife.network.play.client.ServerboundMultipartCameraPacket;
import com.mojang.datafixers.types.Type;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import net.minecraft.core.BlockPos;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.Container;
import net.minecraft.world.ContainerHelper;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
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 net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/* loaded from: outputsrg.jar:com/dairymoose/modernlife/tileentities/PhotocopierBlockEntity.class */
public class PhotocopierBlockEntity extends BlockEntity implements Container {
    public static final BlockEntityType<PhotocopierBlockEntity> PHOTOCOPIER = BlockEntityType.Builder.of(PhotocopierBlockEntity::new, new Block[]{(Block) CustomBlocks.BLOCK_PHOTOCOPIER.get()}).build((Type) null);
    private static final Logger LOGGER = LogManager.getLogger();
    public static final int PHOTOCOPIER_CONTAINER_SIZE = 27;
    private Queue<RequestQueueItem> copyRequestQueue;
    private int counter;
    private int copyProgress;
    private static final int PAPER_PER_CANVAS = 1;
    private NonNullList<ItemStack> items;

    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/tileentities/PhotocopierBlockEntity$RequestQueueItem.class */
    public static class RequestQueueItem {
        Long uniqueId;
        CompoundTag nbt;
    }

    public void addTaskToQueue(long uniqueId, CompoundTag nbt, int copies) {
        if (this.copyRequestQueue != null) {
            for (int i = 0; i < copies; i++) {
                RequestQueueItem rqi = new RequestQueueItem();
                rqi.uniqueId = Long.valueOf(uniqueId);
                rqi.nbt = nbt;
                this.copyRequestQueue.add(rqi);
            }
        }
    }

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

    public void tick() {
        this.counter++;
        if (this.counter % 5 == 0 && !this.copyRequestQueue.isEmpty()) {
            if (this.copyProgress == -1) {
                this.level.playSound((Player) null, getBlockPos(), SoundEvents.NOTE_BLOCK_CHIME, SoundSource.BLOCKS, 0.25f, 0.5f);
                this.copyProgress = 4;
                return;
            }
            if (this.copyProgress > 0) {
                this.copyProgress--;
                return;
            }
            if (this.copyProgress == 0) {
                this.copyProgress = -1;
                int freeSlot = getFirstFreeSlot();
                int paperSlot = getFirstValidPaperSlot();
                if (freeSlot != -1 && paperSlot != -1) {
                    getItem(paperSlot).shrink(1);
                    RequestQueueItem rqi = this.copyRequestQueue.remove();
                    if (rqi != null) {
                        Long uniqueId = rqi.uniqueId;
                        CompoundTag nbt = rqi.nbt;
                        if (uniqueId != null) {
                            long newUniqueId = uniqueId.longValue();
                            Boolean bProductNewImage = (Boolean) ModernLifeConfig.SERVER.photocopierProducesNewImageFiles.get();
                            if (bProductNewImage != null && bProductNewImage.booleanValue()) {
                                newUniqueId = ServerboundMultipartCameraPacket.copyPng(uniqueId.longValue());
                            }
                            if (newUniqueId != -1) {
                                ItemStack itemStack = new ItemStack(CustomBlocks.BLOCK_CANVAS.get());
                                if (nbt == null) {
                                    nbt = new CompoundTag();
                                }
                                itemStack.setTag(nbt);
                                itemStack.getTag().putLong("UniqueId", newUniqueId);
                                setItem(freeSlot, itemStack);
                                this.level.playSound((Player) null, getBlockPos(), SoundEvents.PAINTING_PLACE, SoundSource.BLOCKS, 0.25f, 0.5f);
                                return;
                            }
                            return;
                        }
                        return;
                    }
                    return;
                }
                BlockPos pos = getBlockPos();
                List<Player> players = this.level.m_45976_(Player.class, AABB.ofSize(new Vec3(pos.getX(), pos.getY(), pos.getZ()), 10.0d, 10.0d, 10.0d));
                this.level.playSound((Player) null, getBlockPos(), SoundEvents.CHAIN_BREAK, SoundSource.BLOCKS, 0.25f, 0.5f);
                if (players != null && !players.isEmpty()) {
                    for (Player p : players) {
                        if (freeSlot == -1) {
                            p.sendMessage(new TextComponent("Photocopier inventory is full!  All pending jobs cancelled"), p.getUUID());
                        } else if (paperSlot == -1) {
                            p.sendMessage(new TextComponent("Photocopier requires at least 1 paper in its inventory to copy!  All pending jobs cancelled"), p.getUUID());
                        }
                    }
                }
                this.copyRequestQueue.clear();
            }
        }
    }

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

    public PhotocopierBlockEntity(BlockPos pos, BlockState state) {
        super(PHOTOCOPIER, pos, state);
        this.copyRequestQueue = new ConcurrentLinkedQueue();
        this.counter = 0;
        this.copyProgress = -1;
        this.items = NonNullList.withSize(27, ItemStack.EMPTY);
    }

    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 27;
    }

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

    public int freeSlotCount() {
        int freeSlots = 0;
        for (int i = 0; i < this.items.size(); i++) {
            if (((ItemStack) this.items.get(i)).isEmpty()) {
                freeSlots++;
            }
        }
        return freeSlots;
    }

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

    public int getFirstValidPaperSlot() {
        for (int i = 0; i < this.items.size(); i++) {
            if (((ItemStack) this.items.get(i)).is(Items.PAPER) && ((ItemStack) this.items.get(i)).getCount() >= 1) {
                return i;
            }
        }
        return -1;
    }

    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;
    }
}
