-- Fredzaza --
-- TMP --
-- Update 17/04/2020 --

EC480E_Regime = {};
EC480E_Regime.modDirectory = g_currentModDirectory;

function EC480E_Regime.prerequisitesPresent(specializations)
      return true;
end;
function EC480E_Regime.initSpecialization()
end
function EC480E_Regime.registerOverwrittenFunctions(vehicleType)
end
function EC480E_Regime.registerFunctions(vehicleType)	
end
function EC480E_Regime.registerEvents(vehicleType)
end
function EC480E_Regime:registerActionEventsPlayer()
end
function EC480E_Regime:registerActionEventsMenu()
end

function EC480E_Regime:onRegisterActionEvents(isSelected, isOnActiveVehicle)
	if isOnActiveVehicle then
		if self.EC480E_Regime == nil then 
			self.EC480E_Regime = {}
		else	
			self:clearActionEventsTable( self.EC480E_Regime )
		end 
		for _,actionName in pairs({"Regime_Motor"}) do
			local _, eventName = self:addActionEvent(self.EC480E_Regime, InputAction[actionName], self, EC480E_Regime.actionCallback, true, true, false, true, nil);
	
			if isSelected then
				g_inputBinding.events[eventName].displayPriority = 1
			elseif  isOnActiveVehicle then
				g_inputBinding.events[eventName].displayPriority = 3
			end
		end
	end
end

function EC480E_Regime.registerEventListeners(vehicleType)
	SpecializationUtil.registerEventListener(vehicleType, "onLoad", EC480E_Regime)
	SpecializationUtil.registerEventListener(vehicleType, "onDelete", EC480E_Regime)
    SpecializationUtil.registerEventListener(vehicleType, "onReadStream", EC480E_Regime)
    SpecializationUtil.registerEventListener(vehicleType, "onWriteStream", EC480E_Regime)
	SpecializationUtil.registerEventListener(vehicleType, "onUpdate", EC480E_Regime)
	SpecializationUtil.registerEventListener(vehicleType, "onDraw", EC480E_Regime)
	SpecializationUtil.registerEventListener(vehicleType, "onRegisterActionEvents", EC480E_Regime)
end

function EC480E_Regime:onLoad(vehicle)
	local ler = self.r_regimemotor

	self.inicio = EC480E_Regime.inicio
	self.altoregime = EC480E_Regime.altoregime
	self.regimeforca = EC480E_Regime.regimeforca
	self.forcaregimealto = EC480E_Regime.forcaregimealto
	self.forcaregimebaixo = EC480E_Regime.forcaregimebaixo

	self.Regimealto= false
	self.Regimeemcima = false
	self.Regimeembaixo = true
	self.Regimedeforca = false
	self.funcaoA = false
	self.funcaoB = false
	self.forcajogada = false
	self.forcaembaixojagada = true

	self.samples = {}
    if self.isClient then
		self.samples.LigarRegime = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.sounds", "LigarRegime", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)  
		self.samples.Movimento = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.sounds", "Movimento", self.baseDirectory, self.components, 0, AudioGroup.VEHICLE, self.i3dMappings, self)
		self.samples.DesligarRegime = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.sounds", "DesligarRegime", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
		self.samples.Motor = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.sounds", "Motor", self.baseDirectory, self.components, 0, AudioGroup.VEHICLE, self.i3dMappings, self)
	end
end;

function EC480E_Regime:onDelete() 
		g_soundManager:stopSample(self.samples.Movimento)
		g_soundManager:stopSample(self.samples.DesligarRegime)
		g_soundManager:stopSample(self.samples.LigarRegime)
		g_soundManager:stopSample(self.samples.Motor)
end;

function EC480E_Regime:onReadStream(streamId, connection)
	local Regimealto = streamReadBool(streamId)
    self:altoregime(Regimealto, true)
	local Regimedeforca = streamReadBool(streamId)
    self:regimeforca(Regimedeforca, true)
end;

function EC480E_Regime:onWriteStream(streamId, connection)
	streamWriteBool(streamId, self.Regimealto)
	streamWriteBool(streamId, self.Regimedeforca)
end;

function EC480E_Regime:mouseEvent(posX, posY, isDown, isUp, button)
end;

function EC480E_Regime:keyEvent(unicode, sym, modifier, isDown)
end;

