-- Kosiarka rotacyjna Z-173 --
-- Rockstar94 --
-- 04.11.2016 --


Z173 = {};

function Z173.prerequisitesPresent(specializations)
    return true
end;

function Z173:load(savegame)
	self.mower_scroll = Utils.loadScrollers(self.components, self.xmlFile, "vehicle.mower_scroll.mower_scroll", {}, false)
	
	self.foldingRange = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.foldingRange#index"));
	self.foldingAnimation = SpecializationUtil.callSpecializationsFunction("foldingAnimation");
	self.foldingAnimationAnim = getXMLString(self.xmlFile, "vehicle.foldingAnimation#animationName");
	self.isFold = false;
	
	self.cover = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.cover#index"));
	setVisibility((self.cover), true);
	
	self.cover_flagShader = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.cover_flagShader#index"));
	setVisibility((self.cover_flagShader), true);
	
	self.cover_act = true;
	self.cover_flagShader_act = true;
	
	self.cover_shader = {};
    local i = 0;
    while true do
        local key = string.format("vehicle.cover_shader.cover_shader(%d)", i);
        if not hasXMLProperty(self.xmlFile, key) then
            break;
        end;
        local node = Utils.indexToObject(self.components, getXMLString(self.xmlFile, key.."#index"));
        if node ~= nil then
            table.insert(self.cover_shader, {node=node});
        end;
	    i = i +1;
    end;
end;

function Z173:delete()
end;

function Z173:onDeactivateSounds()

end;

function Z173:keyEvent(unicode, sym, modifier, isDown)
end;
 
function Z173:updateTick(dt)
	if g_currentMission.player ~= nil then
		local vx, vy, vz = getWorldTranslation(g_currentMission.player.rootNode);
		local sx, sy, sz = getWorldTranslation(self.foldingRange);
		local distance = Utils.vector3Length(sx-vx, sy-vy, sz-vz);
		if distance < 2 then
			self.inRange = true; 
		else
			self.inRange = false; 
		end;
	end;
end; 
 
function Z173:update(dt)	
    if self.isClient then
        Utils.updateScrollers(self.mower_scroll, dt, self:getIsActive() and self:getIsTurnedOn())
    end
	
	if self.inRange then
		if InputBinding.hasEvent(InputBinding.Z173_folding) then
			if self:getIsTurnedOn() then
				g_currentMission:showBlinkingWarning(g_i18n:getText("Z173_folding_unavailable"), 2000);
			else
				self:foldingAnimation(not self.isFold);
			end;
		end;
		if not self:getIsTurnedOn() then
			if self.isFold then
				g_currentMission:addHelpButtonText(g_i18n:getText("Z173_unfold"), InputBinding.Z173_folding);
			else
				g_currentMission:addHelpButtonText(g_i18n:getText("Z173_fold"), InputBinding.Z173_folding);
			end;
			if self.cover_act then
				if InputBinding.hasEvent(InputBinding.Z173_cover_flagShader) then
					self.cover_flagShader_act = not self.cover_flagShader_act;
				end;
				if self.cover_flagShader_act then
					g_currentMission:addHelpButtonText(g_i18n:getText("Z173_hide_cover"), InputBinding.Z173_cover_flagShader);
				else
					g_currentMission:addHelpButtonText(g_i18n:getText("Z173_show_cover"), InputBinding.Z173_cover_flagShader);
				end;
			end;
			if not self.cover_flagShader_act then
				if InputBinding.hasEvent(InputBinding.Z173_cover) then
					self.cover_act = not self.cover_act;
				end;
				if self.cover_act then
					g_currentMission:addHelpButtonText(g_i18n:getText("Z173_hide_cover_1"), InputBinding.Z173_cover);
				else
					g_currentMission:addHelpButtonText(g_i18n:getText("Z173_show_cover_1"), InputBinding.Z173_cover);
				end;
			end;
		else
			if InputBinding.hasEvent(InputBinding.Z173_cover) or InputBinding.hasEvent(InputBinding.Z173_cover_flagShader) then
				g_currentMission:showBlinkingWarning(g_i18n:getText("Z173_cover_unavailable"), 2000);
			end;
		end;
	end;
	
	if self.cover_act then
		setVisibility((self.cover), true);
	else
		setVisibility((self.cover), false);
	end;
	
	if self.cover_flagShader_act then
		setVisibility((self.cover_flagShader), true);
	else
		setVisibility((self.cover_flagShader), false);
	end;
	
	if not self.isFold then
        self:setIsTurnedOn(false);
		SoundUtil.stopSample(self.mower.sampleMowerStop, true)
    end;
	
    if self:getIsActive() then
	    if self:getIsTurnedOn() then
	    	for _, part in pairs(self.cover_shader) do
				 if self.lastSpeed*3600 > 3 then
					setShaderParameter(part.node, "displacementSpeedFrequencyAndTexOff", 0.04, 10, 1, 0.26, false);
				end;
				 if self.lastSpeed*3600 > 6 then
					setShaderParameter(part.node, "displacementSpeedFrequencyAndTexOff", 0.05, 10, 1, 0.26, false);
				end;
				 if self.lastSpeed*3600 > 9 then
					setShaderParameter(part.node, "displacementSpeedFrequencyAndTexOff", 0.06, 10, 1, 0.26, false);
				end;
				 if self.lastSpeed*3600 > 12 then
					setShaderParameter(part.node, "displacementSpeedFrequencyAndTexOff", 0.07, 10, 1, 0.26, false);
				end;
				 if self.lastSpeed*3600 > 15 then
					setShaderParameter(part.node, "displacementSpeedFrequencyAndTexOff", 0.08, 10, 1, 0.26, false);
				end;
				if self.lastSpeed*3600 < 3 then
					setShaderParameter(part.node, "displacementSpeedFrequencyAndTexOff", 0.025, 10, 1, 0.26, false);
				end;
	    	end;		
	    else
	        for _, part in pairs(self.cover_shader) do
            	setShaderParameter(part.node, "displacementSpeedFrequencyAndTexOff", 0, 0, 0, 0, false);
  	    	end;	
        end;
	else
		for _, part in pairs(self.cover_shader) do
            setShaderParameter(part.node, "displacementSpeedFrequencyAndTexOff", 0, 0, 0, 0, false);
  	    end;	
    end;
