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

Mecalac15Mwr_Regime = {};
Mecalac15Mwr_Regime.modDirectory = g_currentModDirectory;

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

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

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

	self.inicio = Mecalac15Mwr_Regime.inicio
	self.altoregime = Mecalac15Mwr_Regime.altoregime
	self.regimeforca = Mecalac15Mwr_Regime.regimeforca
	self.forcaregimealto = Mecalac15Mwr_Regime.forcaregimealto
	self.forcaregimebaixo = Mecalac15Mwr_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 Mecalac15Mwr_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 Mecalac15Mwr_Regime:onReadStream(streamId, connection)
	local Regimealto = streamReadBool(streamId)
    self:altoregime(Regimealto, true)
	local Regimedeforca = streamReadBool(streamId)
    self:regimeforca(Regimedeforca, true)
end;

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

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

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

function Mecalac15Mwr_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 Mecalac15Mwr_Regime:onUpdateTick() 
end;

function Mecalac15Mwr_Regime:onDeactivate()
end;

function Mecalac15Mwr_Regime:onDraw()  
end;

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

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

--- Event ---

Mecalac15Mwr_RegimealtoEvent = {};
Mecalac15Mwr_RegimealtoEvent_mt = Class(Mecalac15Mwr_RegimealtoEvent, Event);
InitEventClass(Mecalac15Mwr_RegimealtoEvent, "Mecalac15Mwr_RegimealtoEvent");

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

Mecalac15Mwr_RegimedeforcatEvent = {};
Mecalac15Mwr_RegimedeforcatEvent_mt = Class(Mecalac15Mwr_RegimedeforcatEvent, Event);
InitEventClass(Mecalac15Mwr_RegimedeforcatEvent, "Mecalac15Mwr_RegimedeforcatEvent");

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