package common.entity.animal;

import java.util.function.Predicate;

import common.ai.EntityAIAvoidEntity;
import common.ai.EntityAIFollowOwner;
import common.ai.EntityAILeapAtTarget;
import common.ai.EntityAIMate;
import common.ai.EntityAIOcelotAttack;
import common.ai.EntityAIOcelotSit;
import common.ai.EntityAISwimming;
import common.ai.EntityAITargetNonTamed;
import common.ai.EntityAITempt;
import common.ai.EntityAIWander;
import common.ai.EntityAIWatchClosest;
import common.attributes.Attributes;
import common.entity.DamageSource;
import common.entity.Entity;
import common.entity.npc.Alignment;
import common.entity.npc.EntityNPC;
import common.entity.types.EntityAnimal;
import common.entity.types.EntityLiving;
import common.entity.types.EntityTameable;
import common.init.Config;
import common.init.Items;
import common.init.SoundEvent;
import common.item.Item;
import common.item.ItemStack;
import common.nbt.NBTTagCompound;
import common.pathfinding.PathNavigateGround;
import common.world.World;

public class EntityOcelot extends EntityTameable
{
    private EntityAIAvoidEntity<EntityNPC> avoidEntity;

    /**
     * The tempt AI task for this mob, used to prevent taming while it is fleeing.
     */
    private EntityAITempt aiTempt;
    private boolean wasTempted;

    public EntityOcelot(World worldIn)
    {
        super(worldIn);
        this.setSize(0.6F, 0.7F);
        ((PathNavigateGround)this.getNavigator()).setAvoidsWater(true);
        this.tasks.addTask(1, new EntityAISwimming(this));
        this.tasks.addTask(2, this.aiSit);
        this.tasks.addTask(3, this.aiTempt = new EntityAITempt(this, 0.6D, Items.fish, true));
        this.tasks.addTask(5, new EntityAIFollowOwner(this, 1.0D, 10.0F, 5.0F));
        this.tasks.addTask(6, new EntityAIOcelotSit(this, 0.8D));
        this.tasks.addTask(7, new EntityAILeapAtTarget(this, 0.3F));
        this.tasks.addTask(8, new EntityAIOcelotAttack(this));
        this.tasks.addTask(9, new EntityAIMate(this, 0.8D));
        this.tasks.addTask(10, new EntityAIWander(this, 0.8D));
        this.tasks.addTask(11, new EntityAIWatchClosest(this, null, 10.0F));
        this.targets.addTask(1, new EntityAITargetNonTamed(this, EntityAnimal.class, false, new Predicate<EntityAnimal>() {
			public boolean test(EntityAnimal entity) {
				return entity instanceof EntityChicken || entity instanceof EntityMouse;
			}
		}));
    }

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

    public void updateAITasks()
    {
        if (this.getMoveHelper().isUpdating())
        {
            double d0 = this.getMoveHelper().getSpeed();

            if (d0 == 0.6D)
            {
                this.setSneaking(this.wasTempted);
                this.setSprinting(false);
                this.wasTempted = true;
            }
            else if (d0 == 1.33D)
            {
                this.setSneaking(false);
                this.setSprinting(true);
                this.wasTempted = false;
            }
            else
            {
                this.setSneaking(false);
                this.setSprinting(false);
                this.wasTempted = false;
            }
        }
        else
        {
            this.setSneaking(false);
            this.setSprinting(false);
            this.wasTempted = false;
        }
    }

//    /**
//     * Determines if an entity can be despawned, used on idle far away entities
//     */
//    protected boolean canDespawn()
//    {
//        return !this.isTamed() && this.ticksExisted > 2400;
//    }

    protected void applyEntityAttributes()
    {
        super.applyEntityAttributes();
        this.setMaxHealth(10);
        this.getEntityAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(0.30000001192092896D);
    }

    public void fall(float distance, float damageMultiplier)
    {
    }

    /**
     * (abstract) Protected helper method to write subclass entity data to NBT.
     */
    public void writeEntityToNBT(NBTTagCompound tagCompound)
    {
        super.writeEntityToNBT(tagCompound);
        tagCompound.setInteger("CatType", this.getTameSkin());
    }

    /**
     * (abstract) Protected helper method to read subclass entity data from NBT.
     */
    public void readEntityFromNBT(NBTTagCompound tagCompund)
    {
        super.readEntityFromNBT(tagCompund);
        this.setTameSkin(tagCompund.getInteger("CatType"));
    }

    /**
     * Returns the sound this mob makes while it's alive.
     */
    protected SoundEvent getLivingSound()
    {
        return this.isTamed() ? (this.isInLove() ? SoundEvent.CAT_PURREOW : (this.rand.chance(4) ? SoundEvent.CAT_PURREOW : SoundEvent.CAT_MEOW)) : null;
    }

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

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

    /**
     * Returns the volume for the sounds this mob makes.
     */
    protected float getSoundVolume()
    {
        return 0.4F;
    }

    protected Item getDropItem()
    {
        return Items.leather;
    }

    public boolean attackEntityAsMob(Entity entityIn)
    {
    	if(!this.worldObj.client && !Config.damageMobs)
    		return false;
        return entityIn.attackEntityFrom(DamageSource.causeMobDamage(this), 3);
    }

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