end;

function Z173:foldingAnimation(foldingAnimation, noEventSend)
	self.isFold = foldingAnimation;
	if not foldingAnimation then
		if self.foldingAnimationAnim ~= nil and self.playAnimation ~= nil then
			self:playAnimation(self.foldingAnimationAnim, -1, nil, true);
		end;
	else
		if self.foldingAnimationAnim ~= nil and self.playAnimation ~= nil then
			self:playAnimation(self.foldingAnimationAnim, 1, nil, true);
		end;
	end;
end;

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

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

function Z173:postLoad(savegame)
    if savegame ~= nil and not savegame.resetVehicles then
		local cover_act = Utils.getNoNil(getXMLBool(savegame.xmlFile, savegame.key .. "#cover_act"), self.cover_act);
		if cover_act ~= nil then
			self.cover_act = cover_act;
		end;
		
		local cover_flagShader_act = Utils.getNoNil(getXMLBool(savegame.xmlFile, savegame.key .. "#cover_flagShader_act"), self.cover_flagShader_act);
		if cover_flagShader_act ~= nil then
			self.cover_flagShader_act = cover_flagShader_act;
		end;	

		local isFold = Utils.getNoNil(getXMLBool(savegame.xmlFile, savegame.key.."#isFold"), false);
		if isFold then
			self:foldingAnimation(true);
		end;
    end
end

function Z173:getSaveAttributesAndNodes(nodeIdent)
	if self.cover_act then
		cover_act = "true";
	else
		cover_act = "false";
	end;
	
	if self.cover_flagShader_act then
		cover_flagShader_act = "true";
	else
		cover_flagShader_act = "false";
	end;

	local attributes = 'isFold="' .. tostring(self.isFold) ..'" cover_act="' .. tostring(self.cover_act) ..'" cover_flagShader_act="' .. tostring(self.cover_flagShader_act) ..'"';
    return attributes,nodes;
end;

function Z173:draw()
	if self.isClient then
		if self:getIsActiveForInput(self.needsSelection) and not self:hasInputConflictWithSelection() then
			g_currentMission:addExtraPrintText(g_i18n:getText("Z173_switch_mode"));
			g_currentMission:addExtraPrintText(g_i18n:getText("Z173_fold_info"));
		end;
	end;
end;