package hmggirlfront.entity.npc;

import java.util.List;

import gvclib.entity.living.EntityGVCLivingBase;
import gvclib.entity.living.ISoldier;
import hmggirlfront.mod_GirlFront;
import hmggirlfront.entity.EntityDoll;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.EntityAIFleeSun;
import net.minecraft.entity.ai.EntityAIRestrictSun;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.entity.monster.IMob;
import net.minecraft.entity.passive.IAnimals;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.MobEffects;
import net.minecraft.init.SoundEvents;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.network.datasync.DataSerializers;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumHand;
import net.minecraft.world.DifficultyInstance;
import net.minecraft.world.EnumDifficulty;
import net.minecraft.world.World;

public class EntityNPC_Doll extends EntityGVCLivingBase implements IAnimals, ISoldier {

	public int evasion = 0;
	public int evasion_base = 0;
	public int power = 0;
	public int power_base = 0;
	public String bullet_type_am = "";
	public float bure = 0;
	public float bure_base = 0;
	public int rpm = 0;
	public int rpm_base = 0;
	public float doll_size = 1.0F;
	
	public Item bullet_type = null;
	
	public int doll_skill = 0;
	public boolean doll_skill_can = true;
	public int doll_autoskill = 0;
	
	
	 private static final DataParameter<Integer> squadid = 
	    		EntityDataManager.<Integer>createKey(EntityNPC_Doll.class, DataSerializers.VARINT);
	 
	 private static final DataParameter<Integer> missionid = 
	    		EntityDataManager.<Integer>createKey(EntityNPC_Doll.class, DataSerializers.VARINT);
	
	public EntityNPC_Doll(World worldIn) {
		super(worldIn);
		this.setSize(0.5F, 1.8F);
		this.biped = false;
		
		this.squad_followrange = mod_GirlFront.cfg_doll_followrange;
	}

	protected void initEntityAI() {
		this.tasks.addTask(1, new EntityAISwimming(this));
		this.tasks.addTask(2, new EntityAIRestrictSun(this));
		this.tasks.addTask(3, new EntityAIFleeSun(this, 1.0D));

	}

