--
-- HoodOpener
--
-- written by fruktor, visit: www.eifok-team.de
--

HoodOpener = {};

HoodOpener.modDir = g_currentModDirectory;

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

function HoodOpener:load(xmlFile)

	self.playerCallbackHood = SpecializationUtil.callSpecializationsFunction("playerCallbackHood"); 
	
	self.hoodO = {};
	
	local trigger = Utils.indexToObject( self.components, getXMLString(xmlFile, string.format("vehicle.hoodOpener#trigger")) );
	if trigger == nil then
		print("[Error::HoodOpener] node is OK, but trigger could not be located. Check line "..tostring(i+1).." in your vehicle.xml");
	end;
	
	local icIndex = getXMLInt(xmlFile, string.format("vehicle.hoodOpener#icIndex", i));
	
	self.hoodO.icIndex = icIndex;
	self.hoodO.trigger = trigger;
	self.hoodO.plIR = false;
		
	addTrigger( trigger, "playerCallbackHood", self );				
	
end;

function HoodOpener:delete()
	if self.hoodO.trigger ~= nil then
		removeTrigger(self.hoodO.trigger);
	end;
end;

function HoodOpener:readStream(streamId, connection)
end;

function HoodOpener:writeStream(streamId, connection)
end;

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

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

function HoodOpener:update(dt)
	
	if self.hoodO.plIR then
		g_currentMission:addHelpButtonText( g_i18n:getText("SET_HOODOPENER"), InputBinding.SET_HOODOPENER );	
		setTextColor(1.0, 1.0, 1.0, 1.0);
		setTextAlignment(RenderText.ALIGN_LEFT); 				
		renderText( 0.5, 0.06, 0.02, g_i18n:getText("SET_HOODOPENER") );
		if InputBinding.hasEvent(InputBinding.SET_HOODOPENER)then
			local obj = self.interactiveObjects[self.hoodO.icIndex];
			if obj ~= nil then
				--self.setHood(not obj.isOpen);
				self:doActionOnObject(self.hoodO.icIndex);
			end;
			
			--self.vehicle:playAnimation(self.animation, dir, Utils.clamp(self.vehicle:getAnimationTime(self.animation), 0, 1), true);
		end
	end;
end;

function HoodOpener:onLeave()
end;

function HoodOpener:draw()
end;

function HoodOpener:playerCallbackHood(triggerId, otherId, onEnter, onLeave, onStay)
	
	if onEnter and g_currentMission.controlPlayer and g_currentMission.player ~= nil and otherId == g_currentMission.player.rootNode then
		self.hoodO.plIR = true;
	elseif onLeave and g_currentMission.player ~= nil and otherId == g_currentMission.player.rootNode then
		self.hoodO.plIR = false;
	end;
	
end;

