package common.entity.npc;

import common.ai.EntityAIHurtByTarget;
import common.attributes.AttributeInstance;
import common.attributes.Attributes;
import common.entity.DamageSource;
import common.entity.Entity;
import common.entity.types.EntityLiving;
import common.nbt.NBTTagCompound;
import common.world.World;

public abstract class EntityMobNPC extends EntityNPC
{
    private int angerLevel;
//    private int randomSoundDelay;
    private int angerTarget;
//    private Class<? extends EntityNPC> angerClass;

    public EntityMobNPC(World worldIn)
    {
        super(worldIn);
        this.targets.addTask(1, new AIHurtByAggressor(this));
//        this.targets.addTask(2, new AITargetAggressor(this));
    }

//    public boolean isImmuneToFire()
//    {
//        return true;
//    }

    public void setAttackedBy(EntityLiving livingBase)
    {
        super.setAttackedBy(livingBase);

        if (livingBase != null)
        {
            this.angerTarget = livingBase.getId();
        }
    }

//    protected void applyEntityAI()
//    {
//        this.targets.addTask(1, new EntityMobNPC.AIHurtByAggressor(this));
//        this.targets.addTask(2, new EntityMobNPC.AITargetAggressor(this));
//    }

//    protected void applyEntityAttributes()
//    {
//        super.applyEntityAttributes();
//        this.getEntityAttribute(Attributes.REINFORCEMENT_CHANCE).setBaseValue(0.0D);
//        this.getEntityAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(0.23000000417232513D);
//        this.getEntityAttribute(Attributes.ATTACK_DAMAGE).setBaseValue(5.0D);
//    }

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

    protected void updateAITasks()
    {
        AttributeInstance iattributeinstance = this.getEntityAttribute(Attributes.MOVEMENT_SPEED);

        if (this.isAngry())
        {
            if (/* !this.isChild() && */ !iattributeinstance.hasModifier(Attributes.RUSHING_SPEED_MOD))
            {
                iattributeinstance.applyModifier(Attributes.RUSHING_SPEED_MOD);
            }

            --this.angerLevel;
        }
        else if (iattributeinstance.hasModifier(Attributes.RUSHING_SPEED_MOD))
        {
            iattributeinstance.removeModifier(Attributes.RUSHING_SPEED_MOD);
        }

//        if (this.randomSoundDelay > 0 && --this.randomSoundDelay == 0)
//        {
//            this.playSound("mob.zombiepig.zpigangry", this.getSoundVolume() * 2.0F, ((this.rand.floatv() - this.rand.floatv()) * 0.2F + 1.0F) * 1.8F);
//        }

        if (this.angerLevel > 0 && this.angerTarget != 0 && this.getAttackedBy() == null)
        {
            Entity entity = this.worldObj.getEntityByID(this.angerTarget);
            if(entity instanceof EntityLiving)
            	this.setAttackedBy((EntityLiving)entity);
            if(entity != null && entity.isPlayer())
            	this.playerAttacker = (EntityNPC)entity;
            this.recentlyHit = this.getAttackedTime();
        }

        super.updateAITasks();
    }

//    /**
//     * Checks if the entity's current position is a valid location to spawn this entity.
//     */
//    public boolean getCanSpawnHere()
//    {
//        return this.worldObj.getDifficulty() != Difficulty.PEACEFUL;
//    }

    /**
     * (abstract) Protected helper method to write subclass entity data to NBT.
     */
    public void writeEntityToNBT(NBTTagCompound tagCompound)
    {
        super.writeEntityToNBT(tagCompound);
        tagCompound.setShort("Anger", (short)this.angerLevel);

//        if (this.angerTarget != null)
//        {
//            tagCompound.setString("HurtBy", this.angerTarget);
//        }
//        else
//        {
//            tagCompound.setString("HurtBy", "");
//        }
    }

    /**
     * (abstract) Protected helper method to read subclass entity data from NBT.
     */
    public void readEntityFromNBT(NBTTagCompound tagCompund)
    {
        super.readEntityFromNBT(tagCompund);
        this.angerLevel = tagCompund.getShort("Anger");
//        String s = tagCompund.getString("HurtBy");
//
//        if (s.length() > 0)
//        {
//            this.angerTarget = s;
//            EntityNPC entityplayer = this.worldObj.getPlayer(this.angerTarget);
//            this.setAttackedBy(entityplayer);
//
//            if (entityplayer != null)
//            {
//                this.playerAttacker = entityplayer;
//                this.recentlyHit = this.getAttackedTime();
//            }
//        }
    }

