package net.minecraft.client.color.item;

import net.minecraft.world.item.Item;
import net.minecraft.core.Registry;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.BlockState;
import java.util.Iterator;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.MapItem;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.SpawnEggItem;
import net.minecraft.world.item.alchemy.PotionUtils;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.GrassColor;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.item.DyeableLeatherItem;
import net.minecraft.client.color.block.BlockColors;
import net.minecraft.core.IdMapper;

public class ItemColors {
    private final IdMapper<ItemColor> itemColors;
    
    public ItemColors() {
        this.itemColors = new IdMapper<ItemColor>(32);
    }
    
    public static ItemColors createDefault(final BlockColors dmn) {
        final ItemColors dmr2 = new ItemColors();
        dmr2.register((bki, integer) -> (integer > 0) ? -1 : ((DyeableLeatherItem)bki.getItem()).getColor(bki), Items.LEATHER_HELMET, Items.LEATHER_CHESTPLATE, Items.LEATHER_LEGGINGS, Items.LEATHER_BOOTS, Items.LEATHER_HORSE_ARMOR);
        dmr2.register((bki, integer) -> GrassColor.get(0.5, 1.0), Blocks.TALL_GRASS, Blocks.LARGE_FERN);
        CompoundTag le3;
        int[] arr4;
        int integer2;
        int integer3;
        int integer4;
        final int[] array;
        int length;
        int i = 0;
        int integer5;
        int integer6;
        int integer7;
        int integer8;
        dmr2.register((bki, integer) -> {
            if (integer != 1) {
                return -1;
            }
            else {
                le3 = bki.getTagElement("Explosion");
                arr4 = (int[])((le3 != null && le3.contains("Colors", 11)) ? le3.getIntArray("Colors") : null);
                if (arr4 == null || arr4.length == 0) {
                    return 9079434;
                }
                else if (arr4.length == 1) {
                    return arr4[0];
                }
                else {
                    integer2 = 0;
                    integer3 = 0;
                    integer4 = 0;
                    for (length = array.length; i < length; ++i) {
                        integer5 = array[i];
                        integer2 += (integer5 & 0xFF0000) >> 16;
                        integer3 += (integer5 & 0xFF00) >> 8;
                        integer4 += (integer5 & 0xFF) >> 0;
                    }
                    integer6 = integer2 / arr4.length;
                    integer7 = integer3 / arr4.length;
                    integer8 = integer4 / arr4.length;
                    return integer6 << 16 | integer7 << 8 | integer8;
                }
            }
        }, Items.FIREWORK_STAR);
        dmr2.register((bki, integer) -> (integer > 0) ? -1 : PotionUtils.getColor(bki), Items.POTION, Items.SPLASH_POTION, Items.LINGERING_POTION);
        for (final SpawnEggItem blh4 : SpawnEggItem.eggs()) {
            dmr2.register((bki, integer) -> blh4.getColor(integer), blh4);
        }
        final BlockState cfj4;
        dmr2.register((bki, integer) -> {
            cfj4 = ((BlockItem)bki.getItem()).getBlock().defaultBlockState();
            return dmn.getColor(cfj4, null, null, integer);
        }, Blocks.GRASS_BLOCK, Blocks.GRASS, Blocks.FERN, Blocks.VINE, Blocks.OAK_LEAVES, Blocks.SPRUCE_LEAVES, Blocks.BIRCH_LEAVES, Blocks.JUNGLE_LEAVES, Blocks.ACACIA_LEAVES, Blocks.DARK_OAK_LEAVES, Blocks.LILY_PAD);
        dmr2.register((bki, integer) -> (integer == 0) ? PotionUtils.getColor(bki) : -1, Items.TIPPED_ARROW);
        dmr2.register((bki, integer) -> (integer == 0) ? -1 : MapItem.getColor(bki), Items.FILLED_MAP);
        return dmr2;
    }
    
    public int getColor(final ItemStack bki, final int integer) {
        final ItemColor dmq4 = this.itemColors.byId(Registry.ITEM.getId(bki.getItem()));
        return (dmq4 == null) ? -1 : dmq4.getColor(bki, integer);
    }
    
    public void register(final ItemColor dmq, final ItemLike... arr) {
        for (final ItemLike bqa7 : arr) {
            this.itemColors.addMapping(dmq, Item.getId(bqa7.asItem()));
        }
    }
}
