package hmggirlfront.entity.vehicle;

import gvclib.mod_GVCLib;
import gvclib.entity.living.EntityGVCLivingBase;
import gvclib.entity.living.PL_RoteModel;
import gvclib.network.GVCLClientMessageKeyPressed;
import gvclib.network.GVCLMessageKeyPressed;
import gvclib.network.GVCLPacketHandler;
import net.minecraft.entity.EntityLivingBase;

public class DollPL_AirCraftMove {
	public static void moveheli(EntityLivingBase player, EntityGVCLivingBase entity, float sp, float turnspeed){
		if(player.rotationYawHead > 360F || player.rotationYawHead < -360F){
			player.rotationYawHead = 0;
			player.rotationYaw = 0;
			player.prevRotationYaw = 0;
			player.prevRotationYawHead = 0;
			player.renderYawOffset = 0;
		}
		if(player.rotationYawHead > 180F){
			player.rotationYawHead = -179F;
			player.rotationYaw = -179F;
			player.prevRotationYaw = -179F;
			player.prevRotationYawHead = -179F;
			player.renderYawOffset = -179F;
		}
		if(player.rotationYawHead < -180F){
			player.rotationYawHead = 179F;
			player.rotationYaw = 179F;
			player.prevRotationYaw = 179F;
			player.prevRotationYawHead = 179F;
			player.renderYawOffset = 179F;
		}
		float f = Math.abs(entity.rotationYawHead - player.rotationYawHead);
		float f2 = entity.rotationYawHead - player.rotationYawHead;
		entity.rotation = player.prevRotationYawHead;
		entity.rotationp = player.prevRotationPitch;
		if(entity.onGround)
		{
			if(entity.thpower > 1){
			if(f <= turnspeed && f >= -turnspeed){
				entity.rotationYawHead = player.rotationYaw;
				entity.rotationYaw = player.rotationYawHead;
				entity.prevRotationYaw = player.prevRotationYawHead;
				entity.prevRotationYawHead = player.prevRotationYawHead;
				entity.renderYawOffset = player.prevRotationYawHead;
			}
			else if(f2 > 0.1F){
				if(f2 > 180F){
					PL_RoteModel.rotemodel(entity, + turnspeed/5);
				}else{
					PL_RoteModel.rotemodel(entity,- turnspeed/5);
				}
			}
			else if(f2 < -0.1F){
				if(f2 < -180F){
					PL_RoteModel.rotemodel(entity,- turnspeed/5);
				}else{
					PL_RoteModel.rotemodel(entity,+ turnspeed/5);
				}
			}
			/*if(player.rotationPitch > 30) {
				entity.rotationPitch = 30;
				entity.prevRotationPitch = 30;
			}else if(player.rotationPitch < -30) {
				entity.rotationPitch = -30;
				entity.prevRotationPitch = -30;
			}else {
				entity.rotationPitch = player.rotationPitch;
				entity.prevRotationPitch = player.prevRotationPitch;
			}*/
			//entity.rotationPitch = 0;
			//entity.prevRotationPitch = 0;
			}
		}else{
			if(f <= turnspeed*2 && f >= -turnspeed*2){
				entity.rotationYawHead = player.rotationYaw;
				entity.rotationYaw = player.rotationYawHead;
				entity.prevRotationYaw = player.prevRotationYawHead;
				entity.prevRotationYawHead = player.prevRotationYawHead;
				entity.renderYawOffset = player.prevRotationYawHead;
			}else
			if(f2 > 0.1F){
				if(f2 > 180F){
					PL_RoteModel.rotemodel(entity,+ turnspeed);
					if(entity.throte < 50){
						entity.throte = entity.throte + 2;
					}
				}else{
					PL_RoteModel.rotemodel(entity,- turnspeed);
					if(entity.throte > -50){
						entity.throte = entity.throte - 2;
					}
				}
			}
			else if(f2 < -0.1F){
				if(f2 < -180F){
					PL_RoteModel.rotemodel(entity,- turnspeed);
					if(entity.throte > -50){
						entity.throte = entity.throte - 2;
					}
				}else{
					PL_RoteModel.rotemodel(entity,+ turnspeed);
					if(entity.throte < 50){
						entity.throte = entity.throte + 2;
					}
				}
			}
			/*if(player.rotationPitch > 30) {
				entity.rotationPitch = 30;
				entity.prevRotationPitch = 30;
			}else if(player.rotationPitch < -20) {
				entity.rotationPitch = -20;
				entity.prevRotationPitch = -20;
			}else {
				entity.rotationPitch = player.rotationPitch;
				entity.prevRotationPitch = player.prevRotationPitch;
			}*/
			entity.rotep = player.rotationPitch;
			if ((entity.rotationPitch > (entity.rotep + turnspeed)) || (entity.rotationPitch < entity.rotep - turnspeed)) {
				float bai = 0.5F;
				if (entity.rotationPitch < entity.rotep) {
					entity.rotationp = entity.rotationp + turnspeed*bai;
					entity.rotationPitch = entity.rotationPitch + turnspeed*bai;
					entity.prevRotationPitch = entity.prevRotationPitch + turnspeed*bai;
				} else if (entity.rotationPitch > entity.rotep) {
					entity.rotationp = entity.rotationp - turnspeed*bai;
					entity.rotationPitch = entity.rotationPitch - turnspeed*bai;
					entity.prevRotationPitch = entity.prevRotationPitch - turnspeed*bai;
				}
			}
		}
		

		if (player.moveForward > 0.0F) {
			if(entity.throttle < entity.thmax){
				++entity.throttle;
			}
		}
		if (player.moveForward < 0.0F) {
			if(entity.throttle >= 1){
				--entity.throttle;
			}
		}
		if( entity.throttle >= 0){
			if(entity.thpower < entity.throttle){
				if( entity.throttle > entity.thmax-5){
					entity.thpower = entity.thpower + entity.thmaxa;
				}else{
					entity.thpower = entity.thpower + entity.thmaxa*0.5;
				}
			}else{
				entity.thpower = entity.thpower + entity.thmina;
			}
			if(entity.throttle <= 0 && entity.throttle > 0){
				entity.thpower = entity.thpower + entity.thmina * 2;
			}
		}
		if (player.moveStrafing < 0.0F && entity.thpower >= 1) {
			entity.moveangle = 3;
		}
		if (player.moveStrafing > 0.0F && entity.thpower >= 1) {
			entity.moveangle = 4;
		}
		boolean jump = mod_GVCLib.proxy.jumped();
		if (jump) {
			entity.serverspace = true;
			GVCLPacketHandler.INSTANCE.sendToServer(new GVCLMessageKeyPressed(13, entity.getEntityId()));
			GVCLPacketHandler.INSTANCE2.sendToAll(new GVCLClientMessageKeyPressed(13, entity.getEntityId()));
		}
		if (entity.serverspace) {
			entity.throttle_up = 1;
			entity.serverspace = false;
		}else if(player.isSneaking()) {
			entity.throttle_up = -1;
		}else {
			entity.throttle_up = 0;
		}
		
	}
}
