package hmggirlfront.entity.vehicle;

import gvclib.entity.living.EntityGVCLivingBase;
import net.minecraft.entity.MoverType;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.DamageSource;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;

public class DollAI_AirCraftSet {
	public static void setheli(EntityGVCLivingBase entity, SoundEvent sound, float f1, float sp, float brake){
		Vec3d look = entity.getLookVec();
		double dx = Math.abs(entity.posX - entity.prevPosX);
		double dz = Math.abs(entity.posZ - entity.prevPosZ);
		float dd = (float)Math.sqrt((dx * dx) + (dz * dz)) * 20;
		if(dd > 5 && (entity.collidedHorizontally) && entity != null){
			entity.attackEntityFrom(DamageSource.causeIndirectDamage(entity, entity), 10F);
		}
		if(entity.throttle > 1){
			//entity.motionY *= 0.2;
		}
		double x = 0;
		double y = -0.1;
		double z = 0;
		//if( entity.throttle >= 1)
		{
			double speed = -entity.rotationPitch * entity.thpower;
			x += MathHelper.sin(entity.rotationYawHead * 0.01745329252F) * sp * speed * 0.015;
			z -= MathHelper.cos(entity.rotationYawHead * 0.01745329252F) * sp * speed *0.015;
		}
		if(entity.moveangle == 3) {
			x += MathHelper.sin(entity.rotationYawHead * 0.01745329252F - 1.57F) * sp;
			z -= MathHelper.cos(entity.rotationYawHead * 0.01745329252F - 1.57F) * sp;
		}
		if(entity.moveangle == 4) {
			x += MathHelper.sin(entity.rotationYawHead * 0.01745329252F + 1.57F) * sp;
			z -= MathHelper.cos(entity.rotationYawHead * 0.01745329252F + 1.57F) * sp;
		}
		//if(entity.thpower > entity.thmax / 2)
		if(entity.thpower > 1)
		{
			/*
			if(entity.rotationPitch > -25 && entity.rotationPitch < 25){
				y += 0.01 * (entity.thpower + (25 - Math.abs(entity.rotationPitch))  * 0.1);
			}
			else if(entity.rotationPitch > -45 && entity.rotationPitch < 45){
				double yy = 0.01 * (entity.thpower + (45 - Math.abs(entity.rotationPitch))  * 0.1);
				if(yy > 0.1) {
					y += 0.1;
				}else {
					y += yy;
				}
				//y += 0.1;
				//System.out.println(String.format("%1$.2f", y));
			}else {
				//y -= (90 - Math.abs(entity.rotationPitch)) * 0.01 * entity.thpower;
			}*/
			//y += 0.025 * entity.thpower;
			if(entity.throttle_up == 1 && entity.thpower > 5) {
				y += 0.01 * entity.thpower;
				//System.out.println(String.format("%1$.2f", y));
			}else if(entity.throttle_up == -1) {
				y += 0;
				//System.out.println(String.format("%1$.2f", y));
			}else {
				y += 0.1;
				//System.out.println(String.format("%1$.2f", y));
			}
		}
		if(entity.thpower > 1) 
		{
			entity.motionX = x;
			entity.motionZ = z;
			entity.motionY = y;
			entity.move(MoverType.PLAYER, entity.motionX, entity.motionY, entity.motionZ);
		}
		
		
		
		if(entity.getControllingPassenger() == null  && entity.getHealth() > 0.0F) 
		{
			if(entity.thpower > 1){
				entity.thpower = entity.thpower - brake;
			}
			if(entity.throttle > 1){
				--entity.throttle;
			}
			if(!entity.onGround)entity.rotationPitch = entity.rotationPitch + 0.5F;
			//entity.setVelocity(look.x, MathHelper.sin(-entity.rotationPitch * 0.017453292F), look.z);
		}
		if(entity.getHealth() <= 0.0F && entity.rotationPitch < 60) {
			if(!entity.onGround)entity.rotationPitch = entity.rotationPitch + 0.5F;
			//entity.setVelocity(look.x, MathHelper.sin(-entity.rotationPitch * 0.017453292F), look.z);
		}
		if(entity.throttle > 1){
			if(entity.ontick % 4 == 0)
			{
				entity.world.playSound((EntityPlayer)null, entity.posX, entity.posY, entity.posZ, sound, SoundCategory.WEATHER, 5.0F, 1.0F);
			}
		}
		setentityrote(entity);
		
		if(entity.onGround) {
			if(entity.rotationPitch > 1) {
				--entity.rotationPitch;
			}
			else if(entity.rotationPitch < -1) {
				++entity.rotationPitch;
			}
			if(entity.rotationPitch > 15 || entity.rotationPitch < -15) {
				entity.attackEntityFrom(DamageSource.causeIndirectDamage(entity, entity), 1F);
			}
			
		}
		
		if(entity.throte >= 0){
			--entity.throte;
		}
		if(entity.throte <= 0){
			++entity.throte;
		}
		if( entity.throttle >= 1){
			if(entity.thpera < 360){
				entity.thpera = entity.thpera + (entity.throttle*2);
			}else{
				entity.thpera = 0;
			}
		}
		entity.moveangle = 0;
	}
	
	public static void setentityrote(EntityGVCLivingBase entity) {
		if(entity.rotationYawHead > 360F || entity.rotationYawHead < -360F){
        	entity.rotation = 0;
			entity.rotationp = 0;
			entity.rotationYawHead = 0;
			entity.rotationYaw = 0;
			entity.prevRotationYaw = 0;
			entity.prevRotationYawHead = 0;
			entity.renderYawOffset = 0;
		}
		if(entity.rotationYawHead > 180F){
			entity.rotation = -179F;
			entity.rotationp = -179F;
			entity.rotationYawHead = -179F;
			entity.rotationYaw = -179F;
			entity.prevRotationYaw = -179F;
			entity.prevRotationYawHead = -179F;
			entity.renderYawOffset = -179F;
		}
		if(entity.rotationYawHead < -180F){
			entity.rotation = 179F;
			entity.rotationp = 179F;
			entity.rotationYawHead = 179F;
			entity.rotationYaw = 179F;
			entity.prevRotationYaw = 179F;
			entity.prevRotationYawHead = 179F;
			entity.renderYawOffset = 179F;
		}
	}
}
