KuhnLSB1270 = {};

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

function KuhnLSB1270:load(xmlFile)

	self.moveBalesOut = KuhnLSB1270.moveBalesOut;
	self.moveBales = SpecializationUtil.callSpecializationsFunction("moveBales");
	self.setJointLimit = SpecializationUtil.callSpecializationsFunction("setJointLimit");
	self.setTransportMode = SpecializationUtil.callSpecializationsFunction("setTransportMode");
	self.allowPickingUp = KuhnLSB1270.allowPickingUp;
	
	self.pickupRotLimit = {0};

	local path = Utils.getFilename("Balecounter_hud.dds", self.baseDirectory);
    self.balecounterHud = Overlay:new("counterOverlay", path, 0.8143, 0.8315, 0.176, 0.06638);
	
	self.baleCount = 0;
	self.baleMoveLastTime = 0;
	self.lastBaleCount = 0;
	self.moveBalesOutside = false;
	self.transportMode = true;
	self.printWait = false;
	
	self.dustParticleSystems = {};
    local dustParticleSystemCount = Utils.getNoNil(getXMLInt(xmlFile, "vehicle.dustParticleSystems#count"), 0);
    for i=1, dustParticleSystemCount do
        local namei = string.format("vehicle.dustParticleSystems.part%d", i);
		local nodei = Utils.indexToObject(self.rootNode, getXMLString(xmlFile, namei .. "#index"));
        Utils.loadParticleSystem(xmlFile, self.dustParticleSystems, namei, nodei, false, nil, self.baseDirectory)
    end;

end;

function KuhnLSB1270:delete()
	if self.balecounterHud ~= nil then
		self.balecounterHud:delete();
	end;
end;

function KuhnLSB1270:readStream(streamId, connection)
	self.baleCount = streamReadInt32(streamId);
end;

function KuhnLSB1270:writeStream(streamId, connection)
	streamWriteInt32(streamId, self.baleCount);
end;

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

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

function KuhnLSB1270:moveBales(dt)
    self.baleMoveLastTime = self.time;
end;

function KuhnLSB1270:getSaveAttributesAndNodes(nodeIdent)
	local attributes = 'baleCount="'.. tostring(self.baleCount) ..'"';
	return attributes, nil;
end;

function KuhnLSB1270:loadFromAttributesAndNodes(xmlFile, key, resetVehicles)
	if not resetVehicles then
		self.baleCount = Utils.getNoNil(getXMLInt(xmlFile, key.."#baleCount"), 0);
		if self.animationParts[2].isLoading and self.animationParts[2].inputTime > self.animationParts[2].offSet then
			self:setTransportMode(false);
		end;
	end;
    return BaseMission.VEHICLE_LOAD_OK;
end;

function KuhnLSB1270:update(dt)

	if self:getIsActiveForInput() then
		if InputBinding.hasEvent(InputBinding.KUHN_LSB_1270_PICKUP) then
			if self.animationParts[1].clipEndTime then
				self:setAnimationTime(1, self.animationParts[1].offSet);
			elseif self.animationParts[1].clipStartTime then
				self:setAnimationTime(1, self.animationParts[1].animDuration);
			end;
		end;
		if InputBinding.hasEvent(InputBinding.KUHN_LSB_1270_BALE) then
			self:setTransportMode(not self.transportMode);
		end;
		if InputBinding.hasEvent(InputBinding.KUHN_LSB_1270_RESET) then
			self.baleCount = 0;
		end;
	end;
	
	if self.transportMode then
		if self.moveBalesOutside then
			self.printWait = false;
			self:setAnimationTime(2, self.animationParts[2].offSet);
		else
			self:moveBalesOut(dt);
			self.printWait = true;
		end;
	else
		self.printWait = false;
		self:setAnimationTime(2, self.animationParts[2].animDuration);
		self.moveBalesOutside = false;
	end;
	
	local newRot = Utils.getMovedLimitedValues(self.pickupRotLimit, {8}, {0}, 1, 2000, dt, not self.animationParts[1].clipEndTime);
	local jointDesc = self.componentJoints[2];
	setJointRotationLimit(jointDesc.jointIndex, 0, true, Utils.degToRad(-newRot[1]), Utils.degToRad(newRot[1]));
	self.pickupRotLimit = newRot;
			
	if not self.animationParts[2].clipEndTime then
		self.isTurnedOn = false;
	end;
	
	if self.setAnimationTime ~= nil then
		if self.isTurnedOn then
			self:setAnimationTime(3, 1);
		else
			self:setAnimationTime(3, 0);
		end;
	end;
