package hmggirlfront.entity.npc;

import java.util.List;

import hmggirlfront.entity.mob.EntityIBC10u;
import hmggirlfront.entity.mob.EntityIBCBoss01M;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.MobEffects;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.TextComponentTranslation;

public class NPCDoll_Misson2 {

	public static void load(EntityNPC_Doll doll) {
		//task進行
				boolean getplayer = false;
				EntityPlayer player = null;
				if(doll.getFTMode() == 1){
							List llist = doll.world.getEntitiesWithinAABBExcludingEntity(doll, doll.getEntityBoundingBox()
					        		.expand(doll.motionX, doll.motionY, doll.motionZ).grow(60.0D));
							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 EntityLivingBase && entity1 instanceof EntityPlayer)
				                        {
											EntityPlayer playerIn = (EntityPlayer) entity1;
											getplayer = true;
											player = playerIn;
											break;
				                        }
									}
								}
							}
							if(getplayer && player != null) {
								if(doll.task_time_progress == 0 && !doll.world.isRemote) {
									if(doll.task_progress == 0)player.sendMessage(new TextComponentTranslation("hmggirlfront.mission2.doll.talk0.name", new Object[0]));
									if(doll.task_progress == 1)player.sendMessage(new TextComponentTranslation("hmggirlfront.mission2.doll.talk1.name", new Object[0]));
									if(doll.task_progress == 2)player.sendMessage(new TextComponentTranslation("hmggirlfront.mission2.doll.talk2.name", new Object[0]));
									if(doll.task_progress == 3)player.sendMessage(new TextComponentTranslation("hmggirlfront.mission2.doll.talk3.name", new Object[0]));
									if(doll.task_progress == 4)player.sendMessage(new TextComponentTranslation("hmggirlfront.mission2.doll.talk4.name", new Object[0]));
									++doll.task_progress;
								}
								if(!doll.world.isRemote)++doll.task_time_progress;
								if(doll.task_time_progress > 50) {
									doll.task_time_progress = 0;
								}
							}
						}
				
				//クリアー
				{
					{
						List llist = doll.world.getEntitiesWithinAABBExcludingEntity(doll, doll.getEntityBoundingBox()
				        		.expand(doll.motionX, doll.motionY, doll.motionZ).grow(80.0D));
						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 EntityLivingBase)
			                        {
										if(entity1 instanceof EntityIBCBoss01M) 
										{
											EntityIBCBoss01M u = (EntityIBCBoss01M) entity1;
											if(u.deathTime > 0) {
												doll.task_clear = true;
											}
										}
			                        }
								}
							}
						}
					}
					if(doll.task_clear){
						 doll.addPotionEffect(new PotionEffect(MobEffects.GLOWING, 10, 10));
						++doll.task_clear_time;
						if(getplayer && player != null) {
							if(doll.task_clear_time == 20 && !doll.world.isRemote)player.sendMessage(new TextComponentTranslation("hmggirlfront.mission2.doll.clear0.name", new Object[0]));
							if(doll.task_clear_time == 80 && !doll.world.isRemote)player.sendMessage(new TextComponentTranslation("hmggirlfront.mission2.doll.clear1.name", new Object[0]));
							if(doll.task_clear_time == 140 && !doll.world.isRemote)player.sendMessage(new TextComponentTranslation("hmggirlfront.mission2.doll.clear2.name", new Object[0]));
						}
						if(doll.task_clear_time > 200) {
							for (int k = 0; k < 20; ++k)
				            {
				                double d2 = doll.world.rand.nextGaussian() * 0.02D;
				                double d0 = doll.world.rand.nextGaussian() * 0.02D;
				                double d1 = doll.world.rand.nextGaussian() * 0.02D;
				                doll.world.spawnParticle(EnumParticleTypes.EXPLOSION_NORMAL, doll.posX + (double)(doll.world.rand.nextFloat() * doll.width * 2.0F) - (double)doll.width, doll.posY + (double)(doll.world.rand.nextFloat() * doll.height), doll.posZ + (double)(doll.world.rand.nextFloat() * doll.width * 2.0F) - (double)doll.width, d2, d0, d1);
				            }
							if(!doll.world.isRemote) {
								doll.setDead();
							}
						}
					}
				}
				
				//輸送型撃破後の会話
				if(doll.getFTMode() == 1){
					boolean getu = false;
					List llist = doll.world.getEntitiesWithinAABBExcludingEntity(doll, doll.getEntityBoundingBox()
			        		.expand(doll.motionX, doll.motionY, doll.motionZ).grow(160.0D));
					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 EntityLivingBase && entity1 instanceof EntityIBC10u)
		                        {
									EntityIBC10u u = (EntityIBC10u) entity1;
									if(u.deathTime > 0) {
										if(u.deathTime == 20 && !doll.world.isRemote)player.sendMessage(
												new TextComponentTranslation("hmggirlfront.mission2.doll.break_u0.name", new Object[0]));
										if(u.deathTime == 80 && !doll.world.isRemote)player.sendMessage(
												new TextComponentTranslation("hmggirlfront.mission2.doll.break_u1.name", new Object[0]));
										if(u.deathTime == 160 && !doll.world.isRemote)player.sendMessage(
												new TextComponentTranslation("hmggirlfront.mission2.doll.break_u2.name", new Object[0]));
										
									}
		                        }
							}
						}
					}
				}
				
				//輸送機が離脱
				{
					boolean getu = false;
					List llist = doll.world.getEntitiesWithinAABBExcludingEntity(doll, doll.getEntityBoundingBox()
			        		.expand(doll.motionX, doll.motionY, doll.motionZ).grow(160.0D));
					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 EntityLivingBase && entity1 instanceof EntityIBC10u)
		                        {
									EntityIBC10u u = (EntityIBC10u) entity1;
									double d5 = entity1.posX - doll.posX;
									double d7 = entity1.posZ - doll.posZ;
									double ddx = Math.abs(d5);
									double ddz = Math.abs(d7);
									double dxz = MathHelper.sqrt(d5 * d5 + d7 * d7);
									//if ((ddx > 120 || ddz > 120)) 
									if(dxz > 50){
										u.addPotionEffect(new PotionEffect(MobEffects.GLOWING, 10, 10));
									}
									getu = true;
		                        }
								if(entity1 instanceof EntityIBCBoss01M) 
								{
									getu = true;
								}
							}
						}
					}
					if(!getu) {
						//doll.setHealth(-2000);
						doll.task_failed = true;
					}
				}
				
				
				//部隊の誰かが死亡
				{
					boolean get_ally = false;
					List llist = doll.world.getEntitiesWithinAABBExcludingEntity(doll, doll.getEntityBoundingBox()
			        		.expand(doll.motionX, doll.motionY, doll.motionZ).grow(40.0D));
					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 EntityLivingBase && entity1 instanceof EntityNPC_Doll)
		                        {
									EntityNPC_Doll doll2 = (EntityNPC_Doll) entity1;
									if(doll2.getSquadID() == doll.getSquadID() && doll2.deathTime > 1)
									{
										get_ally = true;
									}
		                        }
								
							}
						}
					}
					if(get_ally || doll.task_failed) {
						for (int k = 0; k < 20; ++k)
			            {
			                double d2 = doll.world.rand.nextGaussian() * 0.02D;
			                double d0 = doll.world.rand.nextGaussian() * 0.02D;
			                double d1 = doll.world.rand.nextGaussian() * 0.02D;
			                doll.world.spawnParticle(EnumParticleTypes.EXPLOSION_NORMAL, doll.posX + (double)(doll.world.rand.nextFloat() * doll.width * 2.0F) - (double)doll.width, doll.posY + (double)(doll.world.rand.nextFloat() * doll.height), doll.posZ + (double)(doll.world.rand.nextFloat() * doll.width * 2.0F) - (double)doll.width, d2, d0, d1);
			            }
						if(!doll.world.isRemote) {
							doll.setDead();
						}
					}
				}
				//プレイヤーが死亡
				{
					boolean get_ally = false;
					List llist = doll.world.getEntitiesWithinAABBExcludingEntity(doll, doll.getEntityBoundingBox()
			        		.expand(doll.motionX, doll.motionY, doll.motionZ).grow(80.0D));
					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 EntityLivingBase && entity1 instanceof EntityPlayer)
		                        {
									EntityPlayer player2 = (EntityPlayer) entity1;
									if(doll.isOwner(player2) && player2.deathTime > 1)
									{
										get_ally = true;
									}
		                        }
								
							}
						}
					}
					if(get_ally || doll.task_failed) {
						for (int k = 0; k < 20; ++k)
			            {
			                double d2 = doll.world.rand.nextGaussian() * 0.02D;
			                double d0 = doll.world.rand.nextGaussian() * 0.02D;
			                double d1 = doll.world.rand.nextGaussian() * 0.02D;
			                doll.world.spawnParticle(EnumParticleTypes.EXPLOSION_NORMAL, doll.posX + (double)(doll.world.rand.nextFloat() * doll.width * 2.0F) - (double)doll.width, doll.posY + (double)(doll.world.rand.nextFloat() * doll.height), doll.posZ + (double)(doll.world.rand.nextFloat() * doll.width * 2.0F) - (double)doll.width, d2, d0, d1);
			            }
						if(!doll.world.isRemote) {
							doll.setDead();
						}
					}
				}
	}
}
