package net.minecraft.world.inventory;

import org.apache.logging.log4j.LogManager;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import java.util.Iterator;
import java.util.Map;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import org.apache.commons.lang3.StringUtils;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.EnchantedBookItem;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.level.block.AnvilBlock;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.block.Block;
import net.minecraft.tags.Tag;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.entity.player.Inventory;
import org.apache.logging.log4j.Logger;

public class AnvilMenu extends ItemCombinerMenu {
    private static final Logger LOGGER;
    private int repairItemCountCost;
    private String itemName;
    private final DataSlot cost;
    
    public AnvilMenu(final int integer, final Inventory beb) {
        this(integer, beb, ContainerLevelAccess.NULL);
    }
    
    public AnvilMenu(final int integer, final Inventory beb, final ContainerLevelAccess bgs) {
        super(MenuType.ANVIL, integer, beb, bgs);
        this.addDataSlot(this.cost = DataSlot.standalone());
    }
    
    @Override
    protected boolean isValidBlock(final BlockState cfj) {
        return cfj.is(BlockTags.ANVIL);
    }
    
    @Override
    protected boolean mayPickup(final Player bec, final boolean boolean2) {
        return (bec.abilities.instabuild || bec.experienceLevel >= this.cost.get()) && this.cost.get() > 0;
    }
    
    @Override
    protected ItemStack onTake(final Player bec, final ItemStack bki) {
        if (!bec.abilities.instabuild) {
            bec.giveExperienceLevels(-this.cost.get());
        }
        this.inputSlots.setItem(0, ItemStack.EMPTY);
        if (this.repairItemCountCost > 0) {
            final ItemStack bki2 = this.inputSlots.getItem(1);
            if (!bki2.isEmpty() && bki2.getCount() > this.repairItemCountCost) {
                bki2.shrink(this.repairItemCountCost);
                this.inputSlots.setItem(1, bki2);
            }
            else {
                this.inputSlots.setItem(1, ItemStack.EMPTY);
            }
        }
        else {
            this.inputSlots.setItem(1, ItemStack.EMPTY);
        }
        this.cost.set(0);
        final BlockState cfj4;
        BlockState cfj5;
        this.access.execute((bqb, fu) -> {
            cfj4 = bqb.getBlockState(fu);
            if (!bec.abilities.instabuild && cfj4.is(BlockTags.ANVIL) && bec.getRandom().nextFloat() < 0.12f) {
                cfj5 = AnvilBlock.damage(cfj4);
                if (cfj5 == null) {
                    bqb.removeBlock(fu, false);
                    bqb.levelEvent(1029, fu, 0);
                }
                else {
                    bqb.setBlock(fu, cfj5, 2);
                    bqb.levelEvent(1030, fu, 0);
                }
            }
            else {
                bqb.levelEvent(1030, fu, 0);
            }
            return;
        });
        return bki;
    }
    
