--
-- IndoorSound
-- Specialization for IndoorSound
--
-- @author  	Manuel Leithner (SFM-Modding), PES 4ever (v1.1)
-- @version 	v1.0
-- @date  		02/11/11
-- @history:	v1.0 - Initial version
-- 				v1.1 - Outdoor sound if window(s) open
--
-- Information (v1.1) - Windows are closed if the current animation time is zero.
--
-- free for noncommerical-usage
--

IndoorSound = {};

function IndoorSound.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(Steerable, specializations);
end;

function IndoorSound:load(xmlFile)

	self.indoorSounds = {};
	self.indoorSounds.sounds = {};
	self.indoorSounds.customSounds = {};
	i = 0;
	while true do 
		local key = string.format("vehicle.indoorSounds.sound(%d)", i);
		if not hasXMLProperty(xmlFile, key .. "#id") then
			break;
		end;
		
		local id = getXMLString(xmlFile, key .. "#id");
		
		local sound = {};
		sound.id = self[id];
		sound.name = id;
		if sound.id == nil then
			print("IndoorSounds: Sound '" .. id .. "' does not exist");
			break;
		end;		
		--sound.outdoor = getSampleVolume(sound.id);
		--print("sound.outdoor="..sound.outdoor);
		sound.outdoor = getXMLFloat(xmlFile, key .. "#outDoorVolume" );
		sound.indoorFactor = Utils.getNoNil(getXMLFloat(xmlFile, key .. "#percent"), 0.5);
		sound.indoor = sound.outdoor * sound.indoorFactor;
		
		local volumeId = getXMLString(xmlFile, key .. "#volume");
		if volumeId ~= nil then
			sound.volumeId = volumeId;
		end;
		
		local customSound = getXMLString(xmlFile, key .. "#customSound");
		if customSound ~= nil and customSound ~= "" then
			customSound = Utils.getFilename(customSound, self.baseDirectory);
			sound.customSound = createSample("customSound"..i);
			sound.customSoundVolume = Utils.getNoNil(getXMLFloat(xmlFile, key .. "#customVolume"), 1);
			sound.customSoundLoops = Utils.getNoNil(getXMLFloat(xmlFile, key .. "#customLoop"), 1);
			loadSample(sound.customSound, customSound, false);
			table.insert(self.indoorSounds.customSounds, sound);
		else
			table.insert(self.indoorSounds.sounds, sound);
		end;
		
		i = i + 1;
	end;
	
	self.indoorSounds.indoorCamIndex = Utils.getNoNil(getXMLInt(xmlFile, "vehicle.indoorSounds#camIndex"), 2);	
	self.indoorSounds.lastCamIndex = 0;
	self.lastIsOpen = false;
end;

function IndoorSound:delete()
	for _, sound in pairs(self.indoorSounds.customSounds) do
		if self.indoorSounds.indoorCamIndex == self.indoorSounds.lastCamIndex then
			delete(sound.id);	
		else
			delete(sound.customSound);	
		end;
	end;
end;

function IndoorSound:readStream(streamId, connection)
end;

function IndoorSound:writeStream(streamId, connection)
end;

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

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

function IndoorSound:update(dt)
end;

function IndoorSound:updateTick(dt)
	if self:getIsActiveForInput(false) then
		local sound = self.indoorSounds;
		local isOpen = false;
		
		if self.interactiveObjects ~= nil and self.camIndex == sound.indoorCamIndex then
			for _, interactiveObject in pairs(self.interactiveObjects) do
				if interactiveObject.animation ~= nil then
					if self.animations ~= nil and self.animations[interactiveObject.animation] ~= nil then
						if self.getAnimationTime ~= nil then
							if self:getAnimationTime(interactiveObject.animation) > 0 then
								isOpen = true;
								break;
							end;
						end;
					else
						if interactiveObject.animation.animCharSet ~= nil then
							if math.max(getAnimTrackTime(interactiveObject.animation.animCharSet, 0), 0) > 0 then
								isOpen = true; -- at least one window open
								break;
							end;
						end;
					end;
				end;
			end;
		end;
		
		if sound.lastCamIndex ~= self.camIndex or (isOpen ~= self.lastIsOpen and self.camIndex == sound.indoorCamIndex) then
			if self.camIndex == sound.indoorCamIndex and not isOpen then
				for _, sound in pairs(sound.customSounds) do
					stopSample(sound.id);
					self[sound.name] = sound.customSound;	
					playSample(sound.customSound, sound.customSoundLoops, sound.customSoundVolume, 0);
				end;
				for _, sound in pairs(sound.sounds) do
					setSampleVolume(sound.id, sound.indoor);
					if sound.volumeId ~= nil then
						self[sound.volumeId] = sound.indoor;
					end;
				end;
				sound.lastCamIndex = self.camIndex;
			else
				for _, sound in pairs(sound.customSounds) do
					stopSample(sound.customSound);
					self[sound.name] = sound.id;	
					playSample(sound.id, sound.customSoundLoops, sound.customSoundVolume, 0);
				end;
				for _, sound in pairs(sound.sounds) do
					setSampleVolume(sound.id, sound.outdoor);	
					if sound.volumeId ~= nil then
						self[sound.volumeId] = sound.outdoor;
					end;
				end;
			end;
			
			if isOpen ~= self.lastIsOpen and self.camIndex == sound.indoorCamIndex then
				self.lastIsOpen = isOpen;
			end;

			if sound.lastCamIndex ~= self.camIndex then
				sound.lastCamIndex = self.camIndex;
			end;
		end;
	end;
end;

function IndoorSound:draw()	
end;