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

Ewr150e_Regime = {};
Ewr150e_Regime.modDirectory = g_currentModDirectory;

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

function Ewr150e_Regime:onRegisterActionEvents(isSelected, isOnActiveVehicle)
	if isOnActiveVehicle then
		if self.Ewr150e_Regime == nil then 
			self.Ewr150e_Regime = {}
		else	
			self:clearActionEventsTable( self.Ewr150e_Regime )
		end 
		for _,actionName in pairs({"Regime_Motor"}) do
			local _, eventName = self:addActionEvent(self.Ewr150e_Regime, InputAction[actionName], self, Ewr150e_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 Ewr150e_Regime.registerEventListeners(vehicleType)
	SpecializationUtil.registerEventListener(vehicleType, "onLoad", Ewr150e_Regime)
	SpecializationUtil.registerEventListener(vehicleType, "onDelete", Ewr150e_Regime)
    SpecializationUtil.registerEventListener(vehicleType, "onReadStream", Ewr150e_Regime)
    SpecializationUtil.registerEventListener(vehicleType, "onWriteStream", Ewr150e_Regime)
	SpecializationUtil.registerEventListener(vehicleType, "onUpdate", Ewr150e_Regime)
	SpecializationUtil.registerEventListener(vehicleType, "onDraw", Ewr150e_Regime)
	SpecializationUtil.registerEventListener(vehicleType, "onRegisterActionEvents", Ewr150e_Regime)
end

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

	self.inicio = Ewr150e_Regime.inicio
	self.altoregime = Ewr150e_Regime.altoregime
	self.regimeforca = Ewr150e_Regime.regimeforca
	self.forcaregimealto = Ewr150e_Regime.forcaregimealto
	self.forcaregimebaixo = Ewr150e_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
	self.baldevisualexcavadora = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.baldevisualexcavadora#index"));
end;

function Ewr150e_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 Ewr150e_Regime:onReadStream(streamId, connection)
	local Regimealto = streamReadBool(streamId)
    self:altoregime(Regimealto, true)
	local Regimedeforca = streamReadBool(streamId)
    self:regimeforca(Regimedeforca, true)
end;

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

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

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

function Ewr150e_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]
		local tool6 = seguir.movingTools[6]
		local tool7 = seguir.movingTools[7]
		local tool8 = seguir.movingTools[8]
		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 
		or tool6.lastRotSpeed > 0.0001 or tool6.lastRotSpeed < -0.0001 
		or tool7.lastRotSpeed > 0.0001 or tool7.lastRotSpeed < -0.0001 
		or tool8.lastRotSpeed > 0.0001 or tool8.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 Ewr150e_Regime:onUpdateTick() 
end;

function Ewr150e_Regime:onDeactivate()
end;

function Ewr150e_Regime:onDraw()  
end;

function Ewr150e_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 Ewr150e_Regime:inicio()
	local ler = self.r_regimemotor
	local leer = self.spec_motorized
	self.Regimeincio = true
	if self.isClient then
	end;
end;

function Ewr150e_Regime:altoregime(Regimealto, noEventSend)
	Ewr150e_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 Ewr150e_Regime:regimeforca(Regimedeforca, noEventSend)
	Ewr150e_RegimedeforcatEvent.sendEvent(self, Regimedeforca, noEventSend)
	self.Regimedeforca = Regimedeforca		
end;

--- Event ---

Ewr150e_RegimealtoEvent = {};
Ewr150e_RegimealtoEvent_mt = Class(Ewr150e_RegimealtoEvent, Event);
InitEventClass(Ewr150e_RegimealtoEvent, "Ewr150e_RegimealtoEvent");

function Ewr150e_RegimealtoEvent:emptyNew()
    local self = Event:new(Ewr150e_RegimealtoEvent_mt);
    return self;
end;
function Ewr150e_RegimealtoEvent:new(object, Regimealto)
	local self = Ewr150e_RegimealtoEvent:emptyNew()
	self.object = object;
	self.Regimealto = Regimealto;
	return self;
end;
function Ewr150e_RegimealtoEvent:readStream(streamId, connection)
	self.object = NetworkUtil.readNodeObject(streamId);
	self.Regimealto = streamReadBool(streamId);
	self:run(connection);
end;
function Ewr150e_RegimealtoEvent:writeStream(streamId, connection)
	NetworkUtil.writeNodeObject(streamId, self.object);
	streamWriteBool(streamId, self.Regimealto);
end;
function Ewr150e_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 Ewr150e_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(Ewr150e_RegimealtoEvent:new(vehicle, Regimealto), nil, nil, vehicle);
			else
				g_client:getServerConnection():sendEvent(Ewr150e_RegimealtoEvent:new(vehicle, Regimealto));
			end;
		end;
	end;
end;

Ewr150e_RegimedeforcatEvent = {};
Ewr150e_RegimedeforcatEvent_mt = Class(Ewr150e_RegimedeforcatEvent, Event);
InitEventClass(Ewr150e_RegimedeforcatEvent, "Ewr150e_RegimedeforcatEvent");

function Ewr150e_RegimedeforcatEvent:emptyNew()
    local self = Event:new(Ewr150e_RegimedeforcatEvent_mt);
    return self;
end;
function Ewr150e_RegimedeforcatEvent:new(object, Regimedeforca)
	local self = Ewr150e_RegimedeforcatEvent:emptyNew()
	self.object = object;
	self.Regimedeforca = Regimedeforca;
	return self;
end;
function Ewr150e_RegimedeforcatEvent:readStream(streamId, connection)
	self.object = NetworkUtil.readNodeObject(streamId);
	self.Regimedeforca = streamReadBool(streamId);
	self:run(connection);
end;
function Ewr150e_RegimedeforcatEvent:writeStream(streamId, connection)
	NetworkUtil.writeNodeObject(streamId, self.object);
	streamWriteBool(streamId, self.Regimedeforca);
end;
function Ewr150e_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 Ewr150e_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(Ewr150e_RegimedeforcatEvent:new(vehicle, Regimedeforca), nil, nil, vehicle);
			else
				g_client:getServerConnection():sendEvent(Ewr150e_RegimedeforcatEvent:new(vehicle, Regimedeforca));
			end;
		end;
	end;
end;