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 xd, final Villager axw) {
        final Brain<?> alp4 = axw.getBrain();
        if (!alp4.<LivingEntity>getMemory(MemoryModuleType.INTERACTION_TARGET).isPresent()) {
            return false;
        }
        final LivingEntity akz5 = alp4.<LivingEntity>getMemory(MemoryModuleType.INTERACTION_TARGET).get();
        return akz5.getType() == EntityType.PLAYER && axw.isAlive() && akz5.isAlive() && !axw.isBaby() && axw.distanceToSqr(akz5) <= 17.0;
    }
    
    public boolean canStillUse(final ServerLevel xd, final Villager axw, final long long3) {
        return this.checkExtraStartConditions(xd, axw) && this.lookTime > 0 && axw.getBrain().<LivingEntity>getMemory(MemoryModuleType.INTERACTION_TARGET).isPresent();
    }
    
    public void start(final ServerLevel xd, final Villager axw, final long long3) {
        super.start(xd, axw, long3);
        this.lookAtTarget(axw);
        this.cycleCounter = 0;
        this.displayIndex = 0;
        this.lookTime = 40;
    }
    
    public void tick(final ServerLevel xd, final Villager axw, final long long3) {
        final LivingEntity akz6 = this.lookAtTarget(axw);
        this.findItemsToDisplay(akz6, axw);
        if (!this.displayItems.isEmpty()) {
            this.displayCyclingItems(axw);
        }
        else {
            axw.setItemSlot(EquipmentSlot.MAINHAND, ItemStack.EMPTY);
            this.lookTime = Math.min(this.lookTime, 40);
        }
        --this.lookTime;
    }
    
    public void tick(final ServerLevel xd, final Villager axw, final long long3) {
        super.stop(xd, axw, long3);
        axw.getBrain().<LivingEntity>eraseMemory(MemoryModuleType.INTERACTION_TARGET);
        axw.setItemSlot(EquipmentSlot.MAINHAND, ItemStack.EMPTY);
        this.playerItemStack = null;
    }
    
    private void findItemsToDisplay(final LivingEntity akz, final Villager axw) {
        boolean boolean4 = false;
        final ItemStack ben5 = akz.getMainHandItem();
        if (this.playerItemStack == null || !ItemStack.isSame(this.playerItemStack, ben5)) {
            this.playerItemStack = ben5;
            boolean4 = true;
            this.displayItems.clear();
        }
        if (boolean4 && !this.playerItemStack.isEmpty()) {
            this.updateDisplayItems(axw);
            if (!this.displayItems.isEmpty()) {
                this.lookTime = 900;
                this.displayFirstItem(axw);
            }
        }
    }
    
    private void displayFirstItem(final Villager axw) {
        axw.setItemSlot(EquipmentSlot.MAINHAND, this.displayItems.get(0));
    }
    
    private void updateDisplayItems(final Villager axw) {
        for (final MerchantOffer biz4 : axw.getOffers()) {
            if (!biz4.isOutOfStock() && this.playerItemStackMatchesCostOfOffer(biz4)) {
                this.displayItems.add(biz4.getResult());
            }
        }
    }
    
    private boolean playerItemStackMatchesCostOfOffer(final MerchantOffer biz) {
        return ItemStack.isSame(this.playerItemStack, biz.getCostA()) || ItemStack.isSame(this.playerItemStack, biz.getCostB());
    }
    
    private LivingEntity lookAtTarget(final Villager axw) {
        final Brain<?> alp3 = axw.getBrain();
        final LivingEntity akz4 = alp3.<LivingEntity>getMemory(MemoryModuleType.INTERACTION_TARGET).get();
        alp3.<EntityPosWrapper>setMemory((MemoryModuleType<EntityPosWrapper>)MemoryModuleType.LOOK_TARGET, new EntityPosWrapper(akz4));
        return akz4;
    }
    
    private void displayCyclingItems(final Villager axw) {
        if (this.displayItems.size() >= 2 && ++this.cycleCounter >= 40) {
            ++this.displayIndex;
            this.cycleCounter = 0;
            if (this.displayIndex > this.displayItems.size() - 1) {
                this.displayIndex = 0;
            }
            axw.setItemSlot(EquipmentSlot.MAINHAND, this.displayItems.get(this.displayIndex));
        }
    }
}
