package common.pattern;

import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Predicate;

import common.block.Block;
import common.collect.Maps;
import common.properties.IProperty;
import common.world.State;

public class BlockStateHelper implements Predicate<State>
{
    private final Block block;
    private final Map<IProperty, Predicate> predicates = Maps.<IProperty, Predicate>newHashMap();

    private BlockStateHelper(Block blockStateIn)
    {
        this.block = blockStateIn;
    }

    public static BlockStateHelper forBlock(Block blockIn)
    {
        return new BlockStateHelper(blockIn);
    }

    public boolean test(State p_apply_1_)
    {
        if (p_apply_1_ != null && p_apply_1_.getBlock().equals(this.block))
        {
            for (Entry<IProperty, Predicate> entry : this.predicates.entrySet())
            {
                Object object = p_apply_1_.getValue((IProperty)entry.getKey());

                if (!((Predicate)entry.getValue()).test(object))
                {
                    return false;
                }
            }

            return true;
        }
        else
        {
            return false;
        }
    }

    public <V extends Comparable<V>> BlockStateHelper where(IProperty<V> property, Predicate <? extends V > is)
    {
        if (!this.block.getPropertyMap().contains(property))
        {
            throw new IllegalArgumentException(this.block + " cannot support property " + property);
        }
        else
        {
            this.predicates.put(property, is);
            return this;
        }
    }
}