function EC480E_Regime:onUpdate(dt, vehicle)
	local ler = self.r_regimemotor
	local leer = self.spec_motorized
	local seguir = self.spec_cylindered
	
	for i=1, table.getn(seguir.movingTools) do
		local tool1 = seguir.movingTools[1]
		local tool2 = seguir.movingTools[2]
		local tool3 = seguir.movingTools[3]
		local tool4 = seguir.movingTools[4]
		if tool1.lastRotSpeed > 0.0001 or tool1.lastRotSpeed < -0.0001 
		or tool2.lastRotSpeed > 0.0001 or tool2.lastRotSpeed < -0.0001 
		or tool3.lastRotSpeed > 0.0001 or tool3.lastRotSpeed < -0.0001 
		or tool4.lastRotSpeed > 0.0001 or tool4.lastRotSpeed < -0.0001
		then
			self.Regimedeforca = true
		else
			self.Regimedeforca = false
		end;
	end;
	if self.Regimedeforca then		
		self:regimeforca(true)
	else
		self:regimeforca(false)
	end;	
	if self.Regimealto then
		if self.Regimedeforca then		
			if not self.forcajogada then 
				self.forcajogada = true
				self.forcaembaixojagada = false
			end;
		else	
			if not self.forcaembaixojagada then 
				self.forcajogada = false
				self.forcaembaixojagada = true
			end;
		end;	
	end;		
	if leer.isMotorStarted then	
		if not self.Regimeincio then
			self:inicio()
		end;		
		if self.Regimealto then
				if self.Regimedeforca then
					if not g_soundManager:getIsSamplePlaying(self.samples.DesligarRegime, 1.5*dt) and not self.funcaoA then
						g_soundManager:playSample(self.samples.Motor)
						g_soundManager:playSample(self.samples.Movimento)
						self.funcaoA = true
						self.funcaoB = false
					end;
				else
					if not g_soundManager:getIsSamplePlaying(self.samples.DesligarRegime, 1.5*dt) and not self.funcaoB then
						g_soundManager:playSample(self.samples.Movimento)
						g_soundManager:stopSample(self.samples.Motor)
						self.funcaoA = false
						self.funcaoB = true
					end;
				end;
		else
			g_soundManager:stopSample(self.samples.Movimento)
			g_soundManager:stopSample(self.samples.Motor)
		end;
	else
		g_soundManager:stopSample(self.samples.Movimento)
		g_soundManager:stopSample(self.samples.DesligarRegime)
		g_soundManager:stopSample(self.samples.LigarRegime)
		g_soundManager:stopSample(self.samples.Motor)
		
		self.Regimealto = false
		self.Regimeemcima = false
		self.Regimeembaixo = true
		self.Regimeincio = false
	end;
end;

function EC480E_Regime:onUpdateTick() 
end;

function EC480E_Regime:onDeactivate()
end;

function EC480E_Regime:onDraw()  
end;

function EC480E_Regime:actionCallback(actionName, keyStatus, arg4, arg5, arg6)
	if keyStatus > 0 then
		if actionName == 'Regime_Motor' then
			if self.Regimealto then
				self:altoregime(false)
			else 
				self:altoregime(true)
			end
		end
	end
end

function EC480E_Regime:inicio()
	local ler = self.r_regimemotor
	local leer = self.spec_motorized
	self.Regimeincio = true
	if self.isClient then
	end;
end;

function EC480E_Regime:altoregime(Regimealto, noEventSend)
	EC480E_RegimealtoEvent.sendEvent(self, Regimealto, noEventSend)
	local leer = self.spec_motorized
	self.Regimealto = Regimealto
	if self.Regimeincio then
		if self.Regimealto and not self.Regimeemcima then
			self.Regimeemcima = true
			self.Regimeembaixo = false
			g_soundManager:playSample(self.samples.LigarRegime, 0, leer.samples.motorStart)
			g_soundManager:playSample(self.samples.Movimento, 0, self.samples.LigarRegime)
			g_soundManager:stopSample(self.samples.DesligarRegime)
			g_soundManager.stopSample(self.samples.Motor);
		end;	
		if not self.Regimealto and not self.Regimeembaixo then
			self.Regimeembaixo = true				
			self.Regimeemcima = false
			g_soundManager:playSample(self.samples.DesligarRegime)
			g_soundManager:stopSample(self.samples.LigarRegime)
			g_soundManager:stopSample(self.samples.Movimento)
		end;
	end;			
