package common.entity.npc;

import common.ai.AIFlyingBoxAttack;
import common.attributes.Attributes;
import common.entity.DamageSource;
import common.init.Config;
import common.item.ItemStack;
import common.model.ParticleType;
import common.nbt.NBTTagCompound;
import common.rng.Random;
import common.world.World;

public class EntityGargoyle extends EntityFlyingNPC
{
    public EntityGargoyle(World worldIn)
    {
        super(worldIn);
//		this.setSize(2.2f);
		this.tasks.addTask(7, new AIFlyingBoxAttack(this));
        this.noPickup = true;
    }

	public boolean isBoss() {
		return true;
	}

    public boolean isImmuneToFire()
    {
        return true;
    }

    protected void entityInit()
    {
        super.entityInit();
        this.dataWatcher.addObject(23, 0);
    }
    
    public void writeEntityToNBT(NBTTagCompound tagCompound)
    {
        super.writeEntityToNBT(tagCompound);
        tagCompound.setInteger("Invul", this.getInvulTime());
    }
    
    public void readEntityFromNBT(NBTTagCompound tagCompund)
    {
        super.readEntityFromNBT(tagCompund);
        this.setInvulTime(tagCompund.getInteger("Invul"));
    }
    
    public void onLivingUpdate()
    {
//        this.motionY *= 0.6000000238418579D;

//        if (!this.worldObj.client)
//        {
//        	EntityLivingBase entity = this.getAttackTarget();
//
//            if (entity != null)
//            {
//                if (this.posY < entity.posY || /* !this.isArmored() && */ this.posY < entity.posY + 5.0D)
//                {
//                    if (this.motionY < 0.0D)
//                    {
//                        this.motionY = 0.0D;
//                    }
//
//                    this.motionY += (0.5D - this.motionY) * 0.6000000238418579D;
//                }
//
//                double d0 = entity.posX - this.posX;
//                double d1 = entity.posZ - this.posZ;
//                double d3 = d0 * d0 + d1 * d1;
//
//                if (d3 > 9.0D)
//                {
//                    double d5 = (double)MathHelper.sqrt_double(d3);
//                    this.motionX += (d0 / d5 * 0.5D - this.motionX) * 0.6000000238418579D;
//                    this.motionZ += (d1 / d5 * 0.5D - this.motionZ) * 0.6000000238418579D;
//                }
//            }
//        }

//        if (this.motionX * this.motionX + this.motionZ * this.motionZ > 0.05000000074505806D)
//        {
//            this.rotYaw = (float)MathHelper.atan2(this.motionZ, this.motionX) * (180F / (float)Math.PI) - 90.0F;
//        }
        
        super.onLivingUpdate();

        for (int l = 0; l < 5; ++l)
        {
            this.worldObj.spawnParticle(ParticleType.SMOKE_NORMAL, this.posX + this.rand.gaussian() * 0.30000001192092896D, this.posY + this.height + this.rand.gaussian() * 0.30000001192092896D, this.posZ + this.rand.gaussian() * 0.30000001192092896D, 0.0D, 0.0D, 0.0D);
        }

        if (this.getInvulTime() > 0)
        {
            for (int i1 = 0; i1 < 3; ++i1)
            {
                this.worldObj.spawnParticle(ParticleType.SPELL_MOB, this.posX + this.rand.gaussian() * 1.0D, this.posY + (double)(this.rand.floatv() * 3.3F), this.posZ + this.rand.gaussian() * 1.0D, 0.699999988079071D, 0.699999988079071D, 0.8999999761581421D);
            }
        }
    }

    protected void updateAITasks()
    {
        super.updateAITasks();
        
        if (this.getInvulTime() > 0)
        {
            int j1 = this.getInvulTime() - 1;

            if (j1 <= 0)
            {
                this.worldObj.newExplosion(this, this.posX, this.posY + (double)this.getEyeHeight(), this.posZ, 7.0F, false, 
                		Config.mobGrief, false);
//                this.worldObj.broadcastSound(1013, new BlockPos(this), 0);
            }

            this.setInvulTime(j1);

            if (this.ticksExisted % 10 == 0)
            {
                this.heal(10);
            }
        }
        else
        {
            if (this.ticksExisted % 20 == 0)
            {
                this.heal(1);
            }
        }
    }
    
    public void setInWeb()
    {
    }
    
//    public void attackEntityWithRangedAttack(EntityLivingBase target, float p_82196_2_)
//    {
//        this.launchBoxToEntity(target);
//    }
    
    public boolean isRangedWeapon(ItemStack stack) {
    	return false;
    }
    
    public boolean attackEntityFrom(DamageSource source, int amount)
    {
//        if (this.isEntityInvulnerable(source))
//        {
//            return false;
//        }
//        else
        if (this.getInvulTime() > 0 && source != DamageSource.outOfWorld)
        {
            return false;
        }
        else
        {
            return super.attackEntityFrom(source, amount);
        }
    }

    protected void applyEntityAttributes()
    {
        super.applyEntityAttributes();
        this.getEntityAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(0.6000000238418579D);
        this.getEntityAttribute(Attributes.FOLLOW_RANGE).setBaseValue(40.0D);
    }

    public int getInvulTime()
    {
        return this.dataWatcher.getWatchableObjectInt(23);
    }

    public void setInvulTime(int time)
    {
        this.dataWatcher.updateObject(23, Integer.valueOf(time));
    }
    
//    public boolean isPotionApplicable(PotionEffect potioneffectIn) {
//    	return potioneffectIn.getPotionID() != Potion.moveSlowdown.id;
//    }
    
    public boolean isCharged() {
    	return this.getInvulTime() > 0;
    }
    
	public int getColor() {
		return 0xc13633;
	}

//	public boolean isAggressive() {
//		return true;
//	}
//
//	public boolean isPeaceful() {
//		return false;
//	}

	public boolean isAggressive(Class<? extends EntityNPC> clazz) {
		return true;
	}

//	public boolean isPeaceful(Class<? extends EntityNPC> clazz) {
//		return false;
//	}

//	public boolean canInfight(Alignment align) {
//		return false;
//	}
//
//	public boolean canMurder(Alignment align) {
//		return false;
//	}

//	public boolean canUseMagic() {
//		return false;
//	}

	public int getBaseHealth(Random rand) {
		return rand.range(300, 400);
	}

//	public int getBaseEnergy(Random rand) {
//		return 0;
//	}

    public float getHeightDeviationMax() {
    	return 0.05f;
    }
    
    public float getSpeciesBaseSize() {
    	return 2.2f;
    }

	public Alignment getNaturalAlign() {
		return Alignment.CHAOTIC_EVIL;
	}
    
//    public boolean canAmbush(EntityLiving entity) {
//    	return true;
//    }
}
