package com.zivilon.cinder_loe.blocks.plants;

import com.zivilon.cinder_loe.CinderLoE;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.block.BlockCrops;
import net.minecraft.block.IGrowable;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.util.IIcon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.EnumPlantType;

import java.util.Random;

public class onionCrop extends BlockCrops implements IGrowable {
    private IIcon[] icons;

    public onionCrop() {
        setBlockName("onionPlant");
        setBlockTextureName("lotr:onion");
        setTickRandomly(true);
        setHardness(0.0F);
        setStepSound(soundTypeGrass);
        disableStats();
    }

    @Override
    public void registerBlockIcons(IIconRegister iconRegister) {
        icons = new IIcon[4];
        for (int i = 0; i < 4; ++i) {
            icons[i] = iconRegister.registerIcon("lotr:onion_" + i);
        }
    }
    @Override
    public IIcon getIcon(int side, int meta) {
        return icons[Math.min(meta, 3)];
    }

    @Override
    public boolean canPlaceBlockOn(Block block) {
        return block == Blocks.farmland;
    }

    @Override
    public void updateTick(World world, int x, int y, int z, Random rand) {
        if (!world.isRemote && world.getBlockLightValue(x, y + 1, z) >= 9) {
            int meta = world.getBlockMetadata(x, y, z);
            if (meta < 3 && rand.nextInt(5) == 0) {
                world.setBlockMetadataWithNotify(x, y, z, meta + 1, 2);
            }
        }
    }

    @Override
    public boolean func_149851_a(World world, int x, int y, int z, boolean isClient) {
        return world.getBlockMetadata(x, y, z) < 3;
    }

    @Override
    public boolean func_149852_a(World world, Random rand, int x, int y, int z) {
        return true;
    }

    @Override
    public void func_149853_b(World world, Random rand, int x, int y, int z) {
        int meta = world.getBlockMetadata(x, y, z);
        if (meta < 3) {
            world.setBlockMetadataWithNotify(x, y, z, meta + 1, 2);
        }
    }

    @Override
    public Item getItemDropped(int meta, Random rand, int fortune) {
        return meta >= 3 ? CinderLoE.onion : null;
    }

    @Override
    public int quantityDropped(Random rand) {
        return 1 + rand.nextInt(3); // 1–3 onions
    }

    @Override
    public Item getItem(World world, int x, int y, int z) {
        return CinderLoE.onion;
    }
}