end;

function EC480E_Regime:regimeforca(Regimedeforca, noEventSend)
	EC480E_RegimedeforcatEvent.sendEvent(self, Regimedeforca, noEventSend)
	self.Regimedeforca = Regimedeforca		
end;

--- Event ---

EC480E_RegimealtoEvent = {};
EC480E_RegimealtoEvent_mt = Class(EC480E_RegimealtoEvent, Event);
InitEventClass(EC480E_RegimealtoEvent, "EC480E_RegimealtoEvent");

function EC480E_RegimealtoEvent:emptyNew()
    local self = Event:new(EC480E_RegimealtoEvent_mt);
    return self;
end;
function EC480E_RegimealtoEvent:new(object, Regimealto)
	local self = EC480E_RegimealtoEvent:emptyNew()
	self.object = object;
	self.Regimealto = Regimealto;
	return self;
end;
function EC480E_RegimealtoEvent:readStream(streamId, connection)
	self.object = NetworkUtil.readNodeObject(streamId);
	self.Regimealto = streamReadBool(streamId);
	self:run(connection);
end;
function EC480E_RegimealtoEvent:writeStream(streamId, connection)
	NetworkUtil.writeNodeObject(streamId, self.object);
	streamWriteBool(streamId, self.Regimealto);
end;
function EC480E_RegimealtoEvent:run(connection)
	if not connection:getIsServer() then
		g_server:broadcastEvent(self, false, connection, self.object);
	end;
	if self.object ~= nil then
		self.object:altoregime(self.Regimealto, true);
	end;
end;
function EC480E_RegimealtoEvent.sendEvent(vehicle, Regimealto, noEventSend)
	if Regimealto ~= vehicle.Regimealto then
		if noEventSend == nil or noEventSend == false then
			if g_server ~= nil then
				g_server:broadcastEvent(EC480E_RegimealtoEvent:new(vehicle, Regimealto), nil, nil, vehicle);
			else
				g_client:getServerConnection():sendEvent(EC480E_RegimealtoEvent:new(vehicle, Regimealto));
			end;
		end;
	end;
end;

EC480E_RegimedeforcatEvent = {};
EC480E_RegimedeforcatEvent_mt = Class(EC480E_RegimedeforcatEvent, Event);
InitEventClass(EC480E_RegimedeforcatEvent, "EC480E_RegimedeforcatEvent");

function EC480E_RegimedeforcatEvent:emptyNew()
    local self = Event:new(EC480E_RegimedeforcatEvent_mt);
    return self;
end;
function EC480E_RegimedeforcatEvent:new(object, Regimedeforca)
	local self = EC480E_RegimedeforcatEvent:emptyNew()
	self.object = object;
	self.Regimedeforca = Regimedeforca;
	return self;
end;
function EC480E_RegimedeforcatEvent:readStream(streamId, connection)
	self.object = NetworkUtil.readNodeObject(streamId);
	self.Regimedeforca = streamReadBool(streamId);
	self:run(connection);
end;
function EC480E_RegimedeforcatEvent:writeStream(streamId, connection)
	NetworkUtil.writeNodeObject(streamId, self.object);
	streamWriteBool(streamId, self.Regimedeforca);
end;
function EC480E_RegimedeforcatEvent:run(connection)
	if not connection:getIsServer() then
		g_server:broadcastEvent(self, false, connection, self.object);
	end;
	if self.object ~= nil then
		self.object:regimeforca(self.Regimedeforca, true);
	end;
end;
function EC480E_RegimedeforcatEvent.sendEvent(vehicle, Regimedeforca, noEventSend)
	if Regimedeforca ~= vehicle.Regimedeforca then
		if noEventSend == nil or noEventSend == false then
			if g_server ~= nil then
				g_server:broadcastEvent(EC480E_RegimedeforcatEvent:new(vehicle, Regimedeforca), nil, nil, vehicle);
			else
				g_client:getServerConnection():sendEvent(EC480E_RegimedeforcatEvent:new(vehicle, Regimedeforca));
			end;
		end;
	end;
end;