package hmggirlfront.entity.mob;

import javax.annotation.Nullable;

import gvclib.entity.living.AI_EntityWeapon;
import gvclib.entity.living.AI_EntityWeapon_new;
import gvclib.event.GVCSoundEvent;
import hmggirlfront.GFSoundEvent;
import hmggirlfront.mod_GirlFront;
import hmggirlfront.entity.mob.ai.IBCAI_Entity;
import hmggirlfront.entity.mob.ai.IBCAI_EntityMoveS;
import net.minecraft.entity.IEntityLivingData;
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.item.ItemStack;
import net.minecraft.util.DamageSource;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.DifficultyInstance;
import net.minecraft.world.World;

public class EntityIBC01 extends EntityIBCBase {

	public EntityIBC01(World worldIn) {
		super(worldIn);
		this.setSize(1.5F, 1.4F);
		this.experienceValue = 10;
		this.find_enemy_maxcount = 100;
	}

	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(100.0D);
	}

	public boolean getCanSpawnHere() {
		return this.getLevel() >= 0 && this.getCanSpawnHere_max(mod_GirlFront.cfg_spawner_limit_easy) && super.getCanSpawnHere();
	}
	
	protected void setEquipmentBasedOnDifficulty(DifficultyInstance difficulty) {
		super.setEquipmentBasedOnDifficulty(difficulty);
		
	}
	@Nullable
    public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, @Nullable IEntityLivingData livingdata)
    {
        livingdata = super.onInitialSpawn(difficulty, livingdata);
        int i = this.world.rand.nextInt(3) +this.world.getDifficulty().getDifficultyId() + (this.getLevel() * 2);
        for(int ii = 0; ii < i; ++ii){
        	int ix = this.world.rand.nextInt(8);
			int iz = this.world.rand.nextInt(8);
        	EntityIBC01 entityskeleton = new EntityIBC01(this.world);
        	entityskeleton.setLocationAndAngles(this.posX - 4 +ix, this.posY+1, this.posZ - 4 +iz, 0, 0.0F);
	        this.world.spawnEntity(entityskeleton);
        }
        return livingdata;
    }
	/**
     * Drop the equipment for this entity.
     */
    protected void dropEquipment(boolean wasRecentlyHit, int lootingModifier)
    {
    	DamageSource source = this.getLastDamageSource();
    	int dame = 0;
    	if(source == null || source.getTrueSource() == null) {
    		dame = 1;
    	}else {
    		dame = 0;
    	}
    	if(dame == 1)return;
    	
    	
    	{
    		int ra = this.world.rand.nextInt(2);
    		if(ra == 0){
        		this.entityDropItem(new ItemStack(mod_GirlFront.idoll_bullet), 0.0F);
        	}
    	}
    	{
    		int ra = this.world.rand.nextInt(6);
    		if(ra == 0){
        		this.entityDropItem(new ItemStack(mod_GirlFront.idoll_food), 0.0F);
        	}
    		if(ra == 1){
        		this.entityDropItem(new ItemStack(mod_GirlFront.idoll_hp), 0.0F);
        	}
    		if(ra == 2){
        		this.entityDropItem(new ItemStack(mod_GirlFront.idoll_bullet), 0.0F);
        	}
    		if(ra == 3){
        		this.entityDropItem(new ItemStack(mod_GirlFront.idoll_parts), 0.0F);
        	}
    	}
    	{
    		int ra = this.world.rand.nextInt(60);
    		if(ra == 0){
        		this.entityDropItem(new ItemStack(mod_GirlFront.idoll_doll_contract), 0.0F);
        	}
    		if(ra == 1){
        		this.entityDropItem(new ItemStack(mod_GirlFront.idoll_doll_rapidcontract), 0.0F);
        	}
    		if(ra == 2){
        		this.entityDropItem(new ItemStack(mod_GirlFront.idoll_doll_repair), 0.0F);
        	}
    		if(ra == 4){
        		this.entityDropItem(new ItemStack(mod_GirlFront.idoll_squad_cn), 0.0F);
        	}
    		
    		if(ra == 10){
        		this.entityDropItem(new ItemStack(mod_GirlFront.dollam_horo), 0.0F);
        	}
    		if(ra == 11){
        		this.entityDropItem(new ItemStack(mod_GirlFront.dollam_dotsight), 0.0F);
        	}
    		if(ra == 12){
        		this.entityDropItem(new ItemStack(mod_GirlFront.dollam_bullet_ap), 0.0F);
        	}
    		if(ra == 13){
        		this.entityDropItem(new ItemStack(mod_GirlFront.dollam_bullet_hv), 0.0F);
        	}
    		if(ra == 14){
        		this.entityDropItem(new ItemStack(mod_GirlFront.dollam_frame_m), 0.0F);
        	}
    		if(ra == 15){
        		this.entityDropItem(new ItemStack(mod_GirlFront.dollam_frame_x), 0.0F);
        	}
    		if(ra == 16){
        		this.entityDropItem(new ItemStack(mod_GirlFront.dollam_acog), 0.0F);
        	}
    		if(ra == 17){
        		this.entityDropItem(new ItemStack(mod_GirlFront.dollam_scope), 0.0F);
        	}
    		if(ra == 18){
        		this.entityDropItem(new ItemStack(mod_GirlFront.dollam_lasersight), 0.0F);
        	}
    		
    		if(ra == 19){
        		this.entityDropItem(new ItemStack(mod_GirlFront.dollam_micro), 0.0F);
        	}
    		if(ra == 20){
        		this.entityDropItem(new ItemStack(mod_GirlFront.dollam_coyote), 0.0F);
        	}
    		if(ra == 21){
        		this.entityDropItem(new ItemStack(mod_GirlFront.dollam_kobra), 0.0F);
        	}
    		if(ra == 22){
        		this.entityDropItem(new ItemStack(mod_GirlFront.dollam_suppressor), 0.0F);
        	}
    		if(ra == 23){
        		this.entityDropItem(new ItemStack(mod_GirlFront.dollam_grip), 0.0F);
        	}
    		if(ra == 24){
        		this.entityDropItem(new ItemStack(mod_GirlFront.dollam_bipod), 0.0F);
        	}
    		if(ra == 25){
        		this.entityDropItem(new ItemStack(mod_GirlFront.dollam_anglegrip), 0.0F);
        	}
    		if(ra == 26){
        		this.entityDropItem(new ItemStack(mod_GirlFront.dollam_gripod), 0.0F);
        	}
    		if(ra == 27){
        		this.entityDropItem(new ItemStack(mod_GirlFront.dollam_bullet_backshot), 0.0F);
        	}
    		if(ra == 28){
        		this.entityDropItem(new ItemStack(mod_GirlFront.dollam_bullet_srag), 0.0F);
        	}
    		if(ra == 29){
        		this.entityDropItem(new ItemStack(mod_GirlFront.dollam_bullet_frag), 0.0F);
        	}
    		if(ra == 30){
        		this.entityDropItem(new ItemStack(mod_GirlFront.dollam_bullet_hp), 0.0F);
        	}
    	}
    }
    
    protected void onDeathUpdate() {
    	super.onDeathUpdate();
		if (this.deathTime == 1 && !this.world.isRemote) {
			//GVCExplosionBase.ExplosionKai(this, this, this.posX + 0, this.posY + 0, this.posZ + 0, 1, false,  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.
	 */
    int self_ex = 0;
	public void onLivingUpdate() {
		super.onLivingUpdate();
		this.biped = true;
		if(this.getRemain_L() <= 0){
			++reload1;
			if(reload1 == reload_time1 - 10){
				this.playSound(GVCSoundEvent.reload_mag, 1.0F, 1.0F);
				if(this.targetentity != null)IBCAI_Entity.notifyEntity(this, 20, this.targetentity, false);
			}
			if(reload1 >= reload_time1){
				this.setRemain_L(this.magazine);
				reload1 = 0;
			}
		}
		reload_time1 = 50;
		float sp = 0.04F;
		Vec3d looked = this.getLookVec();
		
		//this.setAIType(0);
		this.updateArmSwingProgress();
		
		boolean exflag = false;
		
		IBCAI_Entity.getTargetEntity(this, 20, 30, 10, 10, 20);
		if(this.getcanDespawn() == 1)this.doblock(this, 60, mod_GirlFront.bdoll_ibc01);
		//this.getbeacon();
		
		
		int max = 10;
		int range = 30;
		int attack_range = 15;
		this.magazine = 10;
		if(this.discover_count > 20 - (this.world.getDifficulty().getDifficultyId() * 5)){
    		if(this.cooltime > 50 && this.getRemain_L() > 0)
			{
    			this.counter1 = true;
    			this.cooltime = 0;
			}
    		if(this.counter1 && this.gun_count1 > 5 && this.getRemain_L() > 0){
    			/*SoundEvent sounds = GFSoundEvent.fire_ibc_rifle;
    			
    			AI_EntityWeapon.Attacktask(this, this, 0, 15, 
    					"hmggirlfront:textures/entity/bulletnormal.mqo", "hmggirlfront:textures/entity/bulletbeam.png",
    					"gvclib:textures/entity/mflash.mqo", "gvclib:textures/entity/mflash.png",2, 
    					sounds, 
    					0, 0, 1.5, 0.5,this.posX, this.posY, this.posZ,looked,this.rotationYawHead, 20, 10,
						2, 2, 3, 0, false, 1, 0.029, 80, 0);*/
    			{
					String model = "hmggirlfront:textures/entity/bullet/bullet_bullet_s.mqo";
		    		String tex = "hmggirlfront:textures/entity/bullet/bullet_bullet_beam.png";
		    		String modelf = "hmggirlfront:textures/entity/mflash_ibc.mqo";
		    		String texf = "hmggirlfront:textures/entity/mflash_ibc.png";
		    		SoundEvent sounds = GFSoundEvent.fire_ibc_hg;
		    		if(sounds == null) {
		    			sounds = SoundEvent.REGISTRY.getObject(new ResourceLocation(mod_GirlFront.MOD_ID, "fire_ibc_hg"));
		    		}
		    		if(sounds == null) {
		    			System.out.println(String.format("soundnull"));
		    		}
		        			AI_EntityWeapon_new we = new AI_EntityWeapon_new(this, this);
							we.id = 0;
							we.model = model;
							we.tex = tex;
							we.modelf = modelf;
							we.texf = texf;
							we.ftime = 0;
							we.sound = sounds;
							we.f = 1.57F;
							we.w = 0.5F;
							we.h = 1.05;
							we.z = 0.5;
							we.bx = 0.0F;
							we.by = 1.05;
							we.bz = 0.0;
							we.follow_rote = true;//向いている方向に
							we.xoffset = 0;
							we.yoffset = 0;
							we.px = this.posX;
							we.py = this.posY;
							we.pz = this.posZ;
							we.lock = looked;
							we.rote = this.rotationYawHead;
							we.maxy = 20;
							we.miny = 10;
							
							we.power = 2 + this.world.getDifficulty().getDifficultyId();
							we.damegetime = false;
							we.speed = 2F;
							we.bure = 5F;
							we.ex = 0;
							we.extrue = false;
							we.kazu = 1;
							we.gra = 0.029;
							we.maxtime = 80;
							we.dameid = 0;
							we.Attacktask(this, this, attack_range);
				}
				this.gun_count1 = 0;
				if(this.firetrue){
					this.setRemain_L(this.getRemain_L() - 1);
				}
				++this.countlimit1;
			}
			if(this.countlimit1 >= 2){
				this.gun_count1 = 0;
				this.counter1 = false;
				this.countlimit1 = 0;
			}
    	}
		IBCAI_EntityMoveS.newmove(this, 0, sp, 0, max, range, attack_range);
		
	}
}