package net.minecraft.world.entity.ai.behavior;

import java.util.Iterator;
import net.minecraft.world.item.trading.MerchantOffer;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.ai.Brain;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.server.level.ServerLevel;
import com.google.common.collect.Lists;
import java.util.Map;
import net.minecraft.world.entity.LivingEntity;
import com.google.common.collect.ImmutableMap;
import net.minecraft.world.entity.ai.memory.MemoryStatus;
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
import java.util.List;
import javax.annotation.Nullable;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.npc.Villager;

public class ShowTradesToPlayer extends Behavior<Villager> {
    @Nullable
    private ItemStack playerItemStack;
    private final List<ItemStack> displayItems;
    private int cycleCounter;
    private int displayIndex;
    private int lookTime;
    
    public ShowTradesToPlayer(final int integer1, final int integer2) {
        super(ImmutableMap.<MemoryModuleType<LivingEntity>, MemoryStatus>of(MemoryModuleType.INTERACTION_TARGET, MemoryStatus.VALUE_PRESENT), integer1, integer2);
        this.displayItems = Lists.newArrayList();
    }
    
    public boolean checkExtraStartConditions(final ServerLevel zd, final Villager bdp) {
        final Brain<?> apr4 = bdp.getBrain();
        if (!apr4.<LivingEntity>getMemory(MemoryModuleType.INTERACTION_TARGET).isPresent()) {
            return false;
        }
        final LivingEntity aoy5 = apr4.<LivingEntity>getMemory(MemoryModuleType.INTERACTION_TARGET).get();
        return aoy5.getType() == EntityType.PLAYER && bdp.isAlive() && aoy5.isAlive() && !bdp.isBaby() && bdp.distanceToSqr(aoy5) <= 17.0;
    }
    
    public boolean canStillUse(final ServerLevel zd, final Villager bdp, final long long3) {
        return this.checkExtraStartConditions(zd, bdp) && this.lookTime > 0 && bdp.getBrain().<LivingEntity>getMemory(MemoryModuleType.INTERACTION_TARGET).isPresent();
    }
    
    public void start(final ServerLevel zd, final Villager bdp, final long long3) {
        super.start(zd, bdp, long3);
        this.lookAtTarget(bdp);
        this.cycleCounter = 0;
        this.displayIndex = 0;
        this.lookTime = 40;
    }
    
    public void tick(final ServerLevel zd, final Villager bdp, final long long3) {
        final LivingEntity aoy6 = this.lookAtTarget(bdp);
        this.findItemsToDisplay(aoy6, bdp);
        if (!this.displayItems.isEmpty()) {
            this.displayCyclingItems(bdp);
        }
        else {
            bdp.setItemSlot(EquipmentSlot.MAINHAND, ItemStack.EMPTY);
            this.lookTime = Math.min(this.lookTime, 40);
        }
        --this.lookTime;
    }
    
    public void tick(final ServerLevel zd, final Villager bdp, final long long3) {
        super.stop(zd, bdp, long3);
        bdp.getBrain().<LivingEntity>eraseMemory(MemoryModuleType.INTERACTION_TARGET);
        bdp.setItemSlot(EquipmentSlot.MAINHAND, ItemStack.EMPTY);
        this.playerItemStack = null;
    }
    
    private void findItemsToDisplay(final LivingEntity aoy, final Villager bdp) {
        boolean boolean4 = false;
        final ItemStack bki5 = aoy.getMainHandItem();
        if (this.playerItemStack == null || !ItemStack.isSame(this.playerItemStack, bki5)) {
            this.playerItemStack = bki5;
            boolean4 = true;
            this.displayItems.clear();
        }
        if (boolean4 && !this.playerItemStack.isEmpty()) {
            this.updateDisplayItems(bdp);
            if (!this.displayItems.isEmpty()) {
                this.lookTime = 900;
                this.displayFirstItem(bdp);
            }
        }
    }
    
    private void displayFirstItem(final Villager bdp) {
        bdp.setItemSlot(EquipmentSlot.MAINHAND, this.displayItems.get(0));
    }
    
    private void updateDisplayItems(final Villager bdp) {
        for (final MerchantOffer boz4 : bdp.getOffers()) {
            if (!boz4.isOutOfStock() && this.playerItemStackMatchesCostOfOffer(boz4)) {
                this.displayItems.add(boz4.getResult());
            }
        }
    }
    
    private boolean playerItemStackMatchesCostOfOffer(final MerchantOffer boz) {
        return ItemStack.isSame(this.playerItemStack, boz.getCostA()) || ItemStack.isSame(this.playerItemStack, boz.getCostB());
    }
    
    private LivingEntity lookAtTarget(final Villager bdp) {
        final Brain<?> apr3 = bdp.getBrain();
        final LivingEntity aoy4 = apr3.<LivingEntity>getMemory(MemoryModuleType.INTERACTION_TARGET).get();
        apr3.<EntityTracker>setMemory((MemoryModuleType<EntityTracker>)MemoryModuleType.LOOK_TARGET, new EntityTracker(aoy4, true));
        return aoy4;
    }
    
    private void displayCyclingItems(final Villager bdp) {
        if (this.displayItems.size() >= 2 && ++this.cycleCounter >= 40) {
            ++this.displayIndex;
            this.cycleCounter = 0;
            if (this.displayIndex > this.displayItems.size() - 1) {
                this.displayIndex = 0;
            }
            bdp.setItemSlot(EquipmentSlot.MAINHAND, this.displayItems.get(this.displayIndex));
        }
    }
}
