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_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 EntityIBCML extends EntityIBCBase {

	public EntityIBCML(World worldIn) {
		super(worldIn);
		this.setSize(3.0F, 2.8F);
		this.experienceValue = 10;
		this.rotationp_max = -90;
		this.rotationp_min = 0;
	}

	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(100D);
		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 && super.getCanSpawnHere();
	}
	
	protected void setEquipmentBasedOnDifficulty(DifficultyInstance difficulty) {
		super.setEquipmentBasedOnDifficulty(difficulty);
		
	}
	@Nullable
    public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, @Nullable IEntityLivingData livingdata)
    {
        livingdata = super.onInitialSpawn(difficulty, livingdata);
        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);
        	}
    	}
    }
    
    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(reload1 >= reload_time1){
				this.setRemain_L(this.magazine);
				reload1 = 0;
			}
		}
		reload_time1 = 100;
		float sp = 0F;
		Vec3d looked = this.getLookVec();
		
		//this.setAIType(0);
		this.updateArmSwingProgress();
		
		boolean exflag = false;
		int range = 60;
		
		AI_EntityWeapon.getTargetEntity(this, range, 50, -5);
		//this.getbeacon();
		
		if(this.getMoveT() == 0) {
			this.setMoveT(1);
			this.setMoveX((int)this.posX);
			this.setMoveY((int)this.posY);
			this.setMoveZ((int)this.posZ);
		}
		if(this.getMoveT() == 1)this.setPosition(this.getMoveX(), this.getMoveY(), this.getMoveZ());
		
		
		int max = 10;
		this.magazine = 6;
		{
    		if(this.cooltime > 50 && this.getRemain_L() > 0)
			{
    			this.counter1 = true;
    			this.cooltime = 0;
			}
    		if(this.counter1 && this.gun_count1 > 15 && this.getRemain_L() > 0){
    			{
					String model = "gvclib:textures/entity/BulletRocket.obj";
		    		String tex = "gvclib:textures/entity/BulletRocket.png";
		    		String modelf = "gvclib:textures/entity/msmoke.mqo";
		    		String texf = "gvclib:textures/entity/msmoke.png";
		    		SoundEvent sounds = GVCSoundEvent.fire_roket;
		    		if(sounds == null) {
		    			System.out.println(String.format("soundnull"));
		    		}
		    		{
		    			AI_EntityWeapon_new we = new AI_EntityWeapon_new(this, this);
						we.id = 3;
						we.model = model;
						we.tex = tex;
						we.modelf = modelf;
						we.texf = texf;
						we.ftime = 10;
						we.sound = sounds;
						if(this.getRemain_L() %2 == 0) {
							we.f = 1.57F;
						}else {
							we.f = -1.57F;
						}
						we.w = 0.3F;
						we.h = 1.65;
						we.z = 0.5;
						we.bx = 0.0F;
						we.by = 1.65;
						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 = 60;
						we.miny = 0;
						
						we.power = 5 + this.world.getDifficulty().getDifficultyId();
						we.damegetime = false;
						we.speed = 3F;
						we.bure = 5F;
						we.ex = 2;
						we.extrue = false;
						we.kazu = 1;
						we.gra = 0.02999;
						we.maxtime = 80;
						we.dameid = 0;
						we.Attacktask(this, this, 60);
		    		}
		        			
				}
				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, range);
		
	}
}