package common.entity.npc;

import common.ai.EntityAIAttackOnCollide;
import common.ai.EntityAILeapAtTarget;
import common.entity.Entity;
import common.entity.types.EntityLiving;
import common.pathfinding.PathNavigate;
import common.pathfinding.PathNavigateClimber;
import common.potion.Potion;
import common.potion.PotionEffect;
import common.rng.Random;
import common.util.BlockPos;
import common.world.World;

public class EntityArachnoid extends EntityNPC
{
    public EntityArachnoid(World worldIn)
    {
        super(worldIn);
//        this.setSize(1.4F, 1.6F);
        this.tasks.addTask(3, new EntityAILeapAtTarget(this, 0.4F));
//        this.tasks.addTask(4, new AISpiderAttack(this, EntityNPC.class));
        this.tasks.addTask(4, new AISpiderAttack(this, EntityNPC.class));
//        this.targets.addTask(2, new EntitySpider.AISpiderTarget(this, EntityNPC.class));
//        this.targets.addTask(3, new EntitySpider.AISpiderTarget(this, EntityNPC.class));
    }
    
//    public double getMountedYOffset()
//    {
//        return (double)(this.height * 0.5F);
//    }
    
//    public float getRenderScale()
//    {
//        return 0.9375f * this.height / 1.6f;
//    }
    
    protected PathNavigate getNewNavigator(World worldIn)
    {
        return new PathNavigateClimber(this, worldIn);
    }

    protected void entityInit()
    {
        super.entityInit();
        this.dataWatcher.addObject(23, (byte)0);
    }
    
    public void onUpdate()
    {
        super.onUpdate();

        if (!this.worldObj.client)
        {
            this.setBesideClimbableBlock(this.collidedHorizontally);
        }
    }

//    protected void applyEntityAttributes()
//    {
//        super.applyEntityAttributes();
//        this.getEntityAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(0.30000001192092896D);
//    }
    
//    protected String getLivingSound()
//    {
//        return "mob.spider.say";
//    }
    
    public boolean isOnLadder()
    {
        return super.isOnLadder() || (this.client != null ? this.collidedHorizontally && !this.noclip : this.isBesideClimbableBlock());
    }
    
    public void setInWeb()
    {
    }
    
    public boolean isBesideClimbableBlock()
    {
        return (!this.isPlayer() || !this.noclip) && this.dataWatcher.getWatchableObjectByte(23) != 0;
    }
    
    public void setBesideClimbableBlock(boolean climb)
    {
        this.dataWatcher.updateObject(23, (byte)(climb ? 1 : 0));
    }
    
    public Object onInitialSpawn(Object livingdata)
    {
        livingdata = super.onInitialSpawn(livingdata);
//    	if(!this.isChild()) {
//	        if (this.worldObj.rand.chance(100))
//	        {
//	            EntityUndead entityskeleton = new EntityUndead(this.worldObj);
//	            entityskeleton.setLocationAndAngles(this.posX, this.posY, this.posZ, this.rotYaw, 0.0F);
//	            entityskeleton.onInitialSpawn(difficulty, (IEntityLivingData)null);
//	            this.worldObj.spawnEntityInWorld(entityskeleton);
//	            entityskeleton.mountEntity(this);
//	        }
	
        if (livingdata == null && !this.isPlayer())
        {
            livingdata = new EntityArachnoid.GroupData(this.worldObj.rand.chance(40));

            if (/* this.worldObj.getDifficulty() == Difficulty.HARD && */ this.worldObj.rand.chance())
            {
                ((EntityArachnoid.GroupData)livingdata).pickEffect(this.worldObj.rand);
            }
        }

        if (livingdata instanceof EntityArachnoid.GroupData)
        {
            Potion i = ((EntityArachnoid.GroupData)livingdata).potionEffectId;

            if (i != null)
            {
                this.addEffect(new PotionEffect(i, Integer.MAX_VALUE, 0));
            }
            
            if(((EntityArachnoid.GroupData)livingdata).isChild)
            	this.setGrowingAge(-24000);
        }
//    	}
        return livingdata;
    }

//    public float getEyeHeight()
//    {
//        return super.getEyeHeight() * 0.85f;
//    }
	
    public boolean isSneakingVisually() {
        return false;
    }
    
	public int getColor() {
		return 0xc63ba0;
	}

    static class AISpiderAttack extends EntityAIAttackOnCollide
    {
        public AISpiderAttack(EntityArachnoid spider, Class <? extends Entity > targetClass)
        {
            super(spider, targetClass, 1.0D, true);
        }

        public boolean continueExecuting()
        {
            float f = this.attacker.getBrightness(1.0F);

            if (f >= 0.5F && this.attacker.getRNG().chance(100))
            {
                this.attacker.setAttackTarget((EntityLiving)null);
                return false;
            }
            else
            {
                return super.continueExecuting();
            }
        }

        protected double getAttackRange(EntityLiving attackTarget)
        {
            return (double)(4.0F + attackTarget.width);
        }
    }

//    static class AISpiderTarget<T extends EntityLivingBase> extends EntityAINearestAttackableTarget
//    {
//        public AISpiderTarget(EntitySpider spider, Class<T> classTarget)
//        {
//            super(spider, classTarget, true);
//        }
//
//        public boolean shouldExecute()
//        {
//            float f = this.taskOwner.getBrightness(1.0F);
//            return f >= 0.5F ? false : super.shouldExecute();
//        }
//    }

//	public boolean isAggressive() {
//		return this.getBrightness(1.0f) < 0.5f;
//	}
//
//	public boolean isPeaceful() {
//		return false;
//	}

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

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

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

	public int getBaseHealth(Random rand) {
		return rand.range(16, 24);
	}

//	public int getBaseEnergy(Random rand) {
//		return 0;
//	}
    
//    public float getHeightDeviation(Random rand) {
//    	return rand.frange(0.0f, 0.25f);
//    }

    public float getHeightDeviationMax() {
    	return 0.25f;
    }

	public Alignment getNaturalAlign() {
		return rand.pick(Alignment.LAWFUL_EVIL, Alignment.EVIL, Alignment.LAWFUL, Alignment.NEUTRAL);
	}

    public boolean attackEntityAsMob(Entity entityIn)
    {
        if(super.attackEntityAsMob(entityIn)) {
            if(entityIn instanceof EntityLiving && this.rand.chance(50))
                ((EntityLiving)entityIn).addEffect(new PotionEffect(Potion.POISON, this.rand.range(14, 35) * 20, 0));
            return true;
        }
        return false;
    }

	public boolean getCanSpawnHere() {
		return this.worldObj.getLightFromNeighbors(new BlockPos(this)) <= this.rand.zrange(8);
	}
    
    public boolean canAmbush(EntityLiving entity) {
    	return (float)this.getHealth() >= ((float)this.getMaxHealth() / 4.0f);
    }
    
    public boolean canUseMagic() {
    	return true;
    }
    
    public float getSpeciesBaseSize() {
    	return 1.6f;
    }

    public static class GroupData
    {
        public Potion potionEffectId;
        public boolean isChild;
        
        public GroupData(boolean isChild) {
        	this.isChild = isChild;
        }

        public void pickEffect(Random rand)
        {
        	this.potionEffectId = rand.pick(Potion.SPEED, Potion.SPEED, Potion.STRENGTH, Potion.REGENERATION);
        }
    }
}
