package net.minecraft.client.color.block;

import com.google.common.collect.ImmutableSet;
import javax.annotation.Nullable;
import net.minecraft.world.level.material.MaterialColor;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.BlockAndTintGetter;
import net.minecraft.core.Registry;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.StemBlock;
import net.minecraft.world.level.block.RedStoneWireBlock;
import net.minecraft.world.level.FoliageColor;
import net.minecraft.world.level.GrassColor;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.client.renderer.BiomeColors;
import net.minecraft.world.level.block.state.properties.DoubleBlockHalf;
import net.minecraft.world.level.block.ShearableDoublePlantBlock;
import com.google.common.collect.Maps;
import net.minecraft.world.level.block.state.properties.Property;
import java.util.Set;
import net.minecraft.world.level.block.Block;
import java.util.Map;
import net.minecraft.core.IdMapper;

public class BlockColors {
    private final IdMapper<BlockColor> blockColors;
    private final Map<Block, Set<Property<?>>> coloringStates;
    
    public BlockColors() {
        this.blockColors = new IdMapper<BlockColor>(32);
        this.coloringStates = Maps.newHashMap();
    }
    
    public static BlockColors createDefault() {
        final BlockColors dcb1 = new BlockColors();
        dcb1.register((byj, bje, fk, integer) -> {
            if (bje == null || fk == null) {
                return -1;
            }
            else {
                return BiomeColors.getAverageGrassColor(bje, (byj.<DoubleBlockHalf>getValue(ShearableDoublePlantBlock.HALF) == DoubleBlockHalf.UPPER) ? fk.below() : fk);
            }
        }, Blocks.LARGE_FERN, Blocks.TALL_GRASS);
        dcb1.addColoringState(ShearableDoublePlantBlock.HALF, Blocks.LARGE_FERN, Blocks.TALL_GRASS);
        dcb1.register((byj, bje, fk, integer) -> {
            if (bje == null || fk == null) {
                return GrassColor.get(0.5, 1.0);
            }
            else {
                return BiomeColors.getAverageGrassColor(bje, fk);
            }
        }, Blocks.GRASS_BLOCK, Blocks.FERN, Blocks.GRASS, Blocks.POTTED_FERN);
        dcb1.register((byj, bje, fk, integer) -> FoliageColor.getEvergreenColor(), Blocks.SPRUCE_LEAVES);
        dcb1.register((byj, bje, fk, integer) -> FoliageColor.getBirchColor(), Blocks.BIRCH_LEAVES);
        dcb1.register((byj, bje, fk, integer) -> {
            if (bje == null || fk == null) {
                return FoliageColor.getDefaultColor();
            }
            else {
                return BiomeColors.getAverageFoliageColor(bje, fk);
            }
        }, Blocks.OAK_LEAVES, Blocks.JUNGLE_LEAVES, Blocks.ACACIA_LEAVES, Blocks.DARK_OAK_LEAVES, Blocks.VINE);
        dcb1.register((byj, bje, fk, integer) -> {
            if (bje == null || fk == null) {
                return -1;
            }
            else {
                return BiomeColors.getAverageWaterColor(bje, fk);
            }
        }, Blocks.WATER, Blocks.BUBBLE_COLUMN, Blocks.CAULDRON);
        dcb1.register((byj, bje, fk, integer) -> RedStoneWireBlock.getColorForData(byj.<Integer>getValue((Property<Integer>)RedStoneWireBlock.POWER)), Blocks.REDSTONE_WIRE);
        dcb1.addColoringState(RedStoneWireBlock.POWER, Blocks.REDSTONE_WIRE);
        dcb1.register((byj, bje, fk, integer) -> {
            if (bje == null || fk == null) {
                return -1;
            }
            else {
                return BiomeColors.getAverageGrassColor(bje, fk);
            }
        }, Blocks.SUGAR_CANE);
        dcb1.register((byj, bje, fk, integer) -> 14731036, Blocks.ATTACHED_MELON_STEM, Blocks.ATTACHED_PUMPKIN_STEM);
        final int integer5;
        final int integer6;
        final int integer7;
        final int integer8;
        dcb1.register((byj, bje, fk, integer) -> {
            integer5 = byj.<Integer>getValue((Property<Integer>)StemBlock.AGE);
            integer6 = integer5 * 32;
            integer7 = 255 - integer5 * 8;
            integer8 = integer5 * 4;
            return integer6 << 16 | integer7 << 8 | integer8;
        }, Blocks.MELON_STEM, Blocks.PUMPKIN_STEM);
        dcb1.addColoringState(StemBlock.AGE, Blocks.MELON_STEM, Blocks.PUMPKIN_STEM);
        dcb1.register((byj, bje, fk, integer) -> {
            if (bje == null || fk == null) {
                return 7455580;
            }
            else {
                return 2129968;
            }
        }, Blocks.LILY_PAD);
        return dcb1;
    }
    
    public int getColor(final BlockState byj, final Level bjw, final BlockPos fk) {
        final BlockColor dca5 = this.blockColors.byId(Registry.BLOCK.getId(byj.getBlock()));
        if (dca5 != null) {
            return dca5.getColor(byj, null, null, 0);
        }
        final MaterialColor cop6 = byj.getMapColor(bjw, fk);
        return (cop6 != null) ? cop6.col : -1;
    }
    
    public int getColor(final BlockState byj, @Nullable final BlockAndTintGetter bje, @Nullable final BlockPos fk, final int integer) {
        final BlockColor dca6 = this.blockColors.byId(Registry.BLOCK.getId(byj.getBlock()));
        return (dca6 == null) ? -1 : dca6.getColor(byj, bje, fk, integer);
    }
    
    public void register(final BlockColor dca, final Block... arr) {
        for (final Block bph7 : arr) {
            this.blockColors.addMapping(dca, Registry.BLOCK.getId(bph7));
        }
    }
    
    private void addColoringStates(final Set<Property<?>> set, final Block... arr) {
        for (final Block bph7 : arr) {
            this.coloringStates.put(bph7, set);
        }
    }
    
    private void addColoringState(final Property<?> bzm, final Block... arr) {
        this.addColoringStates(ImmutableSet.<Property<?>>of(bzm), arr);
    }
    
    public Set<Property<?>> getColoringProperties(final Block bph) {
        return this.coloringStates.getOrDefault(bph, ImmutableSet.of());
    }
}
