package hmggirlfront.entity.npc;

import java.util.List;

import hmggirlfront.GFSoundEvent;
import hmggirlfront.mod_GirlFront;
import hmggirlfront.entity.mob.EntityIBC01;
import hmggirlfront.entity.mob.EntityIBC02;
import hmggirlfront.entity.mob.EntityIBC04;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.MobEffects;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.TextComponentTranslation;

public class NPCDoll_Misson1 {

	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.mission1.doll.talk0.name", new Object[0]));
									if(doll.task_progress == 1)player.sendMessage(new TextComponentTranslation("hmggirlfront.mission1.doll.talk1.name", new Object[0]));
									if(doll.task_progress == 2)player.sendMessage(new TextComponentTranslation("hmggirlfront.mission1.doll.talk2.name", new Object[0]));
									if(doll.task_progress == 3)player.sendMessage(new TextComponentTranslation("hmggirlfront.mission1.doll.talk3.name", new Object[0]));
									if(doll.task_progress == 4)player.sendMessage(new TextComponentTranslation("hmggirlfront.mission1.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;
								}
							}
						}
				
				//クリアー
				{
					{
						double d5 = doll.getMoveX() - doll.posX;
						double d7 = doll.getMoveZ() - 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 > 240){
							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.mission1.doll.clear0.name", new Object[0]));
							if(doll.task_clear_time == 80 && !doll.world.isRemote)player.sendMessage(new TextComponentTranslation("hmggirlfront.mission1.doll.clear1.name", new Object[0]));
							if(doll.task_clear_time == 140 && !doll.world.isRemote)player.sendMessage(new TextComponentTranslation("hmggirlfront.mission1.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();
							}
						}
					}
				}
				
				
				//モブの出現
				{
					++doll.mob_spawn;
					if(doll.getFTMode() == 1 && doll.mob_spawn >= 600) {
						doll.mob_spawn = 0;
						doll.playSound(GFSoundEvent.ibc_step, 1.0F, 1.0F);
						{
							double x = doll.posX;
							double y = doll.posY;
							double z = doll.posZ;
							double offset_x = 30;
							double offset_z = 30;
							if(doll.world.rand.nextInt(2) == 0)offset_x = offset_x * -1;
							if(doll.world.rand.nextInt(2) == 0)offset_z = offset_z * -1;
							x = x + offset_x;
							z = z + offset_z;
							int i = doll.world.rand.nextInt(3)+2;
							if(doll.world.getBlockState(new BlockPos(x, y + 1, z)).getBlock() != Blocks.AIR
									&& doll.world.getBlockState(new BlockPos(x, y + 2, z)).getBlock() != Blocks.AIR) {
								for(int yy = 0; yy < 30; ++yy){
									if(doll.world.getBlockState(new BlockPos(x, y + yy, z)).getBlock() == Blocks.AIR
											&& doll.world.getBlockState(new BlockPos(x, y + yy + 1, z)).getBlock() == Blocks.AIR) {
										y = yy;
										break;
									}
								}
							}
							int ran = doll.world.rand.nextInt(4);
							
		     		        for(int ii = 0; ii < i; ++ii){
		     		        	if(ran == 1) {
		     		        		int ix = doll.world.rand.nextInt(8);
			     					int iz = doll.world.rand.nextInt(8);
			     		        	EntityIBC02 entityskeleton = new EntityIBC02(doll.world);
			     		        	entityskeleton.targetentity = doll;
			     		        	entityskeleton.setattacktask(true);
			     		        	entityskeleton.setMoveT(2);
			     		        	entityskeleton.setLocationAndAngles(x - 4 +ix, y+1, z - 4 +iz, 0, 0.0F);
			     			        if(!doll.world.isRemote)doll.world.spawnEntity(entityskeleton);
		     		        	}
		     		        	if(ran == 2) {
		     		        		int ix = doll.world.rand.nextInt(8);
			     					int iz = doll.world.rand.nextInt(8);
			     		        	EntityIBC04 entityskeleton = new EntityIBC04(doll.world);
			     		        	entityskeleton.targetentity = doll;
			     		        	entityskeleton.setattacktask(true);
			     		        	entityskeleton.setMoveT(2);
			     		        	entityskeleton.setLocationAndAngles(x - 4 +ix, y+1, z - 4 +iz, 0, 0.0F);
			     		        	entityskeleton.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, new ItemStack(mod_GirlFront.dollgun_ibc04smg));
			     		        	entityskeleton.setItemStackToSlot(EntityEquipmentSlot.OFFHAND, new ItemStack(mod_GirlFront.dollgun_ibc04smg));
			     			        if(!doll.world.isRemote)doll.world.spawnEntity(entityskeleton);
		     		        	}
		     		        	else {
		     		        		int ix = doll.world.rand.nextInt(8);
			     					int iz = doll.world.rand.nextInt(8);
			     		        	EntityIBC01 entityskeleton = new EntityIBC01(doll.world);
			     		        	entityskeleton.targetentity = doll;
			     		        	entityskeleton.setattacktask(true);
			     		        	entityskeleton.setMoveT(2);
			     		        	entityskeleton.setLocationAndAngles(x - 4 +ix, y+1, z - 4 +iz, 0, 0.0F);
			     			        if(!doll.world.isRemote)doll.world.spawnEntity(entityskeleton);
		     		        	}
		     		        }
						}
					}
				}
				
				//部隊の誰かが死亡
				{
					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) {
						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();
						}
					}
				}
	}
}
