package common.entity.npc;

import common.entity.types.EntityLiving;
import common.init.Items;
import common.item.ItemStack;
import common.rng.Random;
import common.util.BlockPos;
import common.util.Identifyable;
import common.village.Village;
import common.world.World;
import common.world.AWorldServer;

public class EntityHuman extends EntityNPC {
	public static enum ClassType implements Identifyable {
		NONE("none", ""),
		KNIGHT("knight", "Krieger"),
		PEASANT("peasant", "Bauer"),
		MAGE("mage", "Magier"),
		SORCERER("sorcerer", "Hexer"),
		HEALER("healer", "Heiler"),
		BARD("bard", "Barde"),
		LEADER("leader", "Anführer"),
		CAPTAIN("captain", "Kapitän"),
		HUNTER("hunter", "Jäger");

		private final String name;
		private final String display;
		
		private ClassType(String name, String display) {
			this.name = name;
			this.display = display;
		}
		
		public String getName() {
			return this.name;
		}
		
		public String toString() {
			return this.display;
		}
	}
	
	private static final Alignment[] ALIGNMENTS = new Alignment[] {
			Alignment.LAWFUL_GOOD, Alignment.GOOD, Alignment.CHAOTIC_GOOD, Alignment.LAWFUL, Alignment.CHAOTIC, Alignment.LAWFUL_EVIL, 
			Alignment.EVIL, Alignment.CHAOTIC_EVIL
	};
	
	private Village village;
	private int checkTimer;
	
	public EntityHuman(World worldIn) {
		super(worldIn);
	}
	
//	public boolean isAggressive() {
//		return this.alignment.evil && !this.alignment.lawful;
//	}
//	
//    public boolean isPeaceful() {
//    	return this.alignment.good && !this.alignment.chaotic;
//    }
    
    public boolean isAggressive(Class<? extends EntityNPC> clazz) {
    	return this.alignment.evil && !this.alignment.lawful;
    }
    
    public boolean isPeaceful(Class<? extends EntityNPC> clazz) {
    	return this.alignment == Alignment.LAWFUL_GOOD;
    }
    
//    public boolean canInfight(Alignment align) {
//    	return this.alignment != Alignment.LAWFUL_GOOD;
//    }
//    
//    public boolean canMurder(Alignment align) {
//    	return this.alignment == Alignment.CHAOTIC_EVIL;
//    }
    
//    public boolean canUseMagic() {
//    	return false;
//    }
    
//    public int getBaseEnergy(Random rand) {
//    	return rand.range(0, 1);
//    }
    
    public int getBaseHealth(Random rand) {
    	return rand.range(18, 20);
    }

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

    public float getHeightDeviationMax() {
    	return 0.2f;
    }
    
    public float getSpeciesBaseSize() {
    	return 1.8f;
    }
    
    public Alignment getNaturalAlign() {
    	return rand.pick(ALIGNMENTS);
    }
    
    public boolean isBreedingItem(ItemStack stack) {
        return stack.getItem() == Items.golden_apple;
    }

	protected void updateAITasks() {
		if(--this.checkTimer <= 0) {
			BlockPos blockpos = new BlockPos(this);
//			if(((IWorldServer)this.worldObj).isPrimary())
			((AWorldServer)this.worldObj).addToVillagerPositionList(blockpos);
			this.checkTimer = this.rand.excl(70, 120);
			this.village = ((AWorldServer)this.worldObj).getNearestVillage(blockpos, 32);

			if(this.village == null) {
				this.detachHome();
			}
			else {
				BlockPos blockpos1 = this.village.getCenter();
				this.setHomePosAndDistance(blockpos1, (int)((float)this.village.getRadius() * 1.0F));
			}
		}
		super.updateAITasks();
	}
    
    public boolean canTrade() {
        return true;
    }
    
    public boolean canAmbush(EntityLiving entity) {
    	return (float)this.getHealth() >= ((float)this.getMaxHealth() / 2.5f);
    }
}
