package hmggirlfront.entity;

import java.util.List;

import gvclib.item.ItemMagazine;
import hmggirlfront.mod_GirlFront;
import hmggirlfront.items.ItemDollAttachment;
import hmggirlfront.items.ItemDollGunBase;
import net.minecraft.entity.Entity;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.init.SoundEvents;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.AxisAlignedBB;

public class DollAI_Level {
	public static void load(EntityDoll doll) {
		if(doll.getArmID_S() >= doll.getArmID_L() * 5) {
			doll.setArmID_L(doll.getArmID_L() + 1);
			doll.setArmID_S(0);
			doll.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(20D + doll.getArmID_L() + doll.getArmID_A());
			doll.setHealth((float) (20D + doll.getArmID_L() + doll.getArmID_A()));
			doll.playSound(SoundEvents.ENTITY_PLAYER_LEVELUP, 1.0F, 1.0F);
		}
		if(doll.getArmID_L() == 0) {
			doll.setArmID_L(1);
		}
		if(doll.getArmID_L() >= 101) {
			doll.setArmID_L(100);
		}
		
		
		ItemStack main = doll.getHeldItemMainhand();
		if(!main.isEmpty()){
			doll.mainitem = main;
			//初期設定
			//初期化もここで行う
			if (main.getItem() instanceof ItemDollGunBase) 
	    	{
				ItemDollGunBase gun = (ItemDollGunBase) main.getItem();
				doll.gun_class = gun.gun_class;
				doll.bullet_type = gun.magazine;
				if(gun.magazine != null && gun.magazine instanceof ItemMagazine) {
					ItemMagazine mag = (ItemMagazine) gun.magazine;
					doll.bullet_type_tab = mag.magazine_tab;
				}
				doll.evasion = gun.doll_evasion + doll.getArmID_L() * 2;
				doll.power = gun.powor;
				doll.bure = gun.bure;
				doll.pellet = gun.shotgun_pellet;
				int rp = gun.mobammo - (int)(doll.getArmID_L()/2);
				if(gun.hoplites_kazu >= 1) {
					rp = rp - HL_Dummy(doll) * 10;
				}
				if (rp <= gun.mobcycle * gun.mobbarst) {
					doll.rpm = gun.mobcycle * gun.mobbarst + 1;
				} else {
					doll.rpm = rp;
				}
				doll.sound = gun.soundre;
	    		doll.modid = gun.modid;
				doll.setArmID_A(gun.doll_hp);
				doll.doll_size = gun.doll_size;
				doll.doll_skill = gun.doll_skill;
				doll.doll_autoskill = gun.doll_autoskill;
				doll.doll_armor = gun.doll_armor;
				doll.doll_armorbase = gun.doll_armor;
				doll.doll_hp_recovery = gun.doll_hp_recovery;
	    	}
		}
		doll.evasion_base = doll.evasion;
		doll.power_base = doll.power;
		doll.bure_base = doll.bure;
		doll.rpm_base = doll.rpm;
		doll.pellet_base = doll.pellet;
		
		ItemStack head = doll.getItemStackFromSlot(EntityEquipmentSlot.HEAD);
		if(!head.isEmpty() &&  head.getItem() instanceof ItemDollAttachment) {
			ItemDollAttachment am = (ItemDollAttachment) head.getItem();
			if(am.id == 101)
			{
				
			}
		}
		ItemStack chest = doll.getItemStackFromSlot(EntityEquipmentSlot.CHEST);
		if(!chest.isEmpty() &&  chest.getItem() instanceof ItemDollAttachment) {
			ItemDollAttachment am = (ItemDollAttachment) chest.getItem();
			doll.power = (int) (doll.power *  am.power);
			doll.bure = doll.bure * am.bure;
			doll.pellet = am.pellet;
			if(am.bulletid == 1) {
				doll.bullet_type_am = "+AP";
			}
		}
		
		ItemStack legs = doll.getItemStackFromSlot(EntityEquipmentSlot.LEGS);
		if(!legs.isEmpty() &&  legs.getItem() instanceof ItemDollAttachment) {
			ItemDollAttachment am = (ItemDollAttachment) legs.getItem();
			if(am.id == 80)
			{
				doll.evasion = doll.evasion + am.doll_evasion;
				doll.bure = doll.bure * am.doll_bure;
			}
			if(am.id == 81)
			{
				doll.doll_armor = doll.doll_armor + am.doll_armor;
			}
		}
		
		if(doll.evasion >= 90) {
			doll.evasion = 90;
		}
		
		
		
		//　回復処理
		if(doll.getHealth() < doll.getMaxHealth() && doll.getHealth() > 0) {
			++doll.doll_heal_time;
			int level = doll.getArmID_L() * 2;
			int speed = doll.doll_hp_recovery;
			if(doll.doll_hp_recovery - level < 1) {
				speed = 1;
			}else {
				speed = doll.doll_hp_recovery - level;
			}
			if(doll.getRemain_A() > 0 && doll.doll_heal_time > speed) {
				doll.setHealth(doll.getHealth() + 1);
				doll.setRemain_A(doll.getRemain_A() - 1);
				doll.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0F, 1.0F);
				doll.doll_heal_time = 0;
			}
		}
		
