package common.entity.animal;

import common.ai.EntityAIControlledByPlayer;
import common.ai.EntityAIFollowParent;
import common.ai.EntityAILookIdle;
import common.ai.EntityAIMate;
import common.ai.EntityAIPanic;
import common.ai.EntityAISwimming;
import common.ai.EntityAITempt;
import common.ai.EntityAIWander;
import common.ai.EntityAIWatchClosest;
import common.entity.npc.EntityNPC;
import common.entity.types.EntityAnimal;
import common.entity.types.EntityLiving;
import common.init.Items;
import common.init.SoundEvent;
import common.item.Item;
import common.item.ItemStack;
import common.pathfinding.PathNavigateGround;
import common.tags.TagObject;
import common.world.World;

public class EntityPig extends EntityAnimal
{
    /** AI task for player control. */
    private final EntityAIControlledByPlayer aiControlledByPlayer;

    public EntityPig(World worldIn)
    {
        super(worldIn);
        this.setSize(0.9F, 0.9F);
        ((PathNavigateGround)this.getNavigator()).setAvoidsWater(true);
        this.tasks.addTask(0, new EntityAISwimming(this));
        this.tasks.addTask(1, new EntityAIPanic(this, 1.25D));
        this.tasks.addTask(2, this.aiControlledByPlayer = new EntityAIControlledByPlayer(this, 0.3F));
        this.tasks.addTask(3, new EntityAIMate(this, 1.0D));
        this.tasks.addTask(4, new EntityAITempt(this, 1.2D, Items.carrot_on_a_stick, false));
        this.tasks.addTask(4, new EntityAITempt(this, 1.2D, Items.carrot, false));
        this.tasks.addTask(5, new EntityAIFollowParent(this, 1.1D));
        this.tasks.addTask(6, new EntityAIWander(this, 1.0D));
        this.tasks.addTask(7, new EntityAIWatchClosest(this, null, 6.0F));
        this.tasks.addTask(8, new EntityAILookIdle(this));
    }

    protected void applyEntityAttributes()
    {
        super.applyEntityAttributes();
        this.setMaxHealth(10);
        this.setSpeedBase(0.25f);
    }

    /**
     * returns true if all the conditions for steering the entity are met. For pigs, this is true if it is being ridden
     * by a player and the player is holding a carrot-on-a-stick
     */
    public boolean canBeSteered()
    {
        ItemStack itemstack = ((EntityNPC)this.passenger).getHeldItem();
        return itemstack != null && itemstack.getItem() == Items.carrot_on_a_stick;
    }

    protected void entityInit()
    {
        super.entityInit();
        this.dataWatcher.addObject(16, Byte.valueOf((byte)0));
    }

    /**
     * (abstract) Protected helper method to write subclass entity data to NBT.
     */
    public void writeEntity(TagObject tagCompound)
    {
        super.writeEntity(tagCompound);
        tagCompound.setBool("Saddle", this.getSaddled());
    }

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

    /**
     * Returns the sound this mob makes while it's alive.
     */
    protected SoundEvent getLivingSound()
    {
        return SoundEvent.PIG_IDLE;
    }

    /**
     * Returns the sound this mob makes when it is hurt.
     */
    protected SoundEvent getHurtSound()
    {
        return SoundEvent.PIG_IDLE;
    }

    /**
     * Returns the sound this mob makes on death.
     */
    protected SoundEvent getDeathSound()
    {
        return SoundEvent.PIG_DEATH;
    }

//    protected void playStepSound(BlockPos pos, Block blockIn)
//    {
//        this.playSound("mob.pig.step", 0.15F, 1.0F);
//    }

    /**
     * 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)
    {
        if (super.interact(player))
        {
            return true;
        }
        else if (!this.getSaddled() || this.worldObj.client || this.passenger != null && this.passenger != player)
        {
            return false;
        }
        else
        {
            player.mountEntity(this);
            return true;
        }
    }

    protected Item getDropItem()
    {
        return this.isBurning() ? Items.cooked_porkchop : Items.porkchop;
    }

    /**
     * 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.roll(3) + this.rand.zrange(1 + lootingModifier);

        for (int j = 0; j < i; ++j)
        {
            if (this.isBurning())
            {
                this.dropItem(Items.cooked_porkchop, 1);
            }
            else
            {
                this.dropItem(Items.porkchop, 1);
            }
        }

        if (this.getSaddled())
        {
            this.dropItem(Items.saddle, 1);
        }
    }

    /**
     * Returns true if the pig is saddled.
     */
    public boolean getSaddled()
    {
        return (this.dataWatcher.getWatchableObjectByte(16) & 1) != 0;
    }

    /**
     * Set or remove the saddle of the pig.
     */
    public void setSaddled(boolean saddled)
    {
        if (saddled)
        {
            this.dataWatcher.updateObject(16, Byte.valueOf((byte)1));
        }
        else
        {
            this.dataWatcher.updateObject(16, Byte.valueOf((byte)0));
        }
    }

//    /**
//     * Called when a lightning bolt hits the entity.
//     */
//    public void onStruckByLightning(EntityLightning lightningBolt)
//    {
//        if (!this.worldObj.client && !this.dead && Config.convertPigman)
//        {
//            EntityMobNPC entitypigzombie = new EntityMobNPC(this.worldObj);
//            entitypigzombie.setItem(0, new ItemStack(Items.golden_sword));
//            entitypigzombie.setLocationAndAngles(this.posX, this.posY, this.posZ, this.rotYaw, this.rotPitch);
//            entitypigzombie.setNoAI(this.isAIDisabled());
//
//            if (this.hasCustomName())
//            {
//                entitypigzombie.setCustomNameTag(this.getCustomNameTag());
//                entitypigzombie.setAlwaysRenderNameTag(this.getAlwaysRenderNameTag());
//            }
//
//            this.worldObj.spawnEntityInWorld(entitypigzombie);
//            this.setDead();
//        }
//    }

//    public void fall(float distance, float damageMultiplier)
//    {
//        super.fall(distance, damageMultiplier);
//
//        if (distance > 5.0F && this.passenger.isPlayer())
//        {
//            ((EntityNPC)this.passenger).triggerAchievement(AchievementList.flyPig);
//        }
//    }

    public EntityPig createChild(EntityLiving ageable)
    {
        return new EntityPig(this.worldObj);
    }

    /**
     * Checks if the parameter is an item which this animal can be fed to breed it (wheat, carrots or seeds depending on
     * the animal type)
     */
    public boolean isBreedingItem(ItemStack stack)
    {
        return stack != null && stack.getItem() == Items.carrot;
    }

    /**
     * Return the AI task for player control.
     */
    public EntityAIControlledByPlayer getAIControlledByPlayer()
    {
        return this.aiControlledByPlayer;
    }
    
	public int getColor() {
		return 0xe78e8e;
	}
}
