package common.block;

import java.util.Collection;
import java.util.List;
import java.util.function.Predicate;

import common.collect.Filter;
import common.collect.Lists;
import common.init.Blocks;
import common.init.Config;
import common.item.CheatTab;
import common.item.Item;
import common.item.ItemStack;
import common.model.Model;
import common.model.ModelProvider;
import common.properties.IProperty;
import common.properties.PropertyEnum;
import common.rng.Random;
import common.util.BlockPos;
import common.util.Identifyable;
import common.world.State;
import common.world.AWorldServer;

public abstract class BlockFlower extends BlockBush
{
    protected PropertyEnum<BlockFlower.EnumFlowerType> type;

    public BlockFlower()
    {
        this.setDefaultState(this.getBaseState().withProperty(this.getTypeProperty(), /* this.getBlockType() == BlockFlower.EnumFlowerColor.RED ? BlockFlower.EnumFlowerType.ROSE : */ BlockFlower.EnumFlowerType.DANDELION));
//        this.setTickRandomly();
    }

    public void updateTick(AWorldServer worldIn, BlockPos pos, State state, Random rand)
    {
        if(Config.flowerDry && worldIn.getTemperatureC(pos) >= 50.0f)
        {
        	worldIn.setState(pos, worldIn.rand.chance(3) ? Blocks.air.getState() : 
        			Blocks.tallgrass.getState().withProperty(BlockTallGrass.TYPE, BlockTallGrass.EnumType.DEAD_BUSH));
        	return;
        }
        super.updateTick(worldIn, pos, state, rand);
    }

    /**
     * Gets the metadata of the item this Block can drop. This method is called when the block gets destroyed. It
     * returns the metadata of the dropped item based on the old metadata of the block.
     */
    public int damageDropped(State state)
    {
        return ((BlockFlower.EnumFlowerType)state.getValue(this.getTypeProperty())).getMeta();
    }

    /**
     * returns a list of blocks with the same ID, but different meta (eg: wood returns 4 blocks)
     */
    public void getSubBlocks(Item itemIn, CheatTab tab, List<ItemStack> list)
    {
        for (BlockFlower.EnumFlowerType blockflower$enumflowertype : BlockFlower.EnumFlowerType.getTypes(this.getBlockType()))
        {
            list.add(new ItemStack(itemIn, 1, blockflower$enumflowertype.getMeta()));
        }
    }

    /**
     * Convert the given metadata into a BlockState for this Block
     */
    public State getStateFromMeta(int meta)
    {
        return this.getState().withProperty(this.getTypeProperty(), BlockFlower.EnumFlowerType.getType(this.getBlockType(), meta));
    }

    /**
     * Get the Type of this flower (Yellow/Red)
     */
    public abstract BlockFlower.EnumFlowerColor getBlockType();

    public IProperty<BlockFlower.EnumFlowerType> getTypeProperty()
    {
        if (this.type == null)
        {
            this.type = PropertyEnum.<BlockFlower.EnumFlowerType>create("type", BlockFlower.EnumFlowerType.class, new Predicate<BlockFlower.EnumFlowerType>()
            {
                public boolean test(BlockFlower.EnumFlowerType p_apply_1_)
                {
                    return p_apply_1_.getBlockType() == BlockFlower.this.getBlockType();
                }
            });
        }

        return this.type;
    }

    /**
     * Convert the BlockState into the correct metadata value
     */
    public int getMetaFromState(State state)
    {
        return ((BlockFlower.EnumFlowerType)state.getValue(this.getTypeProperty())).getMeta();
    }

    protected IProperty[] getProperties()
    {
        return new IProperty[] {this.getTypeProperty()};
    }
    
//    public EnumOffsetType getOffsetType()
//    {
//        return EnumOffsetType.XZ;
//    }

    public static enum EnumFlowerColor
    {
        BASE;

        public BlockFlower getBlock()
        {
            return Blocks.flower; // this == BASE ? Blocks.flower : null;
        }
    }

    public Model getModel(ModelProvider provider, String name, State state) {
    	return provider.getModel(state.getValue(this.type).getName()).cross();
    }

