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

ECR88D_Regime = {};
ECR88D_Regime.modDirectory = g_currentModDirectory;

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

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

function ECR88D_Regime:onLoad(vehicle)
	local ler = self.r_regimemotor
	
	self.inicio = ECR88D_Regime.inicio
	self.altoregime = ECR88D_Regime.altoregime
	self.regimeforca = ECR88D_Regime.regimeforca
	self.forcaregimealto = ECR88D_Regime.forcaregimealto
	self.forcaregimebaixo = ECR88D_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 ECR88D_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 ECR88D_Regime:onReadStream(streamId, connection)
	local Regimealto = streamReadBool(streamId)
    self:altoregime(Regimealto, true)
	local Regimedeforca = streamReadBool(streamId)
    self:regimeforca(Regimedeforca, true)
end;

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

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

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

function ECR88D_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 ECR88D_Regime:onUpdateTick() 
end;

function ECR88D_Regime:onDeactivate()
end;

function ECR88D_Regime:onDraw()  
end;

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

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

--- Event ---

ECR88D_RegimealtoEvent = {};
ECR88D_RegimealtoEvent_mt = Class(ECR88D_RegimealtoEvent, Event);
InitEventClass(ECR88D_RegimealtoEvent, "ECR88D_RegimealtoEvent");

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

ECR88D_RegimedeforcatEvent = {};
ECR88D_RegimedeforcatEvent_mt = Class(ECR88D_RegimedeforcatEvent, Event);
InitEventClass(ECR88D_RegimedeforcatEvent, "ECR88D_RegimedeforcatEvent");

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