----------------------------------------------------------------------------
-- @Author: ViperGTS96©-----------------------------------------------------
----------------------------------------------------------------------------
--------------------"The simplest design is the best design." --------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------

bigfootAirControl = {};

function bigfootAirControl:intialize()
	bigfootAirControl.keys = {};
	bigfootAirControl.keys.forward = Input.KEY_KP_8;
	bigfootAirControl.keys.backward = Input.KEY_KP_5;
	bigfootAirControl.keys.left = Input.KEY_KP_4;
	bigfootAirControl.keys.right = Input.KEY_KP_6;
end;

function bigfootAirControl:onLoad(self)
	self.bigfootAirControlEvents = {};
	self.bigfootAirControlEventIds = {};
	self.updateRoll = bigfootAirControl.updateRoll;
	self.updateRollInput = bigfootAirControl.updateRollInput;
	self.rollDelay = bigfootAirControl.rollDelay;
	self.setAirControlState = bigfootAirControl.setAirControlState;
	self.useAltAirKeys = false;
end;

function bigfootAirControl:rollDelay(x,z)
	if self.airTime == nil then 
		self.airTime = (g_time/1000);
		return 0,0;
	elseif x ~= 0 then
		if (self.airTime + 0.2) > (g_time/1000) then--0.1
			return 0,z;
		end;
	elseif z > 0 then
		if (self.airTime + 0.1) > (g_time/1000) then--0.05
			return x,0;
		end;
	elseif z < 0 then
		if (self.airTime + 0.5) > (g_time/1000) then --0.4
			return x,0;
		end;
	end;
	return x,z;
end;

function bigfootAirControl:updateRoll(xOffset,yOffset,zOffset)
	if not self.useAltAirKeys then 
		xOffset,zOffset = self:rollDelay(xOffset,zOffset);--delay to prevent nose dive off jumps etc.
	end;
	--if self.isServer then
		local force = 0.075*self:getTotalMass(true);--0.1
		if zOffset ~= 0 then--higher force values to compensate for momentum (fwd/bwd).
			if zOffset > 0 then force = force*15;
			else force = force*10; end;
		end;
		local dirX,dirY,dirZ = localDirectionToWorld(self.components[1].node,0,1,0);
		--if zOffset > 0 and dirY < 0 then zOffset = zOffset*-1; end;
		addForce(self.components[1].node,dirX*force,dirY*force,dirZ*force,xOffset,yOffset,zOffset,true);
	--end;
end;

function bigfootAirControl:updateRollInput(leftRoll, rightRoll, forwardRoll, backwardRoll)
	if leftRoll then
		local vehicleWidth = Utils.getNoNil(self.sizeWidth, 4);
		local leftOffset = -((vehicleWidth/2)+0.2);
		self:updateRoll(leftOffset,vehicleWidth/2,0);
	elseif rightRoll then
		local vehicleWidth = Utils.getNoNil(self.sizeWidth, 4);
		local rightOffset = (vehicleWidth/2)+0.2;
		self:updateRoll(rightOffset,vehicleWidth/2,0);
	elseif forwardRoll then
		local vehicleWidth = Utils.getNoNil(self.sizeWidth, 4);
		local forwardOffset = -((vehicleWidth/2)+0.2);
		self:updateRoll(0,vehicleWidth/2,forwardOffset);
	elseif backwardRoll then
		local vehicleWidth = Utils.getNoNil(self.sizeWidth, 4);
		local backwardOffset = (vehicleWidth/2)+0.2;
		self:updateRoll(0,vehicleWidth/2,backwardOffset);
	end;
end;

--[[function bigfootAirControl:setAirControlState(left,right,fwd,bwd, noEventSend)
    --setAirControlEvent.sendEvent(self, left,right,fwd,bwd, noEventSend);
	self:updateRollInput(left,right,fwd,bwd);
end;]]

