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.DoublePlantBlock;
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 dmn1 = new BlockColors();
        dmn1.register((cfj, bpe, fu, integer) -> {
            if (bpe == null || fu == null) {
                return -1;
            }
            else {
                return BiomeColors.getAverageGrassColor(bpe, (cfj.<DoubleBlockHalf>getValue(DoublePlantBlock.HALF) == DoubleBlockHalf.UPPER) ? fu.below() : fu);
            }
        }, Blocks.LARGE_FERN, Blocks.TALL_GRASS);
        dmn1.addColoringState(DoublePlantBlock.HALF, Blocks.LARGE_FERN, Blocks.TALL_GRASS);
        dmn1.register((cfj, bpe, fu, integer) -> {
            if (bpe == null || fu == null) {
                return GrassColor.get(0.5, 1.0);
            }
            else {
                return BiomeColors.getAverageGrassColor(bpe, fu);
            }
        }, Blocks.GRASS_BLOCK, Blocks.FERN, Blocks.GRASS, Blocks.POTTED_FERN);
        dmn1.register((cfj, bpe, fu, integer) -> FoliageColor.getEvergreenColor(), Blocks.SPRUCE_LEAVES);
        dmn1.register((cfj, bpe, fu, integer) -> FoliageColor.getBirchColor(), Blocks.BIRCH_LEAVES);
        dmn1.register((cfj, bpe, fu, integer) -> {
            if (bpe == null || fu == null) {
                return FoliageColor.getDefaultColor();
            }
            else {
                return BiomeColors.getAverageFoliageColor(bpe, fu);
            }
        }, Blocks.OAK_LEAVES, Blocks.JUNGLE_LEAVES, Blocks.ACACIA_LEAVES, Blocks.DARK_OAK_LEAVES, Blocks.VINE);
        dmn1.register((cfj, bpe, fu, integer) -> {
            if (bpe == null || fu == null) {
                return -1;
            }
            else {
                return BiomeColors.getAverageWaterColor(bpe, fu);
            }
        }, Blocks.WATER, Blocks.BUBBLE_COLUMN, Blocks.CAULDRON);
        dmn1.register((cfj, bpe, fu, integer) -> RedStoneWireBlock.getColorForPower(cfj.<Integer>getValue((Property<Integer>)RedStoneWireBlock.POWER)), Blocks.REDSTONE_WIRE);
        dmn1.addColoringState(RedStoneWireBlock.POWER, Blocks.REDSTONE_WIRE);
        dmn1.register((cfj, bpe, fu, integer) -> {
            if (bpe == null || fu == null) {
                return -1;
            }
            else {
                return BiomeColors.getAverageGrassColor(bpe, fu);
            }
        }, Blocks.SUGAR_CANE);
        dmn1.register((cfj, bpe, fu, integer) -> 14731036, Blocks.ATTACHED_MELON_STEM, Blocks.ATTACHED_PUMPKIN_STEM);
        final int integer5;
        final int integer6;
        final int integer7;
        final int integer8;
        dmn1.register((cfj, bpe, fu, integer) -> {
            integer5 = cfj.<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);
        dmn1.addColoringState(StemBlock.AGE, Blocks.MELON_STEM, Blocks.PUMPKIN_STEM);
        dmn1.register((cfj, bpe, fu, integer) -> {
            if (bpe == null || fu == null) {
                return 7455580;
            }
            else {
                return 2129968;
            }
        }, Blocks.LILY_PAD);
        return dmn1;
    }
    
    public int getColor(final BlockState cfj, final Level bqb, final BlockPos fu) {
        final BlockColor dmm5 = this.blockColors.byId(Registry.BLOCK.getId(cfj.getBlock()));
        if (dmm5 != null) {
            return dmm5.getColor(cfj, null, null, 0);
        }
        final MaterialColor cxe6 = cfj.getMapColor(bqb, fu);
        return (cxe6 != null) ? cxe6.col : -1;
    }
    
    public int getColor(final BlockState cfj, @Nullable final BlockAndTintGetter bpe, @Nullable final BlockPos fu, final int integer) {
        final BlockColor dmm6 = this.blockColors.byId(Registry.BLOCK.getId(cfj.getBlock()));
        return (dmm6 == null) ? -1 : dmm6.getColor(cfj, bpe, fu, integer);
    }
    
    public void register(final BlockColor dmm, final Block... arr) {
        for (final Block bvr7 : arr) {
            this.blockColors.addMapping(dmm, Registry.BLOCK.getId(bvr7));
        }
    }
    
    private void addColoringStates(final Set<Property<?>> set, final Block... arr) {
        for (final Block bvr7 : arr) {
            this.coloringStates.put(bvr7, set);
        }
    }
    
    private void addColoringState(final Property<?> cgl, final Block... arr) {
        this.addColoringStates(ImmutableSet.<Property<?>>of(cgl), arr);
    }
    
    public Set<Property<?>> getColoringProperties(final Block bvr) {
        return this.coloringStates.getOrDefault(bvr, ImmutableSet.of());
    }
}