end;

function KuhnLSB1270:allowPickingUp()
	local allow = false;
	if self.animationParts[1].currentPosition > 300 then
		allow = true;
	end;
	return allow;
end;

function KuhnLSB1270:setTransportMode(transportMode)
	TransportEvent.sendEvent(self, transportMode, noEventSend);
	self.transportMode = transportMode;
end;

function KuhnLSB1270:moveBalesOut(dt)
	local done = false;
	local drive = true;
	for i=1, table.getn(self.bales) do
		local bale = self.bales[i];
		if bale ~= nil then
			local sendTime = dt/10000;
			self:moveBale(i, sendTime, noEventSend)
		else
			drive = false;
		end;
	end;
	if table.getn(self.bales) == 0 then
		done = true;
		drive = false;
	end;
	if self.attacherVehicle ~= nil then
		local kmh = self.attacherVehicle.lastSpeed*self.attacherVehicle.speedDisplayScale*3600;
		if drive then
			if kmh < 6 then
				self.attacherVehicle.motor:setSpeedLevel(2, true);
			else
				self.attacherVehicle.motor:setSpeedLevel(0, false);
			end;
		else
			self.attacherVehicle.motor:setSpeedLevel(0, false);
		end;
	end;
	self.moveBalesOutside = done;
end;
	
function KuhnLSB1270:updateTick(dt)
	if table.getn(self.bales) > self.lastBaleCount then
		self.baleCount = self.baleCount + 1;
		self.lastBaleCount = self.lastBaleCount + 1;
		self.balerKnotCleaningTime = (self.time-50);
	elseif table.getn(self.bales) < self.lastBaleCount then
		self.lastBaleCount = self.lastBaleCount - 1;
	end;
end;

function KuhnLSB1270:draw()
	
	if self.animationParts[1].clipStartTime then
		g_currentMission:addHelpButtonText(g_i18n:getText("KuhnLSB1270_1"), InputBinding.KUHN_LSB_1270_PICKUP);
	elseif self.animationParts[1].clipEndTime then
		g_currentMission:addHelpButtonText(g_i18n:getText("KuhnLSB1270_2"), InputBinding.KUHN_LSB_1270_PICKUP);
	end;
	if self.animationParts[2].clipStartTime then
		g_currentMission:addHelpButtonText(g_i18n:getText("KuhnLSB1270_3"), InputBinding.KUHN_LSB_1270_BALE);
		g_currentMission:addExtraPrintText(g_i18n:getText("KuhnLSB1270_5"));
	elseif self.animationParts[2].clipEndTime then
		g_currentMission:addHelpButtonText(g_i18n:getText("KuhnLSB1270_4"), InputBinding.KUHN_LSB_1270_BALE);
		g_currentMission:addHelpButtonText(g_i18n:getText("KuhnLSB1270_6"), InputBinding.KUHN_LSB_1270_RESET);
		self.balecounterHud:render();
		setTextAlignment(RenderText.ALIGN_RIGHT);
		setTextBold(true);	
		setTextColor(1, 1, 1, 1);
		renderText(0.9653, 0.849, 0.032, tostring(self.baleCount));
	end;
	if self.printWait then
		g_currentMission:addWarning(g_i18n:getText("BaleWarning"), 0.040, 0.035);
	end;	
end;

function KuhnLSB1270:onAttach(attacherVehicle)
	self.attacherVehicle = attacherVehicle;
end;

function KuhnLSB1270:onDetach()
end;