--
-- Breakdown 
-- Specialization for combine - Allows a combine to breakdown ( engine failure ) and be repaired.
--
-- @author  Bayn - www.tsforum3.com
-- @date  14/07/09

-- @edit by Knagsted
-- @date  16/07/10
--

Breakdown = {};

function Breakdown.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(motorized, specializations);
end;

function Breakdown:load(xmlFile)
	
	
	------------------------------------------
	------ Keys ------------
	------------------------------------------


	self.keys = {};
	-- Generating a random number --
	self.RandomNumber = math.random(2,30);	
	--print ("nombre:" , self.RandomNumber);

	self.AuthorizeBreakdown = true;
	self.servicePrice = 0;
	self.awaitingFixingAck = false;
	self.servicePrice = math.random(200,1500);
end;

function Breakdown:delete()
end;

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

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

function Breakdown:update(dt)

	--manual fixing.
	local nearestDistance = 3.0; --max distance allowed
	local px, py, pz = getWorldTranslation(self.rotationEchellePanel.node); --self.Helice1 is one of the spereaders
	local vx, vy, vz = getWorldTranslation(getCamera());
	if vx ~= nil and  vy ~= nil and vz ~= nil then
		local distance = Utils.vector3Length(px-vx, py-vy, pz-vz);	
		if distance < nearestDistance then
			self.maintenanceStairsInRange = true; --farmer is in range
		else
			self.maintenanceStairsInRange = false; --farmer is not in range
		end;
	end;
	
	if self.maintenanceStairsInRange then
		if not self.Panels then
			g_currentMission:addExtraPrintText(string.format("Key %s: Extendt maintenance ladder", InputBinding.getKeyNamesOfDigitalAction(InputBinding.MAINTENANCELADDER)));
	        end;
	end;
	
	if InputBinding.hasEvent(InputBinding.FIXCOMBINE) then
		if self.maintenanceStairsInRange then
			self.awaitingFixingAck = true;
		end;
	end;
	
	if InputBinding.hasEvent(InputBinding.ACKNOWLEDGE) then
		if self.awaitingFixingAck then
			self.awaitingFixingAck = false;
			self.Breakdown = false;
			self.AuthorizeBreakdown = true;
			self.RandomNumber = self.RandomNumber + math.random(10,30);
			--Fixing costs money!!!
            g_currentMission.missionStats.expensesTotal = g_currentMission.missionStats.expensesTotal + self.servicePrice;
            g_currentMission.missionStats.expensesSession = g_currentMission.missionStats.expensesSession + self.servicePrice;

            g_currentMission.missionStats.money = g_currentMission.missionStats.money - self.servicePrice;
			self.servicePrice = math.random(200,1500);
		end;
	end;
	
	if InputBinding.hasEvent(InputBinding.DECLINE) then
		if self.awaitingFixingAck then
			self.awaitingFixingAck = false;
		end;
	end;

	if self.awaitingFixingAck then
		setTextColor(1, 0, 0, 1.0);
		renderText(0.25, 0.5, 0.03, string.format("Service combine at a price of: " ..self.servicePrice.. " Euro? Y/N"));
	end;
	
	if InputBinding.hasEvent(InputBinding.MAINTENANCELADDER) then
		if 	self.maintenanceStairsInRange then
		end;
	end;
	
	--renderText(0.5, 0.5, 0.020, string.format("%d ",self.RandomNumber));
	
	
	-- Manual fixing done
    
	
	if g_currentMission.missionStats.hectaresThreshedSession >= self.RandomNumber then
		self.Breakdown = true;
	end;
	
	if self.AuthorizeBreakdown and self.Breakdown then
			
			if self.isEntered then
				setTextColor(1, 0, 0, 1.0);
				renderText(0.3, 0.5, 0.04, string.format("Combine breakdown - Exit and fix it"));
			end;
	
end;
end;

function Breakdown:onLeave()
end;

function Breakdown:onEnter()
end;

function Breakdown:draw()
end;