function bigfootAirControl:onUpdate(self, dt)
	if self.isServer then
		if self.spec_enterable ~= nil then
			if self.spec_enterable.isEntered then
				if not self:areAllTiresOnGround() then
					if self.useAltAirKeys then
						local key = bigfootAirControl.keys;
						if Input.isKeyPressed(key.left) then
							self:updateRollInput(true, false, false, false);
						elseif Input.isKeyPressed(key.right) then
							self:updateRollInput(false, true, false, false);
						elseif Input.isKeyPressed(key.forward) then
							self:updateRollInput(false, false, true, false);
						elseif Input.isKeyPressed(key.backward) then
							self:updateRollInput(false, false, false, true);
						end;
					else
						local spec = self.spec_drivable;
						local steerDir = spec:getDashboardSteeringAxis();--spec.axisSide;
						if steerDir >= 0.001 then
							self:updateRollInput(true, false, false, false);
							--self:setAirControlState(true, false, false, false);
						elseif steerDir <= -0.001 then
							self:updateRollInput(false, true, false, false);
							--self:setAirControlState(false, true, false, false);
						elseif spec:getDecelerationAxis() > 0 then
							self:updateRollInput(false, false, true, false);
							--self:setAirControlState(false, false, true, false);
						elseif spec:getAccelerationAxis() > 0 then
							self:updateRollInput(false, false, false, true);
							--self:setAirControlState(false, false, false, true);
						end;
					end;
				elseif self.airTime ~= nil then
					self.airTime = nil;
				end;
			end;
		end;
	end;
end;

function bigfootAirControl:onDraw(self)
	if self.useAltAirKeys then
		g_currentMission:addExtraPrintText("Air Roll Controls : Numpad Keys");
	end;
end;

function bigfootAirControl:airControl_eventToggleKeys()
	self.useAltAirKeys = not self.useAltAirKeys;
end;

function bigfootAirControl:onRegisterActionEvents(self, isActiveForInput, isActiveForInputIgnoreSelection)
	if not GS_MULTIPLAYER then
		self:clearActionEventsTable(self.bigfootAirControlEvents);
		self.bigfootAirControlEventIds = {};
		if isActiveForInputIgnoreSelection then
			local _, actionEventId = self:addActionEvent(self.bigfootAirControlEvents, 'airControl_toggleKeyCntrl', self, bigfootAirControl.airControl_eventToggleKeys, false, true, false, true, nil);
			g_inputBinding:setActionEventTextVisibility(actionEventId, false);
			g_inputBinding:setActionEventTextPriority(actionEventId, GS_PRIO_LOW);
			table.insert(self.bigfootAirControlEventIds, actionEventId);
		end;
	end;
end;

--[[setAirControlEvent = {};
setAirControlEvent_mt = Class(setAirControlEvent, Event);
InitEventClass(setAirControlEvent,"setAirControlEvent");

function setAirControlEvent:emptyNew()
    local self = Event:new(setAirControlEvent_mt);
    self.className = "setAirControlEvent";
    return self;
end;
function setAirControlEvent:new(vehicle,left,right,fwd,bwd)
    self.vehicle = vehicle;
    self.left = left;
    self.right = right;
    self.fwd = fwd;
    self.bwd = bwd;
    return self;
end;
function setAirControlEvent:readStream(streamId,connection)
    self.vehicle = NetworkUtil.readNodeObject(streamId);
    self.left = streamReadBool(streamId);
    self.right = streamReadBool(streamId);
    self.fwd = streamReadBool(streamId);
    self.bwd = streamReadBool(streamId);
    self:run(connection);
end;
function setAirControlEvent:writeStream(streamId,connection)
    NetworkUtil.writeNodeObject(streamId,self.vehicle);
    streamWriteBool(streamId,self.left);
    streamWriteBool(streamId,self.right);
    streamWriteBool(streamId,self.fwd);
    streamWriteBool(streamId,self.bwd);
end;
function setAirControlEvent:run(connection)
    self.vehicle:setAirControlState(self.left,self.right,self.fwd,self.bwd, true);
    if not connection:getIsServer() then
        g_server:broadcastEvent(setAirControlEvent:new(self.vehicle,self.left,self.right,self.fwd,self.bwd),nil,connection,self.vehicle);
    end;
end;
function setAirControlEvent.sendEvent(vehicle,left,right,fwd,bwd,noEventSend)
    if noEventSend == nil or noEventSend == false then
        if g_server ~= nil then
            g_server:broadcastEvent(setAirControlEvent:new(vehicle,left,right,fwd,bwd),nil,nil,vehicle);
        else
            g_client:getServerConnection():sendEvent(setAirControlEvent:new(vehicle,left,right,fwd,bwd));
        end;
    end;
end;]]