--
-- FS19 - Towing Chain v1.1
-- @author:    	Stefan Geiger, kenny456 - FS19 conversion (kenny456@seznam.cz)
-- @history:	v1.1 - 2019-01-11 - converted to FS19
--								  - added manual attaching function
--								  - added function to turn on / off pulled vehicle's throttle
--
Sounds = {};


Sounds = {};

Sounds.modDir = g_currentModDirectory;

function Sounds.prerequisitesPresent(specializations)
    return true; 
end;

function Sounds.registerEventListeners(vehicleType)	
	SpecializationUtil.registerEventListener(vehicleType, "onLoad", Sounds)
	SpecializationUtil.registerEventListener(vehicleType, "onDelete", Sounds)
	SpecializationUtil.registerEventListener(vehicleType, "onUpdate", Sounds)
end

function Sounds:onLoad(vehicle)
	if self.isClient then
	    self.sampleOpenDoorL = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.K700A.sounds", "openDoorL", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
	    self.sampleCloseDoorL = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.K700A.sounds", "closeDoorL", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
	    self.sampleOpenDoorP = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.K700A.sounds", "openDoorP", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
	    self.sampleCloseDoorP = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.K700A.sounds", "closeDoorP", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
		
	    self.sampleOpenWindowL = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.K700A.sounds", "openWindowL", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
	    self.sampleCloseWindowL = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.K700A.sounds", "closeWindowL", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
	    self.sampleOpenWindowP = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.K700A.sounds", "openWindowP", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
	    self.sampleCloseWindowP = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.K700A.sounds", "closeWindowP", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
	end
end


function Sounds:onUpdate(dt)
    local spec = self.spec_animatedVehicle

    if self:getIsActive() and self.isClient then
	    if self:getIsAnimationPlaying("LDver") then
		    if spec.animations["LDver"].currentSpeed > 0 and spec.animations["LDver"].currentTime < 70 then
			    if not g_soundManager:playSample(self.sampleOpenDoorL,1.5*dt) then
				    g_soundManager:playSample(self.sampleOpenDoorL,1.5*dt);
			    end;
		    elseif spec.animations["LDver"].currentSpeed < 0 and spec.animations["LDver"].currentTime > 1430 then
			    if not g_soundManager:playSample(self.sampleCloseDoorL,1.5*dt) then
				    g_soundManager:playSample(self.sampleCloseDoorL,1.5*dt);
			    end;
		    end;
		end;
		
	    if self:getIsAnimationPlaying("PDver") then
		    if spec.animations["PDver"].currentSpeed > 0 and spec.animations["PDver"].currentTime < 70 then
			    if not g_soundManager:playSample(self.sampleOpenDoorP,1.5*dt) then
				    g_soundManager:playSample(self.sampleOpenDoorP,1.5*dt);
			    end;
		    elseif spec.animations["PDver"].currentSpeed < 0 and spec.animations["PDver"].currentTime > 1430 then
			    if not g_soundManager:playSample(self.sampleCloseDoorP,1.5*dt) then
				    g_soundManager:playSample(self.sampleCloseDoorP,1.5*dt);
			    end;
		    end;
		end;
		
	    -- if self:getIsAnimationPlaying("LOkno") then
		    -- if spec.animations["LOkno"].currentSpeed > 0 and spec.animations["LOkno"].currentTime < 70 then
			    -- if not g_soundManager:playSample(self.sampleOpenWindowL,1.5*dt) then
				    -- g_soundManager:playSample(self.sampleOpenWindowL,1.5*dt);
			    -- end;
		    -- elseif spec.animations["LOkno"].currentSpeed < 0 and spec.animations["LOkno"].currentTime > 2630 then
			    -- if not g_soundManager:playSample(self.sampleCloseWindowL,1.5*dt) then
				    -- g_soundManager:playSample(self.sampleCloseWindowL,1.5*dt);
			    -- end;
		    -- end;
		-- end;
	    if self:getIsAnimationPlaying("LOkno") then
		    if spec.animations["LOkno"].currentSpeed > 0 and spec.animations["LOkno"].currentTime < 70 then
			    if not g_soundManager:playSample(self.sampleOpenWindowP,1.5*dt) then
				    g_soundManager:playSample(self.sampleOpenWindowP,1.5*dt);
			    end;
		    elseif spec.animations["LOkno"].currentSpeed < 0 and spec.animations["LOkno"].currentTime > 2730 then
			    if not g_soundManager:playSample(self.sampleCloseWindowP,1.5*dt) then
				    g_soundManager:playSample(self.sampleCloseWindowP,1.5*dt);
			    end;
		    end;
		end;
	    if self:getIsAnimationPlaying("POkno") then
		    if spec.animations["POkno"].currentSpeed > 0 and spec.animations["POkno"].currentTime < 70 then
			    if not g_soundManager:playSample(self.sampleOpenWindowP,1.5*dt) then
				    g_soundManager:playSample(self.sampleOpenWindowP,1.5*dt);
			    end;
		    elseif spec.animations["POkno"].currentSpeed < 0 and spec.animations["POkno"].currentTime > 2730 then
			    if not g_soundManager:playSample(self.sampleCloseWindowP,1.5*dt) then
				    g_soundManager:playSample(self.sampleCloseWindowP,1.5*dt);
			    end;
		    end;
		end;
	end;
end
function Sounds:onUpdateTick(dt)
end
function Sounds:onDelete()
    if self.isClient then
		g_soundManager.deleteSample(self.sampleOpenDoorL);
		g_soundManager.deleteSample(self.sampleCloseDoorL);
		g_soundManager.deleteSample(self.sampleOpenDoorP);
		g_soundManager.deleteSample(self.sampleCloseDoorP);
		
		g_soundManager.deleteSample(self.sampleOpenWindowL);
		g_soundManager.deleteSample(self.sampleCloseWindowL);
		g_soundManager.deleteSample(self.sampleOpenWindowP);
		g_soundManager.deleteSample(self.sampleCloseWindowP);
    end
end