-- Ein Beispielkommentar zur LUA
-- Zum Beispiel eine Beschreibung was die LUA tut und wann und von wem sie geschrieben wurde

attrezzi = {};

function attrezzi.prerequisitesPresent(specializations)
      return SpecializationUtil.hasSpecialization(Sprayer, specializations);
end;

function attrezzi:load(xmlFile)

	self.tubo = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.tubo#index"));
	self.piatto = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.piatto#index"));
	self.piattoattivo=true;
	
	-- Cutting Areas	
	self.cuttingAreas1 = {};
	self.cuttingAreas1.startIndex = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.cuttingAreasUpdate#startIndex1"));		
	self.cuttingAreas1.widthIndex = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.cuttingAreasUpdate#widthIndex1"));		
	self.cuttingAreas1.heightIndex = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.cuttingAreasUpdate#heightIndex1"));		
	self.cuttingAreas2 = {};
	self.cuttingAreas2.startIndex = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.cuttingAreasUpdate#startIndex2"));		
	self.cuttingAreas2.widthIndex = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.cuttingAreasUpdate#widthIndex2"));		
	self.cuttingAreas2.heightIndex = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.cuttingAreasUpdate#heightIndex2"));	
	self.cuttingAreas3 = {};
	self.cuttingAreas3.startIndex = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.cuttingAreasUpdate#startIndex1"));		
	self.cuttingAreas3.widthIndex = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.cuttingAreasUpdate#widthIndex1"));		
	self.cuttingAreas3.heightIndex = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.cuttingAreasUpdate#heightIndex1"));		
	self.cuttingAreas4 = {};
	self.cuttingAreas4.startIndex = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.cuttingAreasUpdate#startIndex2"));		
	self.cuttingAreas4.widthIndex = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.cuttingAreasUpdate#widthIndex2"));		
	self.cuttingAreas4.heightIndex = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.cuttingAreasUpdate#heightIndex2"));	

	self.CuttingAreasIsOnOwnedField = false;
	
	self.notYourFieldWaringTime = 0;
	
	--Particlesystem
	self.sprayValves = {};
	local psFile = getXMLString(xmlFile, "vehicle.sprayParticleSystem#file");
    if psFile ~= nil then
        local i=0;
        while true do
            local baseName = string.format("vehicle.sprayValves.sprayValve(%d)", i);
            local node = getXMLString(xmlFile, baseName.. "#index");
            if node == nil then
                break;
            end;
            node = Utils.indexToObject(self.components, node);
            if node ~= nil then
                local sprayValve = {};
                sprayValve.particleSystems = {};
				if self.isStatic == "true" then
					Utils.loadStaticParticleSystem(xmlFile, sprayValve.particleSystems, "vehicle.sprayParticleSystem", node, false, nil, self.baseDirectory);	
				else
					Utils.loadParticleSystem(xmlFile, sprayValve.particleSystems, "vehicle.sprayParticleSystem", node, false, nil, self.baseDirectory);
				end;
                table.insert(self.sprayValves, sprayValve);
            end;
            i = i+1;
        end;
    end;
	
	self.sprayValves1 = {};
	local psFile = getXMLString(xmlFile, "vehicle.sprayParticleSystem1#file");
    if psFile ~= nil then
        local i=0;
        while true do
            local baseName = string.format("vehicle.sprayValves1.sprayValve(%d)", i);
            local node = getXMLString(xmlFile, baseName.. "#index");
            if node == nil then
                break;
            end;
            node = Utils.indexToObject(self.components, node);
            if node ~= nil then
                local sprayValve1 = {};
                sprayValve1.particleSystems = {};
				if self.isStatic == "true" then
					Utils.loadStaticParticleSystem(xmlFile, sprayValve1.particleSystems, "vehicle.sprayParticleSystem1", node, false, nil, self.baseDirectory);	
				else
					Utils.loadParticleSystem(xmlFile, sprayValve1.particleSystems, "vehicle.sprayParticleSystem1", node, false, nil, self.baseDirectory);
				end;
                table.insert(self.sprayValves1, sprayValve1);
            end;
            i = i+1;
        end;
    end;

