package com.zivilon.cinder_loe.blocks;

import com.zivilon.cinder_loe.LoECreativeTabs;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.IIcon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;

import net.minecraftforge.common.util.ForgeDirection;

import lotr.common.LOTRCreativeTabs;
import lotr.common.block.LOTRBlockOrcChain;

import com.zivilon.cinder_loe.util.Utilities;

public abstract class ChandelierBase extends Block {
    public String textureName;
    public IIcon icon;

    public ChandelierBase(String blockName) {
        super(Material.circuits);
        this.textureName = Utilities.toSnakeCase(blockName);
        this.setBlockName(blockName);
        this.setCreativeTab(LoECreativeTabs.tabBlockLoE);
        setHardness(0.0F);
        setResistance(2.0F);
        setStepSound(Block.soundTypeMetal);
        setLightLevel(0.9375F);
        setBlockBounds(0.0625F, 0.1875F, 0.0625F, 0.9375F, 1.0F, 0.9375F);
    }

    public IIcon getIcon(int i, int j) {
        return this.icon;
    }

    @Override
    public void registerBlockIcons(IIconRegister reg) {
        icon = reg.registerIcon(this.textureName);
    }

    public boolean isOpaqueCube() {
        return false;
    }

    public boolean renderAsNormalBlock() {
        return false;
    }

    public int getRenderType() {
        return 1;
    }

    public AxisAlignedBB getCollisionBoundingBoxFromPool(World world, int i, int j, int k) {
        return null;
    }

    public boolean canBlockStay(World world, int i, int j, int k) {
        Block block = world.getBlock(i, j + 1, k);
        int meta = world.getBlockMetadata(i, j + 1, k);
        if (block instanceof net.minecraft.block.BlockFence || block instanceof net.minecraft.block.BlockWall)
            return true;
        if (block instanceof net.minecraft.block.BlockSlab && !block.isOpaqueCube() && (meta & 0x8) == 0)
            return true;
        if (block instanceof net.minecraft.block.BlockStairs && (meta & 0x4) == 0)
            return true;
        if (block instanceof LOTRBlockOrcChain)
            return true;
        return world.getBlock(i, j + 1, k).isSideSolid((IBlockAccess)world, i, j + 1, k, ForgeDirection.DOWN);
    }

    public boolean canPlaceBlockAt(World world, int i, int j, int k) {
        return canBlockStay(world, i, j, k);
    }

    public void onNeighborBlockChange(World world, int i, int j, int k, Block block) {
        if (!canBlockStay(world, i, j, k)) {
            dropBlockAsItem(world, i, j, k, world.getBlockMetadata(i, j, k), 0);
            world.setBlockToAir(i, j, k);
        }
    }

    @SideOnly(Side.CLIENT)
    public void randomDisplayTick(World world, int i, int j, int k, Random random) {
        double d = 0.13D;
        double d1 = 1.0D - d;
        double d2 = 0.6875D;
        spawnChandelierParticles(world, i + d, j + d2, k + d);
        spawnChandelierParticles(world, i + d1, j + d2, k + d1);
        spawnChandelierParticles(world, i + d, j + d2, k + d1);
        spawnChandelierParticles(world, i + d1, j + d2, k + d);
    }

    private void spawnChandelierParticles(World world, double d, double d1, double d2) {
        world.spawnParticle("smoke", d, d1, d2, 0.0D, 0.0D, 0.0D);
        world.spawnParticle("flame", d, d1, d2, 0.0D, 0.0D, 0.0D);
    }
}