		//レベル1の時の処理。なおワールドリログ時にも呼ばれる
		{
			if(doll.getArmID_L() == 1 && doll.reset) {
				doll.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(20D + doll.getArmID_L() + doll.getArmID_A());
				doll.setHealth((float) (20D + doll.getArmID_L() + doll.getArmID_A()));
				doll.reset = false;
			}
		}
		
		if(mod_GirlFront.cfg_doll_debag) 
		{
			doll.setRemain_A(999);
			doll.setRemain_S(999);
		}
	}
	
	
	
	public static void load_dummy(EntityDoll_Dummy doll) {
		if(doll.getArmID_S() >= doll.getArmID_L() * 5) {
			doll.setArmID_L(doll.getArmID_L() + 1);
			doll.setArmID_S(0);
			doll.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(20D + doll.getArmID_L() + doll.getArmID_A());
			doll.setHealth((float) (20D + doll.getArmID_L() + doll.getArmID_A()));
		}
		if(doll.getArmID_L() == 0) {
			doll.setArmID_L(1);
		}
		if(doll.getArmID_L() >= 101) {
			doll.setArmID_L(100);
		}
		
		
		ItemStack main = doll.getHeldItemMainhand();
		ItemStack head = doll.getItemStackFromSlot(EntityEquipmentSlot.HEAD);
		
		if(!main.isEmpty()){
			doll.mainitem = main;
			//初期設定
			//初期化もここで行う
			if (main.getItem() instanceof ItemDollGunBase) 
	    	{
				ItemDollGunBase gun = (ItemDollGunBase) main.getItem();
				doll.gun_class = gun.gun_class;
				
				doll.evasion = gun.doll_evasion + doll.getArmID_L() * 2;
				
				doll.setArmID_A(gun.doll_hp);
				doll.doll_size = gun.doll_size;
				doll.doll_skill = gun.doll_skill;
				doll.doll_autoskill = gun.doll_autoskill;
				doll.doll_armor = gun.doll_armor;
				doll.doll_armorbase = gun.doll_armor;
				doll.doll_hp_recovery = gun.doll_hp_recovery;
	    	}
		}
		if(!head.isEmpty()){
			if (head.getItem() instanceof ItemDollGunBase) 
	    	{
				ItemDollGunBase gun = (ItemDollGunBase) head.getItem();
				doll.bullet_type = gun.magazine;
				if(gun.magazine != null && gun.magazine instanceof ItemMagazine) {
					ItemMagazine mag = (ItemMagazine) gun.magazine;
					doll.bullet_type_tab = mag.magazine_tab;
				}
				doll.sound = gun.soundre;
	    		doll.modid = gun.modid;
				doll.power = gun.powor;
				doll.bure = gun.bure;
				doll.pellet = gun.shotgun_pellet;
				int rp = gun.mobammo - (int)(doll.getArmID_L()/2);
				if (rp <= gun.mobcycle * gun.mobbarst) {
					doll.rpm = gun.mobcycle * gun.mobbarst + 1;
				} else {
					doll.rpm = rp;
				}
	    	}
		}
		
		doll.evasion_base = doll.evasion;
		doll.power_base = doll.power;
		doll.bure_base = doll.bure;
		doll.rpm_base = doll.rpm;
		doll.pellet_base = doll.pellet;
		
		
		
		if(doll.evasion >= 90) {
			doll.evasion = 90;
		}
		
		
		
		//　回復処理
		/*if(doll.getHealth() < doll.getMaxHealth()) {
			++doll.doll_heal_time;
			int level = doll.getArmID_L() * 2;
			int speed = doll.doll_hp_recovery;
			if(doll.doll_hp_recovery - level < 1) {
				speed = 1;
			}else {
				speed = doll.doll_hp_recovery - level;
			}
			if(doll.getRemain_A() > 0 && doll.doll_heal_time > speed) {
				doll.setHealth(doll.getHealth() + 1);
				doll.setRemain_A(doll.getRemain_A() - 1);
				doll.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0F, 1.0F);
				doll.doll_heal_time = 0;
			}
		}*/
		
		//レベル1の時の処理。なおワールドリログ時にも呼ばれる
		{
			if(doll.getArmID_L() == 1 && doll.reset) {
				doll.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(20D + doll.getArmID_L() + doll.getArmID_A());
				doll.setHealth((float) (20D + doll.getArmID_L() + doll.getArmID_A()));
				doll.reset = false;
			}
		}
		
		
		
		//debag_mode
		if(mod_GirlFront.cfg_doll_debag) 
		{
			doll.setRemain_A(999);
			doll.setRemain_S(999);
		}
	}
	
	
	public static int HL_Dummy(EntityDoll doll) {
		double k = doll.posX;
		double l = doll.posY;
		double i = doll.posZ;
		int han = 20;
		int bit = 0;
		AxisAlignedBB axisalignedbb = (new AxisAlignedBB((double) (k - han), (double) (l - han),
				(double) (i - han), (double) (k + han), (double) (l + han), (double) (i + han)))
						.grow(1);
		List<Entity> llist = doll.world.getEntitiesWithinAABBExcludingEntity(doll, axisalignedbb);
		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 EntityDoll_Dummy) {
						EntityDoll_Dummy dummy = (EntityDoll_Dummy) entity1;
						if(dummy.getDummyID() == bit && doll.getCustomNameTag().equals(dummy.getCustomNameTag())) {
							++bit;
						}
					}
				}
			}
		}
		return bit;
	}
}
