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 EntityIBC03 extends EntityIBCBase {

	public EntityIBC03(World worldIn) {
		super(worldIn);
		this.setSize(0.8F, 1.0F);
		this.experienceValue = 2;
		this.find_enemy_maxcount = 60;
	}

	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(5D);
		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 + 10) && 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(6) + 4 +this.world.getDifficulty().getDifficultyId()*2  + (this.getLevel() * 4);
        for(int ii = 0; ii < i; ++ii){
        	int ix = this.world.rand.nextInt(8);
			int iz = this.world.rand.nextInt(8);
        	EntityIBC03 entityskeleton = new EntityIBC03(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_parts), 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, 10, 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, 10, 30, 5, 5, 10);
		if(this.getcanDespawn() == 1)this.doblock(this, 80, mod_GirlFront.bdoll_ibc03);
		//this.getbeacon();
		
		
		int max = 5;
		int range = 20;
		int attack_range = 5;
		this.magazine = 30;
		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 > 1 && this.getRemain_L() > 0){
    			{
    				String model = "hmggirlfront:textures/entity/bullet/bullet_bullet_s.mqo";
		    		String tex = "hmggirlfront:textures/entity/bullet/bullet_bullet_beam.png";
		    		String modelf = "gvclib:textures/entity/mflash.mqo";
		    		String texf = "gvclib:textures/entity/mflash.png";
		    		SoundEvent sounds = GFSoundEvent.fire_ibc_hg;
		    		if(sounds == null) {
		    			sounds = SoundEvent.REGISTRY.getObject(new ResourceLocation(mod_GirlFront.MOD_ID, "fire_ibc_hg"));
		    		}
		        			AI_EntityWeapon_new we = new AI_EntityWeapon_new(this, this);
							we.id = 0;
							we.model = model;
							we.tex = tex;
							we.modelf = model;
							we.texf = tex;
							we.ftime = 0;
							we.sound = sounds;
							we.f = 0F;
							we.w = 0F;
							we.h = 0.85;
							we.z = 0.0;
							we.bx = 0.0F;
							we.by = 0.85;
							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 = 1 + 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 >= 1){
				this.gun_count1 = 0;
				this.counter1 = false;
				this.countlimit1 = 0;
			}
    	}
		IBCAI_EntityMoveS.newmove(this, 0, sp, 0, max, range, attack_range);
		
	}
}