package hmggirlfront.entity.mob;

import javax.annotation.Nullable;

import gvclib.entity.EntityT_Grenade;
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.init.SoundEvents;
import net.minecraft.inventory.EntityEquipmentSlot;
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 EntitySCM_CYCLOPS extends EntitySCMBase {

	public EntitySCM_CYCLOPS(World worldIn) {
		super(worldIn);
		this.setSize(0.5F, 1.8F);
		this.experienceValue = 10;
		this.find_enemy_maxcount = 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(80D);
		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() >= 2 && this.getCanSpawnHere_max(mod_GirlFront.cfg_spawner_limit_normal) && super.getCanSpawnHere();
	}
	
	protected void setEquipmentBasedOnDifficulty(DifficultyInstance difficulty) {
		super.setEquipmentBasedOnDifficulty(difficulty);
		this.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, new ItemStack(mod_GirlFront.subarm_ak12prt));
	}
	
	@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() * 1);
        for(int ii = 0; ii < i; ++ii){
        	int ix = this.world.rand.nextInt(8);
			int iz = this.world.rand.nextInt(8);
        	EntitySCM_CYCLOPS entityskeleton = new EntitySCM_CYCLOPS(this.world);
        	entityskeleton.setLocationAndAngles(this.posX - 4 +ix, this.posY+1, this.posZ - 4 +iz, 0, 0.0F);
	        entityskeleton.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, new ItemStack(mod_GirlFront.subarm_ak12prt));
	        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;
    	
    	if(this.boss_spawn)return;
    	
    	super.dropEquipment(wasRecentlyHit, lootingModifier);
    	{
    		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(30);
    		if(ra == 0){
    			this.entityDropItem(new ItemStack(mod_GirlFront.idoll_l_performance_circuit), 0.0F);
    		}
    	}
    }
    
    protected void onDeathUpdate() {
    	super.onDeathUpdate();
	}
    
	/**
	 * 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, 0.5F, 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;
			}
		}
		if(this.getRemain_R() <= 0){
			++reload2;
			if(reload2 >= 60){
				this.setRemain_R(1);
				reload2 = 0;
			}
		}
		float sp = 0.06F;
		Vec3d looked = this.getLookVec();
		
		//this.setAIType(0);
		this.updateArmSwingProgress();
		
		boolean exflag = false;
		
		IBCAI_Entity.getTargetEntity(this, 30, 50, 15, 10, 20);
		if(this.getcanDespawn() == 1)this.doblock(this, 80, mod_GirlFront.bsocom_cyclops);
		//this.getbeacon();
		
		
		int max = 25;
		int range = 50;
		int attack_range = 40;
		this.magazine = 30;
		this.reload_time1 = 60;
		if(this.discover_count > 10){
			if(this.getAIType() == 1) {
				if(this.getattacktask()){
		    		if(this.cooltime > 40 && this.getRemain_R() > 0)
					{
		    			{
			    			{
								String model = "gvclib:textures/entity/BulletNormal.obj";
					    		String tex = "gvclib:textures/entity/BulletNormal.png";
					    		String modelf = "hmggirlfront:textures/entity/mflash_ibc.mqo";
					    		String texf = "hmggirlfront:textures/entity/mflash_ibc.png";
					    		SoundEvent sounds = GVCSoundEvent.fire_grenade;
					    		
					    		AI_EntityWeapon_new we = new AI_EntityWeapon_new(this, this);
								we.id = 2;
								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.2F;
								we.h = 1.5;
								we.z = 0.75;
								we.bx = 0.2F;
								we.by = 1.5;
								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 = 12 + this.world.getDifficulty().getDifficultyId();
								we.damegetime = false;
								we.speed = 2F;
								we.bure = 1F;
								we.ex = 3.0F;
								we.extrue = false;
								we.kazu = 1;
								we.gra = 0.00;
								we.maxtime = 80;
								we.dameid = 0;
								we.Attacktask(this, this, attack_range);
							}
							this.setRemain_R(this.getRemain_R() - 1);
							//this.cooltime2 = 0;
		    			}
		    			this.cooltime = 0;
					}
		    	}
			}else {
				if(this.cooltime > 15 && 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/bulletnormal.mqo";
			    		String tex = "hmggirlfront:textures/entity/bulletbeam.png";
			    		String modelf = "hmggirlfront:textures/entity/mflash_ibc.mqo";
			    		String texf = "hmggirlfront:textures/entity/mflash_ibc.png";
			    		SoundEvent sounds = GVCSoundEvent.fire_rifle2;
			    		if(sounds == null) {
			    			sounds = GVCSoundEvent.fire_rifle2;
			    		}
			        			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.2F;
								we.h = 1.5;
								we.z = 0.75;
								we.bx = 0.2F;
								we.by = 1.5;
								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 = 6 + this.world.getDifficulty().getDifficultyId();
								we.damegetime = true;
								we.speed = 4F;
								we.bure = 2F;
								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 >= 3){
					this.gun_count1 = 0;
					this.counter1 = false;
					this.countlimit1 = 0;
				}
			}
    	}
		SCMAI_EntityMoveS.newmove(this, 0, sp, 0, max, range, attack_range);
		
	}
}