    /**
     * Called when the entity is attacked.
     */
    public boolean attackEntityFrom(DamageSource source, int amount)
    {
//        if (this.isEntityInvulnerable(source))
//        {
//            return false;
//        }
//        else
//        {
        Entity entity = source.getEntity();

        if (entity != null && entity instanceof EntityNPC)
        {
            this.becomeAngryAt(entity);
        }

        return super.attackEntityFrom(source, amount);
//        }
    }

    /**
     * Causes this PigZombie to become angry at the supplied Entity (which will be a player).
     */
    private void becomeAngryAt(Entity p_70835_1_)
    {
        this.angerLevel = this.rand.excl(400, 800);
//        this.randomSoundDelay = this.rand.zrange(40);

        if (p_70835_1_ instanceof EntityLiving)
        {
            this.setAttackedBy((EntityLiving)p_70835_1_);
        }
    }

    public boolean isAngry()
    {
        return this.angerLevel > 0;
    }

//    /**
//     * Returns the sound this mob makes while it's alive.
//     */
//    protected String getLivingSound()
//    {
//        return "mob.zombiepig.zpig";
//    }
//
//    /**
//     * Returns the sound this mob makes when it is hurt.
//     */
//    protected String getHurtSound()
//    {
//        return "mob.zombiepig.zpighurt";
//    }
//
//    /**
//     * Returns the sound this mob makes on death.
//     */
//    protected String getDeathSound()
//    {
//        return "mob.zombiepig.zpigdeath";
//    }

//    /**
//     * Drop 0-2 items of this living's type
//     *  
//     * @param wasRecentlyHit true if this this entity was recently hit by appropriate entity (generally only if player
//     * or tameable)
//     * @param lootingModifier level of enchanment to be applied to this drop
//     */
//    protected void dropFewItems(boolean wasRecentlyHit, int lootingModifier)
//    {
//        int i = this.rand.zrange(2 + lootingModifier);
//
//        for (int j = 0; j < i; ++j)
//        {
//            this.dropItem(Items.rotten_flesh, 1);
//        }
//
//        i = this.rand.zrange(2 + lootingModifier);
//
//        for (int k = 0; k < i; ++k)
//        {
//            this.dropItem(Items.gold_nugget, 1);
//        }
//    }
//
//    /**
//     * Called when a player interacts with a mob. e.g. gets milk from a cow, gets into the saddle on a pig.
//     */
//    public boolean interact(EntityNPC player)
//    {
//        return false;
//    }
//
//    /**
//     * Causes this Entity to drop a random item.
//     */
//    protected void addRandomDrop()
//    {
//        this.dropItem(Items.gold_ingot, 1);
//    }
//
//    /**
//     * Gives armor or weapon for entity based on given DifficultyInstance
//     */
//    protected void setEquipmentBasedOnDifficulty(DifficultyInstance difficulty)
//    {
//        this.setItem(0, new ItemStack(Items.golden_sword));
//    }
//
//    /**
//     * Called only once on an entity when first time spawned, via egg, mob spawner, natural spawning etc, but not called
//     * when entity is reloaded from nbt. Mainly used for initializing attributes and inventory
//     */
//    public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, IEntityLivingData livingdata)
//    {
//        super.onInitialSpawn(difficulty, livingdata);
////        this.setVillager(false);
//        return livingdata;
//    }
    
//	public int getColor() {
//		return 0xff9494;
//	}
    
//	public void onStruckByLightning(EntityLightning lightningBolt) {
//	}

    public boolean isAggressive(Class<? extends EntityNPC> clazz) {
    	return this.isAngry() && clazz != this.getClass() && !this.isPeaceful(clazz);
    }

    static class AIHurtByAggressor extends EntityAIHurtByTarget
    {
        public AIHurtByAggressor(EntityMobNPC p_i45828_1_)
        {
            super(p_i45828_1_, true);
        }

        protected void setEntityAttackTarget(EntityLiving creatureIn, EntityLiving entityLivingBaseIn)
        {
            super.setEntityAttackTarget(creatureIn, entityLivingBaseIn);

            if (creatureIn.getClass() == this.taskOwner.getClass())
            {
                ((EntityMobNPC)creatureIn).becomeAngryAt(entityLivingBaseIn);
            }
        }
    }

//    static class AITargetAggressor extends EntityAINearestAttackableTarget<EntityLiving>
//    {
//        public AITargetAggressor(EntityMobNPC p_i45829_1_)
//        {
//            super(p_i45829_1_, EntityLiving.class, 10, true, false, new Predicate<EntityLiving>() {
//				public boolean test(EntityLiving entity) {
//					return entity.isPlayer(); // || entity instanceof EntityNPC;
//				}
//			});
//        }
//
//        public boolean shouldExecute()
//        {
//            return ((EntityMobNPC)this.taskOwner).isAngry() && super.shouldExecute();
//        }
//    }
}