    public static enum EnumFlowerType implements Identifyable
    {
        DANDELION(BlockFlower.EnumFlowerColor.BASE, 0, "dandelion", "Löwenzahn"),
        ROSE(BlockFlower.EnumFlowerColor.BASE, 1, "rose", "Rose"),
        POPPY(BlockFlower.EnumFlowerColor.BASE, 2, "poppy", "Mohn"),
        BLUE_ORCHID(BlockFlower.EnumFlowerColor.BASE, 3, "blue_orchid", "Blaue Orchidee"),
        ALLIUM(BlockFlower.EnumFlowerColor.BASE, 4, "allium", "Sternlauch"),
        HOUSTONIA(BlockFlower.EnumFlowerColor.BASE, 5, "houstonia", "Porzellansternchen"),
        RED_TULIP(BlockFlower.EnumFlowerColor.BASE, 6, "red_tulip", "Rote Tulpe"),
        ORANGE_TULIP(BlockFlower.EnumFlowerColor.BASE, 7, "orange_tulip", "Orange Tulpe"),
        WHITE_TULIP(BlockFlower.EnumFlowerColor.BASE, 8, "white_tulip", "Weiße Tulpe"),
        PINK_TULIP(BlockFlower.EnumFlowerColor.BASE, 9, "pink_tulip", "Rosa Tulpe"),
        OXEYE_DAISY(BlockFlower.EnumFlowerColor.BASE, 10, "oxeye_daisy", "Margerite"),
        BLACK_LOTUS(BlockFlower.EnumFlowerColor.BASE, 11, "black_lotus", "Schwarzer Lotus");

        private static final BlockFlower.EnumFlowerType[][] TYPES_FOR_BLOCK = new BlockFlower.EnumFlowerType[BlockFlower.EnumFlowerColor.values().length][];
        private final BlockFlower.EnumFlowerColor blockType;
        private final int meta;
        private final String name;
        private final String display;

//        private EnumFlowerType(BlockFlower.EnumFlowerColor blockType, int meta, String name)
//        {
//            this(blockType, meta, name, name);
//        }

        private EnumFlowerType(BlockFlower.EnumFlowerColor blockType, int meta, String name, String display)
        {
            this.blockType = blockType;
            this.meta = meta;
            this.name = name;
            this.display = display;
        }

        public BlockFlower.EnumFlowerColor getBlockType()
        {
            return this.blockType;
        }

        public int getMeta()
        {
            return this.meta;
        }

        public static BlockFlower.EnumFlowerType getType(BlockFlower.EnumFlowerColor blockType, int meta)
        {
            BlockFlower.EnumFlowerType[] ablockflower$enumflowertype = TYPES_FOR_BLOCK[blockType.ordinal()];

            if (meta < 0 || meta >= ablockflower$enumflowertype.length)
            {
                meta = 0;
            }

            return ablockflower$enumflowertype[meta];
        }

        public static BlockFlower.EnumFlowerType[] getTypes(BlockFlower.EnumFlowerColor flowerColor)
        {
            return TYPES_FOR_BLOCK[flowerColor.ordinal()];
        }

        public String toString()
        {
            return this.name;
        }

        public String getName()
        {
            return this.name;
        }

        public String getDisplay()
        {
            return this.display;
        }

        static {
            for (final BlockFlower.EnumFlowerColor blockflower$enumflowercolor : BlockFlower.EnumFlowerColor.values())
            {
                Collection<BlockFlower.EnumFlowerType> collection = Filter.<BlockFlower.EnumFlowerType>filter(Lists.newArrayList(values()), new Predicate<BlockFlower.EnumFlowerType>()
                {
                    public boolean test(BlockFlower.EnumFlowerType p_apply_1_)
                    {
                        return p_apply_1_.getBlockType() == blockflower$enumflowercolor;
                    }
                });
                TYPES_FOR_BLOCK[blockflower$enumflowercolor.ordinal()] = (BlockFlower.EnumFlowerType[])collection.toArray(new BlockFlower.EnumFlowerType[collection.size()]);
            }
        }
    }
}