end;
function attrezzi:delete()
end;
function attrezzi:mouseEvent(posX, posY, isDown, isUp, button)
end;
function attrezzi:keyEvent(unicode, sym, modifier, isDown)
end;
function attrezzi:update(dt)
		  	if self:getIsActiveForInput() then
				if InputBinding.hasEvent(InputBinding.CAMBIO) then
					self.piattoattivo = not self.piattoattivo;			
				end;
			end;
			
			if self.piattoattivo then
				setVisibility(self.piatto, true);				
				setVisibility(self.tubo, false);
				setTranslation(self.piatto, 0, 0.42428, -1.96911);
				setRotation(self.piatto, 0, 0, 0);

			else
				setVisibility(self.piatto, false);
				setVisibility(self.tubo, true);
				setTranslation(self.tubo, 0, 0.42213, -1.9088);
				setRotation(self.tubo, 0, 0, 0);
			end;
			
			--Cambio Particle System
			if self.isTurnedOn and self.piattoattivo then
			if self.fillLevel > 0 then
				for k,sprayValve in pairs(self.sprayValves) do
					Utils.setEmittingState(sprayValve.particleSystems, true);
				end;
			end;
			if self.fillLevel > 0 then
				for k,sprayValve1 in pairs(self.sprayValves1) do
					Utils.setEmittingState(sprayValve1.particleSystems, false);
				end;
			end;
			end;
			if self.isTurnedOn and not self.piattoattivo then
				if self.fillLevel > 0 then
					for k,sprayValve1 in pairs(self.sprayValves1) do
						Utils.setEmittingState(sprayValve1.particleSystems, true);
					end;
				end;
				if self.fillLevel > 0 then
					for k,sprayValve in pairs(self.sprayValves) do
						Utils.setEmittingState(sprayValve.particleSystems, false);
					end;
				end;
			end;
			
			--Disattivazione
			if not self.isTurnedOn then
				if self.fillLevel > 0 then
					for k,sprayValve in pairs(self.sprayValves) do
						Utils.setEmittingState(sprayValve.particleSystems, false);
					end;
				end;
				if self.fillLevel > 0 then
					for k,sprayValve1 in pairs(self.sprayValves1) do
						Utils.setEmittingState(sprayValve1.particleSystems, false);
					end;
				end;
			end;
			

	--Cutting Areas
	if self.piattoattivo then
	local A1x1,_,A1z1 = getWorldTranslation(self.cuttingAreas1.startIndex);
	local A1x2,_,A1z2 = getWorldTranslation(self.cuttingAreas1.widthIndex);
	local A1x3,_,A1z3 = getWorldTranslation(self.cuttingAreas1.heightIndex);	
	local A2x1,_,A2z1 = getWorldTranslation(self.cuttingAreas2.startIndex);
	local A2x2,_,A2z2 = getWorldTranslation(self.cuttingAreas2.widthIndex);
	local A2x3,_,A2z3 = getWorldTranslation(self.cuttingAreas2.heightIndex);
	if g_currentMission:getIsFieldOwnedAtWorldPos(A1x1,A1z1) and g_currentMission:getIsFieldOwnedAtWorldPos(A1x2,A1z2) and g_currentMission:getIsFieldOwnedAtWorldPos(A1x3,A1z3) then
		if g_currentMission:getIsFieldOwnedAtWorldPos(A2x1,A2z1) and g_currentMission:getIsFieldOwnedAtWorldPos(A2x2,A2z2) and g_currentMission:getIsFieldOwnedAtWorldPos(A2x3,A2z3) then
			self.CuttingAreasIsOnOwnedField = true;
		else
			self.CuttingAreasIsOnOwnedField = false;
		end;
	else
		self.CuttingAreasIsOnOwnedField = false;
	end;
	
	--Piatto
	if self.isTurnedOn and math.abs(self.speedViolationTimer - self.speedViolationMaxTime) < 2 and self.CuttingAreasIsOnOwnedField then
			if self.fillLevel > 0 then
				local x, y, z = getWorldTranslation(self.cuttingAreas1.startIndex)
				local x1, y1, z1 = getWorldTranslation(self.cuttingAreas1.widthIndex)
				local x2, y2, z2 = getWorldTranslation(self.cuttingAreas1.heightIndex)
				local old1, total1 = Utils.getFruitWindrowArea(FruitUtil.FRUITTYPE_LIQUIDMANURE, x, z, x1, z1, x2, z2);
				local value1 = 1+math.floor(old1 / total1 + 0.7);
				value1 = math.min(value1, g_currentMission.maxWindrowValue);				
				Utils.updateFruitWindrowArea(FruitUtil.FRUITTYPE_LIQUIDMANURE, x, z, x1, z1, x2, z2, value1, true, false)
				Utils.updateSprayArea(x, z, x1, z1, x2, z2);

				local x2, y2, z2 = getWorldTranslation(self.cuttingAreas2.startIndex)
				local x12, y12, z12 = getWorldTranslation(self.cuttingAreas2.widthIndex)
				local x22, y22, z22 = getWorldTranslation(self.cuttingAreas2.heightIndex)
				local old2, total2 = Utils.getFruitWindrowArea(FruitUtil.FRUITTYPE_LIQUIDMANURE, x2, z2, x12, z12, x22, z22);
				local value2 = 1+math.floor(old2 / total2 + 0.7);
				value2 = math.min(value2, g_currentMission.maxWindrowValue);				
				Utils.updateFruitWindrowArea(FruitUtil.FRUITTYPE_LIQUIDMANURE, x2, z2, x12, z12, x22, z22, value2, true, false)
				Utils.updateSprayArea(x2, z2, x12, z12, x22, z22);

				local x, y, z = getWorldTranslation(self.cuttingAreas1.startIndex)
				local x1, y1, z1 = getWorldTranslation(self.cuttingAreas1.widthIndex)
				local x2, y2, z2 = getWorldTranslation(self.cuttingAreas1.heightIndex)
				local old1, total1 = Utils.getFruitWindrowArea(FruitUtil.FRUITTYPE_MANURELIQUID, x, z, x1, z1, x2, z2);
				local value1 = 1+math.floor(old1 / total1 + 0.7);
				value1 = math.min(value1, g_currentMission.maxWindrowValue);				
				Utils.updateFruitWindrowArea(FruitUtil.FRUITTYPE_MANURELIQUID, x, z, x1, z1, x2, z2, value1, true, false)
				Utils.updateSprayArea(x, z, x1, z1, x2, z2);
								
				local x2, y2, z2 = getWorldTranslation(self.cuttingAreas2.startIndex)
				local x12, y12, z12 = getWorldTranslation(self.cuttingAreas2.widthIndex)
				local x22, y22, z22 = getWorldTranslation(self.cuttingAreas2.heightIndex)
				local old2, total2 = Utils.getFruitWindrowArea(FruitUtil.FRUITTYPE_MANURELIQUID, x2, z2, x12, z12, x22, z22);
				local value2 = 1+math.floor(old2 / total2 + 0.7);
				value2 = math.min(value2, g_currentMission.maxWindrowValue);				
				Utils.updateFruitWindrowArea(FruitUtil.FRUITTYPE_MANURELIQUID, x2, z2, x12, z12, x22, z22, value2, true, false)
				Utils.updateSprayArea(x2, z2, x12, z12, x22, z22);
			
				for k,sprayValve in pairs(self.sprayValves) do
					Utils.setEmittingState(sprayValve.particleSystems, true);		
				end;
			end;
		else
            for k,sprayValve in pairs(self.sprayValves) do
                Utils.setEmittingState(sprayValve.particleSystems, false);
            end;		
		end; 
		end;
			
			
			
			--Tubo			
	if not self.piattoattivo then
	local A1x1,_,A1z1 = getWorldTranslation(self.cuttingAreas3.startIndex);
	local A1x2,_,A1z2 = getWorldTranslation(self.cuttingAreas3.widthIndex);
	local A1x3,_,A1z3 = getWorldTranslation(self.cuttingAreas3.heightIndex);	
	local A2x1,_,A2z1 = getWorldTranslation(self.cuttingAreas4.startIndex);
	local A2x2,_,A2z2 = getWorldTranslation(self.cuttingAreas4.widthIndex);
	local A2x3,_,A2z3 = getWorldTranslation(self.cuttingAreas4.heightIndex);
	if g_currentMission:getIsFieldOwnedAtWorldPos(A1x1,A1z1) and g_currentMission:getIsFieldOwnedAtWorldPos(A1x2,A1z2) and g_currentMission:getIsFieldOwnedAtWorldPos(A1x3,A1z3) then
		if g_currentMission:getIsFieldOwnedAtWorldPos(A2x1,A2z1) and g_currentMission:getIsFieldOwnedAtWorldPos(A2x2,A2z2) and g_currentMission:getIsFieldOwnedAtWorldPos(A2x3,A2z3) then
			self.CuttingAreasIsOnOwnedField = true;
		else
			self.CuttingAreasIsOnOwnedField = false;
		end;
	else
		self.CuttingAreasIsOnOwnedField = false;
	end;
	if self.isTurnedOn and math.abs(self.speedViolationTimer - self.speedViolationMaxTime) < 2 and self.CuttingAreasIsOnOwnedField then
			if self.fillLevel > 0 then
				local x, y, z = getWorldTranslation(self.cuttingAreas3.startIndex)
				local x1, y1, z1 = getWorldTranslation(self.cuttingAreas3.widthIndex)
				local x2, y2, z2 = getWorldTranslation(self.cuttingAreas3.heightIndex)
				local old1, total1 = Utils.getFruitWindrowArea(FruitUtil.FRUITTYPE_LIQUIDMANURE, x, z, x1, z1, x2, z2);
				local value1 = 1+math.floor(old1 / total1 + 0.7);
				value1 = math.min(value1, g_currentMission.maxWindrowValue);				
				Utils.updateFruitWindrowArea(FruitUtil.FRUITTYPE_LIQUIDMANURE, x, z, x1, z1, x2, z2, value1, true, false)
				Utils.updateSprayArea(x, z, x1, z1, x2, z2);

				local x2, y2, z2 = getWorldTranslation(self.cuttingAreas4.startIndex)
				local x12, y12, z12 = getWorldTranslation(self.cuttingAreas4.widthIndex)
				local x22, y22, z22 = getWorldTranslation(self.cuttingAreas4.heightIndex)
				local old2, total2 = Utils.getFruitWindrowArea(FruitUtil.FRUITTYPE_LIQUIDMANURE, x2, z2, x12, z12, x22, z22);
				local value2 = 1+math.floor(old2 / total2 + 0.7);
				value2 = math.min(value2, g_currentMission.maxWindrowValue);				
				Utils.updateFruitWindrowArea(FruitUtil.FRUITTYPE_LIQUIDMANURE, x2, z2, x12, z12, x22, z22, value2, true, false)
				Utils.updateSprayArea(x2, z2, x12, z12, x22, z22);

				local x, y, z = getWorldTranslation(self.cuttingAreas3.startIndex)
				local x1, y1, z1 = getWorldTranslation(self.cuttingAreas3.widthIndex)
				local x2, y2, z2 = getWorldTranslation(self.cuttingAreas3.heightIndex)
				local old1, total1 = Utils.getFruitWindrowArea(FruitUtil.FRUITTYPE_MANURELIQUID, x, z, x1, z1, x2, z2);
				local value1 = 1+math.floor(old1 / total1 + 0.7);
				value1 = math.min(value1, g_currentMission.maxWindrowValue);				
				Utils.updateFruitWindrowArea(FruitUtil.FRUITTYPE_MANURELIQUID, x, z, x1, z1, x2, z2, value1, true, false)
				Utils.updateSprayArea(x, z, x1, z1, x2, z2);
								
				local x2, y2, z2 = getWorldTranslation(self.cuttingAreas4.startIndex)
				local x12, y12, z12 = getWorldTranslation(self.cuttingAreas4.widthIndex)
				local x22, y22, z22 = getWorldTranslation(self.cuttingAreas4.heightIndex)
				local old2, total2 = Utils.getFruitWindrowArea(FruitUtil.FRUITTYPE_MANURELIQUID, x2, z2, x12, z12, x22, z22);
				local value2 = 1+math.floor(old2 / total2 + 0.7);
				value2 = math.min(value2, g_currentMission.maxWindrowValue);				
				Utils.updateFruitWindrowArea(FruitUtil.FRUITTYPE_MANURELIQUID, x2, z2, x12, z12, x22, z22, value2, true, false)
				Utils.updateSprayArea(x2, z2, x12, z12, x22, z22);
			
				for k,sprayValve1 in pairs(self.sprayValves1) do
					Utils.setEmittingState(sprayValve1.particleSystems, true);		
				end;
			end;
		else
            for k,sprayValve1 in pairs(self.sprayValves1) do
                Utils.setEmittingState(sprayValve1.particleSystems, false);
            end;		
		end; 
		end;
end;
function attrezzi:updateTick(dt)
end;
function attrezzi:draw()
		if self.isTurnedOn then
			if InputBinding.hasEvent(InputBinding.CAMBIO) then
			renderText(0.5, 0.5, 0.020, string.format("Spegni la botte prima di cambiare l'attrezzo"));
			end;
		end;
		
end;
