package common.entity.npc;

import common.ai.EntityAIExplode;
import common.entity.Entity;
import common.entity.effect.EntityLightning;
import common.entity.types.EntityLiving;
import common.init.Items;
import common.init.SoundEvent;
import common.item.ItemStack;
import common.rng.Random;
import common.tags.TagObject;
import common.vars.Vars;
import common.world.World;
import common.world.AWorldServer;

public class EntityHaunter extends EntityNPC {
//    private int lastActiveTime;
    private int timeSinceIgnited;
    private int fuseTime = 30;
    
	public EntityHaunter(World worldIn) {
		super(worldIn);
        this.tasks.addTask(2, new EntityAIExplode(this));
//        this.tasks.addTask(3, new EntityAIAvoidEntity(this, EntityOcelot.class, 6.0F, 1.0D, 1.2D));
	}

    protected void applyEntityAttributes()
    {
        super.applyEntityAttributes();
        this.setSpeedBase(0.25f);
    }
    
    public int getMaxFallHeight()
    {
        return this.getAttackTarget() == null ? 3 : 3 + (this.getHealth() - 1);
    }

    public void fall(float distance, float damageMultiplier)
    {
        super.fall(distance, damageMultiplier);
        this.timeSinceIgnited = (int)((float)this.timeSinceIgnited + distance * 1.5F);

        if (this.timeSinceIgnited > this.fuseTime - 5)
        {
            this.timeSinceIgnited = this.fuseTime - 5;
        }
    }

    protected void entityInit()
    {
        super.entityInit();
        this.dataWatcher.addObject(23, Byte.valueOf((byte) - 1));
//        this.dataWatcher.addObject(24, Byte.valueOf((byte)0));
        this.dataWatcher.addObject(24, Byte.valueOf((byte)0));
    }

    /**
     * (abstract) Protected helper method to write subclass entity data to NBT.
     */
    public void writeEntity(TagObject tagCompound)
    {
        super.writeEntity(tagCompound);

        if (this.isCharged())
        {
            tagCompound.setBool("Charge", true);
        }

        tagCompound.setShort("Fuse", (short)this.fuseTime);
        tagCompound.setBool("ignited", this.hasIgnited());
    }

    /**
     * (abstract) Protected helper method to read subclass entity data from NBT.
     */
    public void readEntity(TagObject tagCompund)
    {
        super.readEntity(tagCompund);
        this.setCharged(tagCompund.getBool("Charge"));

        this.fuseTime = tagCompund.getShort("Fuse");

        if (tagCompund.getBool("ignited"))
        {
            this.ignite();
        }
    }

    /**
     * Called to update the entity's position/logic.
     */
    public void onUpdate()
    {
        if (this.isEntityAlive())
        {
//            this.lastActiveTime = this.timeSinceIgnited;

            if (this.hasIgnited())
            {
                this.setExplodeState(1);
            }

            int i = this.getExplodeState();

            if (i > 0 && this.timeSinceIgnited == 0)
            {
                this.playSound(SoundEvent.FUSE, 1.0F);
            }

            this.timeSinceIgnited += i;

            if (this.timeSinceIgnited < 0)
            {
                this.timeSinceIgnited = 0;
            }

            if (this.timeSinceIgnited >= this.fuseTime)
            {
                this.timeSinceIgnited = this.fuseTime;
                this.explode();
            }
            
            if(!this.worldObj.client && i <= 0 && this.rand.chance(20)) {
            	EntityLiving target = this.getAttackTarget();
            	if(target != null && this.getDistanceSqToEntity(target) >= 100.0)
            		this.teleportToEntity(target);
            }
        }

        super.onUpdate();
    }
    
    public boolean isGlowing() {
    	return this.getExplodeState() > 0 && (this.ticksExisted & 7) < 4;
    }

//    /**
//     * Returns the sound this mob makes when it is hurt.
//     */
//    protected String getHurtSound()
//    {
//        return "mob.creeper.say";
//    }
//
//    /**
//     * Returns the sound this mob makes on death.
//     */
//    protected String getDeathSound()
//    {
//        return "mob.creeper.death";
//    }

//    /**
//     * Called when the mob's health reaches 0.
//     */
//    public void onDeath(DamageSource cause)
//    {
//        super.onDeath(cause);
//
////        if (cause.getEntity() instanceof EntitySkeleton)
////        {
//////            int i = ItemRegistry.getIdFromItem(Items.record_13);
//////            int j = ItemRegistry.getIdFromItem(Items.record_wait);
//////            int k = i + this.rand.nextInt(j - i + 1);
//////            this.dropItem(ItemRegistry.getItemById(k), 1);
////            this.dropItem(Items.dynamite, 1);
////        }
////        else
//        if (cause.getEntity() instanceof EntityCreeper && cause.getEntity() != this && ((EntityCreeper)cause.getEntity()).isPowered() && ((EntityCreeper)cause.getEntity()).isAIEnabled())
//        {
//            ((EntityCreeper)cause.getEntity()).func_175493_co();
//            this.entityDropItem(new ItemStack(Items.skull, 1, 4), 0.0F);
//        }
//    }