	protected void applyEntityAttributes() {
		super.applyEntityAttributes();
		this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(20D);
		// this.getEntityAttribute(SharedMonsterAttributes.KNOCKBACK_RESISTANCE).setBaseValue(20D);
		this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.25D);
		this.getEntityAttribute(SharedMonsterAttributes.FOLLOW_RANGE).setBaseValue(50.0D);
	}

	public void writeEntityToNBT(NBTTagCompound compound)
    {
        super.writeEntityToNBT(compound);
        NBTTagList nbttaglist = new NBTTagList();
        compound.setInteger("squadid", getSquadID());
        compound.setInteger("missionid", getMissonID());
    }
	public void readEntityFromNBT(NBTTagCompound compound)
    {
        super.readEntityFromNBT(compound);
        this.setSquadID(compound.getInteger("squadid"));
        this.setMissonID(compound.getInteger("missionid"));
    }
	protected void entityInit()
    {
    	super.entityInit();
    	this.dataManager.register(squadid, Integer.valueOf(1));
    	this.dataManager.register(missionid, Integer.valueOf(0));
    }

	public int getSquadID() {
		return ((this.dataManager.get(squadid)).intValue());
	}

	public void setSquadID(int stack) {
		this.dataManager.set(squadid, Integer.valueOf(stack));
	}

	public int getMissonID() {
		return ((this.dataManager.get(missionid)).intValue());
	}

	public void setMissonID(int stack) {
		this.dataManager.set(missionid, Integer.valueOf(stack));
	}
	
	protected void setEquipmentBasedOnDifficulty(DifficultyInstance difficulty) {
		super.setEquipmentBasedOnDifficulty(difficulty);
		
	}
	
	public double getYOffset()
    {
		//-0.35D;
        return 0.1D;
    }
	
	public boolean attackEntityFrom(DamageSource source, float par2)
    {
    	Entity entity;
    	entity = source.getTrueSource();
    	
		if(entity != null){
			if(entity instanceof EntityPlayer && !mod_GirlFront.cfg_friendlyfire_player_to_doll){
				//GFPacketHandler.INSTANCE2.sendToAll(new GFClientMessageKeyPressed(801, this.getEntityId()));
				return false;
			}
			if(entity instanceof EntityNPC_Doll || entity instanceof EntityDoll){
				//GFPacketHandler.INSTANCE2.sendToAll(new GFClientMessageKeyPressed(801, this.getEntityId()));
	    		return false;
	        }else {
				int ran = this.world.rand.nextInt(100);
				if (ran > evasion) {
					return super.attackEntityFrom(source, par2);
				} else {
					//GFPacketHandler.INSTANCE2.sendToAll(new GFClientMessageKeyPressed(801, this.getEntityId()));
					return false;
				}
	        }
		}else{
			return super.attackEntityFrom(source, par2);
		}
    }
	
	public boolean CanAttack(Entity entity){
    	if(entity instanceof IMob && ((EntityLivingBase) entity).getHealth() > 0.0F){
    		return true;
    	}else
    	{
    		return false;
    	}
    }
	
	/**
     * Determines if an entity can be despawned, used on idle far away entities
     */
    protected boolean canDespawn()
    {
    	return false;
    }
	
	protected void dropLoot(boolean wasRecentlyHit, int lootingModifier, DamageSource source)
    {
        //this.dropFewItems(wasRecentlyHit, lootingModifier);
        //this.dropEquipment(wasRecentlyHit, lootingModifier);
    }
	
	public void getbeacon() {
    }
	
	public boolean usehand = false;
	
	public boolean processInteract(EntityPlayer player, EnumHand hand)
    {
    	ItemStack itemstack = player.getHeldItem(hand);
        boolean flag = !itemstack.isEmpty();
        return true;
    }
	
	/*public boolean processInteract(EntityPlayer player, EnumHand hand)
    {
    	this.rotationYaw = this.rotationYawHead = this.rotation =  this.rotationYaw + 1;
		return true;
    }*/
	
	 protected void onDeathUpdate() {
		 ++this.deathTicks;
		 if(this.getMissonID() == 1) {
			 super.onDeathUpdate();
		 }else {
			 
		 }
		
		
		 if(this.deathTicks == 1) { }
	}
	 
	 public void onUpdate()
	    {
	        super.onUpdate();

	        if (!this.world.isRemote && this.world.getDifficulty() == EnumDifficulty.PEACEFUL)
	        {
	            this.setDead();
	        }
	    }
	 
	 //getArmID_R  武器の種類＝モブのID
	 //getArmID_L  モブのレベル
	 //getArmID_S  EXP
	 //getRemain_L 残弾
	 //getRemain_R スキル
	 //getRemain_S　弾薬
	 //getRemain_A　飯
	
	 public ItemStack mainitem = new ItemStack((Item)null);// = nullはだめっぽい
	 
	 public int task_progress = 0;
	 public int task_time_progress = 0;
	 
	 public boolean task_clear = false;
	 public int task_clear_time = 0;
	 public int mob_spawn = 0;
	 
	 public boolean task_failed = false;
	/**
	 * Called frequently so the entity can update its state every tick as
	 * required. For example, zombies and skeletons use this to react to
	 * sunlight and start to burn.
	 */
	 public String sound;
	 public String modid;
	 public boolean reset = true;
	public void onLivingUpdate() {
		super.onLivingUpdate();
		this.biped = false;
		this.updateArmSwingProgress();
		
		
		if(!this.world.isRemote) {
			if (this.getHealth() > 0.0F) {
				this.deathTicks = 0;
			}
			if(this.getMissonID() != 1) {
				if(this.deathTicks > 0 && this.cooltime5 > 300) {
					this.setHealth(20);
					this.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0F, 1.0F);
					this.deathTicks = 0;
					this.cooltime5 = 0;
				}
			}
			
			if(this.getHealth() < this.getMaxHealth()) {
				if(this.cooltime5 > 100 && deathTime == 0 && deathTicks == 0){
					this.setHealth(this.getHealth() + 1);
					this.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0F, 1.0F);
					this.cooltime5 = 0;
				}
			}
		}
		
		
		//何が何でも銃は手放さない
		if (this.getRidingEntity() == null) {// 1
			if( this.getItemStackFromSlot(EntityEquipmentSlot.MAINHAND).isEmpty()) {
				if(!mainitem.isEmpty()) {
					this.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, mainitem);
				}
			}
		}
		
		NPCDollAI_Level.load(this);
		NPCDollAI_Reload.load(this);
		NPCDollAI_MasterArm.load(this);
		
		
		if(this.getMissonID() == 1) {
			NPCDoll_Misson1.load(this);
		}
		if(this.getMissonID() == 2) {
			NPCDoll_Misson2.load(this);
		}
		
		
		
		
		
		
		if(this.getOwner() == null) {
			List llist = this.world.getEntitiesWithinAABBExcludingEntity(this, this.getEntityBoundingBox()
	        		.expand(this.motionX, this.motionY, this.motionZ).grow(15.0D));
			if (llist != null) {
				for (int lj = 0; lj < llist.size(); lj++) {
					Entity entity1 = (Entity) llist.get(lj);
					if (entity1.canBeCollidedWith() && entity1 != null) {
						if (entity1 instanceof EntityLivingBase && entity1 instanceof EntityPlayer)
                        {
							EntityPlayer playerIn = (EntityPlayer) entity1;
							this.setTamedBy(playerIn);
							break;
                        }
					}
				}
			}
			this.setMoveT(3);
			this.addPotionEffect(new PotionEffect(MobEffects.GLOWING, 10, 10));
		}else {
			this.setMoveT(0);
		}
    	
	}
}