package com.dairymoose.inventory.container;

import com.dairymoose.modernlife.core.ModernLifeCommon;
import com.dairymoose.modernlife.network.play.client.ServerboundTrashCanPacket;
import net.minecraft.world.Container;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.ContainerListener;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/* loaded from: outputsrg.jar:com/dairymoose/inventory/container/TrashCanContainer.class */
public class TrashCanContainer extends AbstractContainerMenu {
    public static final MenuType<TrashCanContainer> TRASH_CAN_CONTAINER_TYPE = new MenuType<>(TrashCanContainer::new);
    private static final Logger LOGGER = LogManager.getLogger();
    private final Container container;
    private final int containerRows;
    private Slot trashSlot;
    Inventory playerInventory;

    public TrashCanContainer(int containerId, Inventory playerInventory, Container trashCanInventory) {
        this(TRASH_CAN_CONTAINER_TYPE, containerId, playerInventory, trashCanInventory, 1);
    }

    public TrashCanContainer(int containerId, Inventory playerInventory) {
        this(TRASH_CAN_CONTAINER_TYPE, containerId, playerInventory, new SimpleContainer(8), 1);
    }

    public TrashCanContainer(MenuType<?> p_i50092_1_, int p_i50092_2_, Inventory playerInventory, Container trashCanInventory, int containerRows) {
        super(p_i50092_1_, p_i50092_2_);
        this.trashSlot = null;
        AbstractContainerMenu.checkContainerSize(trashCanInventory, 8);
        this.container = trashCanInventory;
        this.containerRows = containerRows;
        trashCanInventory.startOpen(playerInventory.player);
        this.playerInventory = playerInventory;
        int trashInventoryIndex = 0 + 1;
        this.trashSlot = addSlot(new Slot(trashCanInventory, 0, 8, 18));
        for (int i = 0; i < 7; i++) {
            int i2 = trashInventoryIndex;
            trashInventoryIndex++;
            addSlot(new Slot(trashCanInventory, i2, 44 + (i * 18), 18));
        }
        for (int row = 0; row < 3; row++) {
            for (int col = 0; col < 9; col++) {
                addSlot(new Slot(playerInventory, col + (row * 9) + 9, 8 + (col * 18), 50 + (row * 18)));
            }
        }
        for (int col2 = 0; col2 < 9; col2++) {
            addSlot(new Slot(playerInventory, col2, 8 + (col2 * 18), 108));
        }
        addDataSlot(new IncrementingDataHolder());
        if (!this.playerInventory.player.level.isClientSide) {
            addSlotListener(new ContainerListener() { // from class: com.dairymoose.inventory.container.TrashCanContainer.1
                final /* synthetic */ TrashCanContainer val$thisContainer;

                C00041(TrashCanContainer this) {
                    this = this;
                }

                public void dataChanged(AbstractContainerMenu paramContainer, int index, int data) {
                    if (paramContainer == this) {
                        ItemStack paramItemStack = paramContainer.getSlot(index).getItem();
                        if (TrashCanContainer.this.trashSlot != null && index == TrashCanContainer.this.trashSlot.index && paramItemStack != ItemStack.EMPTY) {
                            ModernLifeCommon.LOGGER.debug("server trash can");
                            if (paramContainer instanceof TrashCanContainer) {
                                TrashCanContainer trashCanContainer = (TrashCanContainer) paramContainer;
                                if (ServerboundTrashCanPacket.playerHasItem(((TrashCanContainer) paramContainer).playerInventory.player, paramItemStack)) {
                                    trashCanContainer.trashItem(paramItemStack);
                                }
                            }
                        }
                    }
                }

                public void slotChanged(AbstractContainerMenu var1, int var2, ItemStack var3) {
                }
            });
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* renamed from: com.dairymoose.inventory.container.TrashCanContainer$1 */
    /* loaded from: outputsrg.jar:com/dairymoose/inventory/container/TrashCanContainer$1.class */
    public class C00041 implements ContainerListener {
        final /* synthetic */ TrashCanContainer val$thisContainer;

        C00041(TrashCanContainer this) {
            this = this;
        }

        public void dataChanged(AbstractContainerMenu paramContainer, int index, int data) {
            if (paramContainer == this) {
                ItemStack paramItemStack = paramContainer.getSlot(index).getItem();
                if (TrashCanContainer.this.trashSlot != null && index == TrashCanContainer.this.trashSlot.index && paramItemStack != ItemStack.EMPTY) {
                    ModernLifeCommon.LOGGER.debug("server trash can");
                    if (paramContainer instanceof TrashCanContainer) {
                        TrashCanContainer trashCanContainer = (TrashCanContainer) paramContainer;
                        if (ServerboundTrashCanPacket.playerHasItem(((TrashCanContainer) paramContainer).playerInventory.player, paramItemStack)) {
                            trashCanContainer.trashItem(paramItemStack);
                        }
                    }
                }
            }
        }

        public void slotChanged(AbstractContainerMenu var1, int var2, ItemStack var3) {
        }
    }

    public void trashItem(ItemStack toTrash) {
        for (int i = 7; i > 1; i--) {
            getSlot(i).set(this.container.getItem(i - 1));
        }
        getSlot(1).set(toTrash);
        getSlot(0).set(ItemStack.EMPTY);
    }

    public boolean stillValid(Player p_75145_1_) {
        return this.container.stillValid(p_75145_1_);
    }

    public ItemStack quickMoveStack(Player p_82846_1_, int p_82846_2_) {
        ItemStack lvt_3_1_ = ItemStack.EMPTY;
        Slot lvt_4_1_ = (Slot) this.slots.get(p_82846_2_);
        if (lvt_4_1_ != null && lvt_4_1_.hasItem()) {
            ItemStack lvt_5_1_ = lvt_4_1_.getItem();
            lvt_3_1_ = lvt_5_1_.copy();
            if (p_82846_2_ < 8) {
                if (!moveItemStackTo(lvt_5_1_, 8, this.slots.size(), true)) {
                    return ItemStack.EMPTY;
                }
            } else if (!moveItemStackTo(lvt_5_1_, 0, 8, false)) {
                return ItemStack.EMPTY;
            }
            if (lvt_5_1_.isEmpty()) {
                lvt_4_1_.set(ItemStack.EMPTY);
            } else {
                lvt_4_1_.setChanged();
            }
        }
        return lvt_3_1_;
    }

    public void removed(Player p_75134_1_) {
        super.removed(p_75134_1_);
        this.container.stopOpen(p_75134_1_);
    }

    public Container getContainer() {
        return this.container;
    }

    @OnlyIn(Dist.CLIENT)
    public int getRowCount() {
        return this.containerRows;
    }
}
