package common.entity.npc;

import common.ai.EntityAITakePlace;
import common.ai.EntityAITempt;
import common.ai.EntityAIWatchClosest2;
import common.entity.effect.EntityLightning;
import common.init.Items;
import common.item.ItemStack;
import common.rng.Random;
import common.util.BlockPos;
import common.world.World;

public class EntityCultivator extends EntityNPC {
	public EntityCultivator(World worldIn) {
		super(worldIn);
		this.tasks.addTask(2, new EntityAITempt(this, 1.0D, null, 10.0, 32.0, false) {
			public boolean shouldExecute() {
				return EntityCultivator.this.canTakePhotos() && super.shouldExecute();
		    }
		});
        this.tasks.addTask(5, new EntityAIWatchClosest2(this, null, 32.0F, 1.0F) {
        	public boolean shouldExecute() {
				return EntityCultivator.this.canTakePhotos() && super.shouldExecute();
		    }
        	
        	public boolean continueExecuting() {
				return EntityCultivator.this.canTakePhotos() && super.continueExecuting();
		    }
        	
        	public void updateTask() {
        		super.updateTask();
        		if(EntityCultivator.this.rand.chance(100) && EntityCultivator.this.getHeldItem() != null 
        				&& EntityCultivator.this.getHeldItem().getItem() == Items.camera) {
        			EntityCultivator.this.swingItem();
        			EntityCultivator.this.worldObj.playAuxSFX(1024, new BlockPos(EntityCultivator.this.posX, 
        					EntityCultivator.this.posY + EntityCultivator.this.getEyeHeight(), EntityCultivator.this.posZ), 0);
        		}
            }
        	
            public void startExecuting() {
                super.startExecuting();
                EntityCultivator.this.setItemNoUpdate(0, new ItemStack(Items.camera));
            }
            
            public void resetTask() {
                super.resetTask();
                EntityCultivator.this.setItemNoUpdate(0, null);
            }
        });
		this.tasks.addTask(10, new EntityAITakePlace(this));
	}
	
	private boolean canTakePhotos() {
		return "Grell".equalsIgnoreCase(this.getCustomNameTag()) || "Grelle".equalsIgnoreCase(this.getCustomNameTag());
	}

	public boolean canPlay() {
		return super.canPlay() && !this.canTakePhotos();
	}

    public boolean isImmuneToFire() {
        return true;
    }
	
//	public boolean isAggressive() {
//		return this.alignment.evil && !this.alignment.lawful;
//	}
//	
//    public boolean isPeaceful() {
//    	return this.alignment.good;
//    }
    
    public boolean isAggressive(Class<? extends EntityNPC> clazz) {
    	return clazz == EntityElf.class || clazz == EntityBloodElf.class || (this.alignment.evil && (this.alignment.chaotic || clazz == EntityWoodElf.class));
    }
    
    public boolean isPeaceful(Class<? extends EntityNPC> clazz) {
    	return this.alignment.good && !this.alignment.chaotic;
    }
    
//    public boolean canInfight(Alignment align) {
//    	return true;
//    }
//    
//    public boolean canMurder(Alignment align) {
//    	return this.alignment == Alignment.CHAOTIC_EVIL;
//    }
    
    public boolean canUseMagic() {
    	return true;
    }
    
//    public int getBaseEnergy(Random rand) {
//    	return rand.range(12, 26);
//    }
    
    public int getBaseHealth(Random rand) {
    	return rand.range(25, 40);
    }
    
    public Alignment getNaturalAlign() {
    	return rand.pick(Alignment.values());
    }

    public float getHeightDeviationMin() {
    	return -0.2f;
    }

    public float getHeightDeviationMax() {
    	return 0.2f;
    }
    
    public float getSpeciesBaseSize() {
    	return 1.85f;
    }
    
	public void onStruckByLightning(EntityLightning lightningBolt) {
	}
    
	public int getColor() {
		return 0x6f0099;
	}
}
