--
-- Autor: Siwus
-- Specialization for sprayer
--


Amazone_ZGB = {};

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


function Amazone_ZGB:load(xmlFile)
	local numCuttingAreasBow = Utils.getNoNil(getXMLInt(xmlFile, "vehicle.cuttingAreasBow#count"), 0);
	self.cuttingAreasBow = {};
    for i=1, numCuttingAreasBow do
        self.cuttingAreasBow[i] = {};
        local areanamei = string.format("vehicle.cuttingAreasBow.cuttingArea%d", i);
        self.cuttingAreasBow[i].start = Utils.indexToObject(self.components, getXMLString(xmlFile, areanamei .. "#startIndex"));
        self.cuttingAreasBow[i].width = Utils.indexToObject(self.components, getXMLString(xmlFile, areanamei .. "#widthIndex"));
        self.cuttingAreasBow[i].height = Utils.indexToObject(self.components, getXMLString(xmlFile, areanamei .. "#heightIndex"));
	end;

	local rotationPartNodeLeft = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, "vehicle.rotationPartLeft#index"));
    if rotationPartNodeLeft ~= nil then
        self.rotationPartLeft = {};
        self.rotationPartLeft.node = rotationPartNodeLeft;

        local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.rotationPartLeft#minRot"));
        self.rotationPartLeft.minRot = {};
        self.rotationPartLeft.minRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotationPartLeft.minRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotationPartLeft.minRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

        x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, "vehicle.rotationPartLeft#maxRot"));
        self.rotationPartLeft.maxRot = {};
        self.rotationPartLeft.maxRot[1] = Utils.degToRad(Utils.getNoNil(x, 0));
        self.rotationPartLeft.maxRot[2] = Utils.degToRad(Utils.getNoNil(y, 0));
        self.rotationPartLeft.maxRot[3] = Utils.degToRad(Utils.getNoNil(z, 0));

        self.rotationPartLeft.rotTime = Utils.getNoNil(getXMLString(xmlFile, "vehicle.rotationPartLeft#rotTime"), 2)*1000;
        self.rotationPartLeft.touchRotLimit = Utils.degToRad(Utils.getNoNil(getXMLString(xmlFile, "vehicle.rotationPartLeft#touchRotLimit"), 10));
    end;
	self.rotationMaxLeft = false;

	self.szerokosc = 40;
	self.literPerHa = 300;
	self.wapno = false;
end;


function Amazone_ZGB:update(dt)

	if self.attacherVehicle then
		if self.isTurnedOn then
			if self.limiter then
				if self.cuttingAreasBow[2] ~= nil then
					local x,y,z = getWorldTranslation(self.cuttingAreasBow[2].start);
					local x1,y1,z1 = getWorldTranslation(self.cuttingAreasBow[2].width);
					local x2,y2,z2 = getWorldTranslation(self.cuttingAreasBow[2].height);	
					Utils.updateSprayArea(x, z, x1, z1, x2, z2);
					self.szerokosc = 30;
--					self.literPerHa = 200;					
				end;
			elseif self.wapno then			
				if self.cuttingAreasBow[3] ~= nil then
					local x,y,z = getWorldTranslation(self.cuttingAreasBow[3].start);
					local x1,y1,z1 = getWorldTranslation(self.cuttingAreasBow[3].width);
					local x2,y2,z2 = getWorldTranslation(self.cuttingAreasBow[3].height);		
					Utils.updateSprayArea(x, z, x1, z1, x2, z2);
					self.szerokosc = 32;
					self.literPerHa = 1000;					
				end;
			else
				if self.cuttingAreasBow[1] ~= nil then
					local x,y,z = getWorldTranslation(self.cuttingAreasBow[1].start);
					local x1,y1,z1 = getWorldTranslation(self.cuttingAreasBow[1].width);
					local x2,y2,z2 = getWorldTranslation(self.cuttingAreasBow[1].height);		
					Utils.updateSprayArea(x, z, x1, z1, x2, z2);
					self.szerokosc = 40;
--					self.literPerHa = 200;					
				end;			
			end;
			local literPercentage = self.literPerHa / 350;
			local widthPercentage = self.szerokosc/42; -- x/42
			local speedPercentage = math.max(1, ((self.lastSpeed * 3600) / 15));			
			self.sprayLitersPerSecond[self.currentFillType] = 6 * literPercentage * widthPercentage * speedPercentage;
		end;
	end;	
    if self.rotationPartLeft ~= nil then
        local x, y, z = getRotation(self.rotationPartLeft.node);
        local rot = {x,y,z};
        local newRot = Utils.getMovedLimitedValues(rot, self.rotationPartLeft.maxRot, self.rotationPartLeft.minRot, 3, self.rotationPartLeft.rotTime, dt, not self.rotationMaxLeft);
        setRotation(self.rotationPartLeft.node, unpack(newRot));
    end;	
end;

function Amazone_ZGB:draw()
	if not self.limiter and not self.wapno then
		g_currentMission:addExtraPrintText(string.format("Wlacz wysiew graniczny: L"));
	elseif not self.wapno then
		g_currentMission:addExtraPrintText(string.format("Wylacz wysiew graniczny: L"));
	end;
	
	if self.wapno then
		g_currentMission:addExtraPrintText(string.format("Wysiew nawozu: K"));
	else
		g_currentMission:addExtraPrintText(string.format("Wysiew wapna granulowanego: K"));
	end;
	
	g_currentMission:addExtraPrintText(string.format("Dawka: %0.1f l/ha;   Num 5/8",self.literPerHa));
end;

function Amazone_ZGB:delete()
end;

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

function Amazone_ZGB:keyEvent(unicode, sym, modifier, isDown)
	if isDown and sym == Input.KEY_KP_9 then
		self.literPerHa = self.literPerHa + 10;
	end;
	if isDown and sym == Input.KEY_KP_7 then
		self.literPerHa = self.literPerHa - 10;
	end;
	if isDown and sym == Input.KEY_l then
		self.limiter = not self.limiter;	
	end;
	if isDown and sym == Input.KEY_k then
		self.wapno = not self.wapno;	
		self.rotationMaxLeft = not self.rotationMaxLeft;	
	end;
end;