package common.entity.effect;

import java.util.List;

import common.entity.Entity;
import common.entity.types.EntityLiving;
import common.entity.types.EntityWeatherEffect;
import common.init.Blocks;
import common.init.Config;
import common.init.SoundEvent;
import common.material.Material;
import common.util.BlockPos;
import common.util.BoundingBox;
import common.world.AWorldClient;
import common.world.World;

public class EntityLightning extends EntityWeatherEffect
{
    public final int color;
    public final int damage;
    public final boolean fire;
    public final EntityLiving summoner;
    
    private int lightningState;
    public long boltVertex;
    private int boltLivingTime;

    public EntityLightning(World worldIn, double posX, double posY, double posZ, int color, int damage, boolean fire, EntityLiving summoner)
    {
        super(worldIn);
        this.setLocationAndAngles(posX, posY, posZ, 0.0F, 0.0F);
        this.lightningState = 2;
        this.color = color;
        this.fire = fire;
        this.damage = damage;
        this.summoner = summoner;
        this.boltVertex = this.rand.longv();
        this.boltLivingTime = this.rand.roll(3);
    }

    /**
     * Called to update the entity's position/logic.
     */
    public void onUpdate()
    {
        super.onUpdate();

        if (this.lightningState == 2)
        {
            this.worldObj.playSound(SoundEvent.THUNDER, this.posX, this.posY, this.posZ, 10000.0F);
            this.worldObj.playSound(SoundEvent.EXPLODE, this.posX, this.posY, this.posZ, 2.0F);
        }

        --this.lightningState;

        if (this.lightningState < 0)
        {
            if (this.boltLivingTime == 0)
            {
                this.setDead();
            }
            else if (this.lightningState < -this.rand.zrange(10))
            {
                --this.boltLivingTime;
                this.lightningState = 1;
                this.boltVertex = this.rand.longv();
                BlockPos blockpos = new BlockPos(this);

                if (!this.worldObj.client && this.fire && Config.fire && this.worldObj.isAreaLoaded(blockpos, 10) && this.worldObj.getState(blockpos).getBlock().getMaterial() == Material.air && Blocks.fire.canPlaceBlockAt(this.worldObj, blockpos))
                {
                    this.worldObj.setState(blockpos, Blocks.fire.getState());
                }
            }
        }

        if (this.lightningState >= 0)
        {
            if (this.worldObj.client)
            {
                ((AWorldClient)this.worldObj).setLastLightning(2, this.color);
            }
            else // if(this.damage > 0)
            {
                double d0 = 3.0D;
                List<Entity> list = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, new BoundingBox(this.posX - d0, this.posY - d0, this.posZ - d0, this.posX + d0, this.posY + 6.0D + d0, this.posZ + d0));

                for (int i = 0; i < list.size(); ++i)
                {
                    Entity entity = (Entity)list.get(i);
                    if(entity != this.summoner)
                    	entity.onStruckByLightning(this);
                }
            }
        }
    }
}