    @Override
    public void createResult() {
        final ItemStack bki2 = this.inputSlots.getItem(0);
        this.cost.set(1);
        int integer3 = 0;
        int integer4 = 0;
        int integer5 = 0;
        if (bki2.isEmpty()) {
            this.resultSlots.setItem(0, ItemStack.EMPTY);
            this.cost.set(0);
            return;
        }
        ItemStack bki3 = bki2.copy();
        final ItemStack bki4 = this.inputSlots.getItem(1);
        final Map<Enchantment, Integer> map8 = EnchantmentHelper.getEnchantments(bki3);
        integer4 += bki2.getBaseRepairCost() + (bki4.isEmpty() ? 0 : bki4.getBaseRepairCost());
        this.repairItemCountCost = 0;
        if (!bki4.isEmpty()) {
            final boolean boolean9 = bki4.getItem() == Items.ENCHANTED_BOOK && !EnchantedBookItem.getEnchantments(bki4).isEmpty();
            if (bki3.isDamageableItem() && bki3.getItem().isValidRepairItem(bki2, bki4)) {
                int integer6 = Math.min(bki3.getDamageValue(), bki3.getMaxDamage() / 4);
                if (integer6 <= 0) {
                    this.resultSlots.setItem(0, ItemStack.EMPTY);
                    this.cost.set(0);
                    return;
                }
                int integer7;
                for (integer7 = 0; integer6 > 0 && integer7 < bki4.getCount(); integer6 = Math.min(bki3.getDamageValue(), bki3.getMaxDamage() / 4), ++integer7) {
                    final int integer8 = bki3.getDamageValue() - integer6;
                    bki3.setDamageValue(integer8);
                    ++integer3;
                }
                this.repairItemCountCost = integer7;
            }
            else {
                if (!boolean9 && (bki3.getItem() != bki4.getItem() || !bki3.isDamageableItem())) {
                    this.resultSlots.setItem(0, ItemStack.EMPTY);
                    this.cost.set(0);
                    return;
                }
                if (bki3.isDamageableItem() && !boolean9) {
                    final int integer6 = bki2.getMaxDamage() - bki2.getDamageValue();
                    final int integer7 = bki4.getMaxDamage() - bki4.getDamageValue();
                    final int integer8 = integer7 + bki3.getMaxDamage() * 12 / 100;
                    final int integer9 = integer6 + integer8;
                    int integer10 = bki3.getMaxDamage() - integer9;
                    if (integer10 < 0) {
                        integer10 = 0;
                    }
                    if (integer10 < bki3.getDamageValue()) {
                        bki3.setDamageValue(integer10);
                        integer3 += 2;
                    }
                }
                final Map<Enchantment, Integer> map9 = EnchantmentHelper.getEnchantments(bki4);
                boolean boolean10 = false;
                boolean boolean11 = false;
                for (final Enchantment bnw14 : map9.keySet()) {
                    if (bnw14 == null) {
                        continue;
                    }
                    final int integer11 = map8.getOrDefault(bnw14, 0);
                    int integer12 = map9.get(bnw14);
                    integer12 = ((integer11 == integer12) ? (integer12 + 1) : Math.max(integer12, integer11));
                    boolean boolean12 = bnw14.canEnchant(bki2);
                    if (this.player.abilities.instabuild || bki2.getItem() == Items.ENCHANTED_BOOK) {
                        boolean12 = true;
                    }
                    for (final Enchantment bnw15 : map8.keySet()) {
                        if (bnw15 != bnw14 && !bnw14.isCompatibleWith(bnw15)) {
                            boolean12 = false;
                            ++integer3;
                        }
                    }
                    if (!boolean12) {
                        boolean11 = true;
                    }
                    else {
                        boolean10 = true;
                        if (integer12 > bnw14.getMaxLevel()) {
                            integer12 = bnw14.getMaxLevel();
                        }
                        map8.put(bnw14, integer12);
                        int integer13 = 0;
                        switch (bnw14.getRarity()) {
                            case COMMON: {
                                integer13 = 1;
                                break;
                            }
                            case UNCOMMON: {
                                integer13 = 2;
                                break;
                            }
                            case RARE: {
                                integer13 = 4;
                                break;
                            }
                            case VERY_RARE: {
                                integer13 = 8;
                                break;
                            }
                        }
                        if (boolean9) {
                            integer13 = Math.max(1, integer13 / 2);
                        }
                        integer3 += integer13 * integer12;
                        if (bki2.getCount() <= 1) {
                            continue;
                        }
                        integer3 = 40;
                    }
                }
                if (boolean11 && !boolean10) {
                    this.resultSlots.setItem(0, ItemStack.EMPTY);
                    this.cost.set(0);
                    return;
                }
            }
        }
        if (StringUtils.isBlank((CharSequence)this.itemName)) {
            if (bki2.hasCustomHoverName()) {
                integer5 = 1;
                integer3 += integer5;
                bki3.resetHoverName();
            }
        }
        else if (!this.itemName.equals(bki2.getHoverName().getString())) {
            integer5 = 1;
            integer3 += integer5;
            bki3.setHoverName(new TextComponent(this.itemName));
        }
        this.cost.set(integer4 + integer3);
        if (integer3 <= 0) {
            bki3 = ItemStack.EMPTY;
        }
        if (integer5 == integer3 && integer5 > 0 && this.cost.get() >= 40) {
            this.cost.set(39);
        }
        if (this.cost.get() >= 40 && !this.player.abilities.instabuild) {
            bki3 = ItemStack.EMPTY;
        }
        if (!bki3.isEmpty()) {
            int integer14 = bki3.getBaseRepairCost();
            if (!bki4.isEmpty() && integer14 < bki4.getBaseRepairCost()) {
                integer14 = bki4.getBaseRepairCost();
            }
            if (integer5 != integer3 || integer5 == 0) {
                integer14 = calculateIncreasedRepairCost(integer14);
            }
            bki3.setRepairCost(integer14);
            EnchantmentHelper.setEnchantments(map8, bki3);
        }
        this.resultSlots.setItem(0, bki3);
        this.broadcastChanges();
    }
    
    public static int calculateIncreasedRepairCost(final int integer) {
        return integer * 2 + 1;
    }
    
    public void setItemName(final String string) {
        this.itemName = string;
        if (this.getSlot(2).hasItem()) {
            final ItemStack bki3 = this.getSlot(2).getItem();
            if (StringUtils.isBlank((CharSequence)string)) {
                bki3.resetHoverName();
            }
            else {
                bki3.setHoverName(new TextComponent(this.itemName));
            }
        }
        this.createResult();
    }
    
    public int getCost() {
        return this.cost.get();
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
}