    public boolean attackEntityAsMob(Entity entityIn)
    {
        return true;
    }

//    /**
//     * Returns true if the creeper is powered by a lightning bolt.
//     */
//    public boolean isPowered()
//    {
//        return this.dataWatcher.getWatchableObjectByte(24) != 0;
//    }
    
//    public float getFlashIntensity(float p_70831_1_)
//    {
//        return ((float)this.lastActiveTime + (float)(this.timeSinceIgnited - this.lastActiveTime) * p_70831_1_) / (float)(this.fuseTime - 2);
//    }

//    protected Item getDropItem()
//    {
//        return Items.gunpowder;
//    }
    
    public int getExplodeState()
    {
        return this.dataWatcher.getWatchableObjectByte(23);
    }
    
    public void setExplodeState(int state)
    {
        this.dataWatcher.updateObject(23, Byte.valueOf((byte)state));
    }
    
    public boolean isCharged()
    {
        return this.getFlag(6);
    }

    public void setCharged(boolean power)
    {
        this.setFlag(6, power);
    }
    
//    public int getPower() {
//    	return (int)(this.dataWatcher.getWatchableObjectByte(24) & 255);
//    }
    
    public void onStruckByLightning(EntityLightning lightningBolt)
    {
//        super.onStruckByLightning(lightningBolt);
    	if(!this.worldObj.client && Vars.chargeHaunter) {
//	    	int power = this.getPower();
//	    	if(++power < 256)
//	    		this.dataWatcher.updateObject(24, (byte)power);
    		this.setCharged(true);
    	}
    }
    
    public boolean interact(EntityNPC player)
    {
        ItemStack itemstack = player.inventory.getCurrentItem();

        if (itemstack != null && itemstack.getItem() == Items.flint_and_steel)
        {
            this.worldObj.playSound(SoundEvent.IGNITE, this.posX + 0.5D, this.posY + 0.5D, this.posZ + 0.5D, 1.0F);
            player.swingItem();

            if (!this.worldObj.client)
            {
                this.ignite();
                itemstack.damageItem(1, player);
                return true;
            }
        }

        return super.interact(player);
    }
    
    private void explode()
    {
        if (!this.worldObj.client)
        {
            boolean flag = Vars.mobGrief;
            float f = 1.0F + (float)(this.isCharged() ? this.rand.range(3, 5) : 0) / 8.0f;
            this.worldObj.createAltExplosion(this, this.posX, this.posY, this.posZ, (float)this.rand.range(3, 5) * f, flag);
            this.setDead();
        }
    }

    public boolean hasIgnited()
    {
        return this.dataWatcher.getWatchableObjectByte(24) != 0;
    }

    public void ignite()
    {
        this.dataWatcher.updateObject(24, Byte.valueOf((byte)1));
    }

//    /**
//     * Returns true if the newer Entity AI code should be run
//     */
//    public boolean isAIEnabled()
//    {
//        return this.field_175494_bm < 1 && Config.dropLoot;
//    }

//    public void func_175493_co()
//    {
//        ++this.field_175494_bm;
//    }
    
	public int getColor() {
		return 0x00ff00;
	}
    
    public float getSpeciesBaseSize() {
    	return 1.55f;
    }
	
//	public boolean isAggressive() {
//		return true;
//	}
//	
//    public boolean isPeaceful() {
//    	return false;
//    }
    
    public boolean isAggressive(Class<? extends EntityNPC> clazz) {
    	return clazz == EntityHuman.class;
    }
    
    public boolean isPeaceful(Class<? extends EntityNPC> clazz) {
    	return clazz == EntityUndead.class;
    }
    
//    public boolean canInfight(Alignment align) {
//    	return false;
//    }
//    
//    public boolean canMurder(Alignment align) {
//    	return false;
//    }
    
//    public boolean canUseMagic() {
//    	return false;
//    }
    
//    public int getBaseEnergy(Random rand) {
//    	return rand.range(1, 2);
//    }
    
    public int getBaseHealth(Random rand) {
    	return rand.range(8, 14);
    }
    
//    public float getHeightDeviation(Random rand) {
//    	return 0.0f;
//    }
    
    public Alignment getNaturalAlign() {
    	return rand.pick(Alignment.EVIL, Alignment.CHAOTIC_EVIL);
    }
    
    public boolean isGhost() {
    	return true;
    }

	public boolean getCanSpawnHere() {
		return !((AWorldServer)this.worldObj).isDaytime();
	}
    
//    public boolean canAmbush(EntityLiving entity) {
//    	return true;
//    }
    
    public float getArmRotation() {
    	return 0.1f;
    }
    
    public float getLegRotation() {
    	return 0.2f;
    }
}