    /**
     * 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)
    {
    }

    /**
     * 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)
    {
        ItemStack itemstack = player.inventory.getCurrentItem();

        if (this.isTamed())
        {
            if (this.isOwner(player) && !this.worldObj.client && !this.isBreedingItem(itemstack))
            {
                this.aiSit.setSitting(!this.isSitting());
            }
        }
        else if (this.aiTempt.isRunning() && itemstack != null && itemstack.getItem() == Items.fish && player.getDistanceSqToEntity(this) < 9.0D)
        {
//            if (!player.creative)
//            {
            --itemstack.stackSize;
//            }

            if (itemstack.stackSize <= 0)
            {
                player.inventory.setInventorySlotContents(player.inventory.currentItem, (ItemStack)null);
            }

            if (!this.worldObj.client)
            {
                if (this.rand.chance(3))
                {
                    this.setTamed(true);
                    this.setTameSkin(this.worldObj.rand.roll(3));
//                    this.setOwnerId(player.getUser());
                    this.playTameEffect(true);
                    this.aiSit.setSitting(true);
                    this.worldObj.setEntityState(this, (byte)7);
                }
                else
                {
                    this.playTameEffect(false);
                    this.worldObj.setEntityState(this, (byte)6);
                }
            }

            return true;
        }

        return super.interact(player);
    }

    public EntityOcelot createChild(EntityLiving ageable)
    {
        EntityOcelot entityocelot = new EntityOcelot(this.worldObj);

        if (this.isTamed())
        {
//            entityocelot.setOwnerId(this.getOwnerId());
            entityocelot.setTamed(true);
            entityocelot.setTameSkin(this.getTameSkin());
        }

        return entityocelot;
    }

    /**
     * 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.fish;
    }

    /**
     * Returns true if the mob is currently able to mate with the specified mob.
     */
    public boolean canMateWith(EntityAnimal otherAnimal)
    {
        if (otherAnimal == this)
        {
            return false;
        }
//        else if (!this.isTamed())
//        {
//            return false;
//        }
        else if (!(otherAnimal instanceof EntityOcelot))
        {
            return false;
        }
        else
        {
            EntityOcelot entityocelot = (EntityOcelot)otherAnimal;
            return (entityocelot.isTamed() != this.isTamed()) ? false : this.isInLove() && entityocelot.isInLove();
        }
    }

    public int getTameSkin()
    {
        return this.dataWatcher.getWatchableObjectByte(18);
    }

    public void setTameSkin(int skinId)
    {
        this.dataWatcher.updateObject(18, Byte.valueOf((byte)skinId));
    }

    /**
     * Checks if the entity's current position is a valid location to spawn this entity.
     */
    public boolean getCanSpawnHere()
    {
        return this.worldObj.rand.rarity(3);
    }

//    /**
//     * Checks that the entity is not colliding with any blocks / liquids
//     */
//    public boolean isNotColliding()
//    {
//        if (this.worldObj.checkNoEntityCollision(this.getEntityBoundingBox(), this) && this.worldObj.getCollidingBoundingBoxes(this, this.getEntityBoundingBox()).isEmpty() && !this.worldObj.isAnyLiquid(this.getEntityBoundingBox()))
//        {
//            BlockPos blockpos = new BlockPos(this.posX, this.getEntityBoundingBox().minY, this.posZ);
//
//            if (blockpos.getY() < this.worldObj.getSeaLevel())
//            {
//                return false;
//            }
//
//            Block block = this.worldObj.getBlockState(blockpos.down()).getBlock();
//
//            if (block == Blocks.grass || block.getMaterial() == Material.leaves)
//            {
//                return true;
//            }
//        }
//
//        return false;
//    }

    /**
     * Get the name of this object. For players this returns their username
     */
    public String getTypeName()
    {
        return this.hasCustomName() ? this.getCustomNameTag() : (this.isTamed() ? "Katze" : super.getTypeName());
    }

    public void setTamed(boolean tamed)
    {
        super.setTamed(tamed);
    }

    protected void setupTamedAI()
    {
        if (this.avoidEntity == null)
        {
            this.avoidEntity = new EntityAIAvoidEntity(this, EntityNPC.class, 
//            		new Predicate<EntityNPC>() {
//				public boolean test(EntityNPC entity) {
//					return !entity.capabilities.disableTarget;
//				}
//			} ,
            		16.0F, 0.8D, 1.33D);
        }

        this.tasks.removeTask(this.avoidEntity);

        if (!this.isTamed())
        {
            this.tasks.addTask(4, this.avoidEntity);
        }
    }

    /**
     * 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 Object onInitialSpawn(Object livingdata)
    {
        livingdata = super.onInitialSpawn(livingdata);

        if (this.worldObj.rand.chance(7))
        {
            for (int i = 0; i < 2; ++i)
            {
                EntityOcelot entityocelot = new EntityOcelot(this.worldObj);
                entityocelot.setLocationAndAngles(this.posX, this.posY, this.posZ, this.rotYaw, 0.0F);
                entityocelot.setGrowingAge(-24000);
                this.worldObj.spawnEntityInWorld(entityocelot);
            }
        }

        return livingdata;
    }
    
    public int getLeashColor() {
    	return this.isTamed() ? 0xf02020 : super.getLeashColor();
    }
    
	public int getColor() {
		return this.isTamed() ? 0xdeb059 : 0xffe15b;
	}
	
	public Alignment getAlignment() {
		return this.isTamed() ? Alignment.CHAOTIC_GOOD : Alignment.CHAOTIC;
	}
}
