--Specjalizacja dla Kosiarka Osa--
--Autor: Rockstar94--
--Data: 07.06.2015--



Osa = {};

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

function Osa:load(xmlFile)
	self.pasek = {};
	
    local i = 0;
	
    while true do
        local key = string.format("vehicle.pasek.pasek(%d)", i);
        if not hasXMLProperty(xmlFile, key) then
            break;
        end;
        local node = Utils.indexToObject(self.components, getXMLString(xmlFile, key.."#index"));
        if node ~= nil then
            table.insert(self.pasek, {node=node});
        end;
	    i = i +1;
    end;
	
	local rozkladanie = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.rozkladanie#index"));
	if rozkladanie ~= nil then
		self.rozkladanie = rozkladanie;
	end;		
	
	openSoundFile = Utils.getFilename("Dzwieki/Skladanie.wav", self.baseDirectory);
	self.openSoundId = createSample("Sample");
	loadSample(self.openSoundId, openSoundFile, false);
	self.openPlaying = false;	
	
	clouseSoundFile = Utils.getFilename("Dzwieki/Skladanie.wav", self.baseDirectory);
	self.clouseSoundId = createSample("Sample");
	loadSample(self.clouseSoundId, clouseSoundFile, false);
	self.clousePlaying = false;
	
	--[[self.kosiarkaParticleSystems = {};
	local i=0;
	while true do
		local baseName = string.format("vehicle.kosiarkaParticleSystems.kosiarkaParticleSystem(%d)", i);

		local particleSystem = {};
		particleSystem.ps = {};
		local ps = Utils.loadParticleSystem(xmlFile, particleSystem.ps, baseName, self.components, false, nil, self.baseDirectory)
		if ps == nil then
			break;
		end;
		table.insert(self.kosiarkaParticleSystems, particleSystem);
		i = i+1;
	end;]]
end;

function Osa:delete()
	--[[for k, v in pairs(self.kosiarkaParticleSystems) do
		Utils.deleteParticleSystem(v.ps)
	end]]
end;

function Osa:keyEvent(unicode, sym, modifier, isDown)
 end;
 
function Osa:update(dt)	
	if self.isTurnedOn then
        self:setAnimationTime(1, 1);
		self:setAnimationTime(2, 1);
		self:setAnimationTime(4, 1);
    else
        self:setAnimationTime(1, 0);
		self:setAnimationTime(2, 0);
		self:setAnimationTime(4, 0);
    end;
	
	if self.isTurnedOn then
		for _, part in pairs(self.pasek) do
			setShaderParameter(part.node, "uvScrollSpeed", 0, 3.7, 0, 0, false);
		end;
	end;
	
	if not self.isTurnedOn then
		for _, part in pairs(self.pasek) do
			setShaderParameter(part.node, "uvScrollSpeed", 0, 0, 0, 0, false);
		end;
	end;
	
    local nearestDistance = 1.5;
    local vx, vy, vz = getWorldTranslation(g_currentMission.player.rootNode);
    local px, py, pz = getWorldTranslation(self.rozkladanie);
    local distance = Utils.vector3Length(px-vx, py-vy, pz-vz);
		if distance < nearestDistance then
			self.przy_rozkladanie = true;
		else
			self.przy_rozkladanie = false;
		end;
		
    if self.przy_rozkladanie then
		if InputBinding.hasEvent(InputBinding.rozkladanie) then
			if self.animationParts[3].clipEndTime then
				self:setAnimationTime(3, self.animationParts[3].offSet);
				self.sound1 = true;
				self.sound2 = false;
			elseif self.animationParts[3].clipStartTime  then
				self:setAnimationTime(3, self.animationParts[3].animDuration);	
				self.sound1 = false;
				self.sound2 = true;
			end;
		end;
    end;
	
	if self.przy_rozkladanie then
		if self.animationParts[3].clipStartTime then
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("rozloz")), InputBinding.rozkladanie);
		else
			g_currentMission:addHelpButtonText(string.format(g_i18n:getText("zloz")), InputBinding.rozkladanie);
		end;
	end;	
	
	if self.sound1 then
		if not self.openPlaying then
			playSample(self.openSoundId, 1, 3, 3);
			self.openPlaying = true;
		end;      
    else
		if self.openPlaying then
		   stopSample(self.openSoundId);
		   self.openPlaying = false;
		end;        
    end;	
	
	if self.sound2 then
		if not self.clousePlaying then
			playSample(self.clouseSoundId, 1, 3, 3);
			self.clousePlaying = true;
		end;       
    else
		if self.clousePlaying then
			stopSample(self.clouseSoundId);
			self.clousePlaying = false;
		end;        
    end;	
	
	if self.animationParts[3].clipStartTime then
		self.isTurnedOn = false;
	end;
	
	if self.animationParts[3].clipStartTime and self:getIsActive(false) and Input.isKeyPressed(Input.KEY_b) then
		g_currentMission:addWarning(g_i18n:getText("info1_kosiarka"), 0.07+0.022, 0.019+0.029);
	end;

	--[[if self:getIsActive() then
		if self.isTurnedOn then
			if self.movingDirection < 0 then
				if self:isLowered(true) then
					if pickedUpWindrow ~= nil then
						for k,v in ipairs(self.kosiarkaParticleSystems) do
							Utils.setEmittingState(v.ps, true);
						end;
					end;
				end;
			end;
		end;
	end;
	
	if self:getIsActive() then
		if self.isTurnedOn then
			if self.lastSpeed*3600 < 5 then
				for k,v in ipairs(self.kosiarkaParticleSystems) do
					Utils.setEmittingState(v.ps, false);
				end;
			end;
			if not self:isLowered(true) then
				for k,v in ipairs(self.kosiarkaParticleSystems) do
					Utils.setEmittingState(v.ps, false);
				end;
			end;			
		end;
	end;]]
end;

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

function Osa:validateAttacherJoint(implement, jointDesc, dt)
return true;
end;

function Osa:draw()
end;