--
-- playerInRangeTool
-- 
-- @author:    	Xentro (Marcus@Xentro.se)
-- @website:	www.Xentro.se
-- @history:	v1.0 - 2015-06-24 - Initial implementation
--

playerInRangeTool = {};
playerInRangeTool.modName = g_currentModName;

addModEventListener(playerInRangeTool);

function playerInRangeTool:loadMap()
	self.playerMinDistance = 2;
	self.scriptVersion = 1.0;
	
	if g_currentMission.playerInRangeTool == nil then
		g_currentMission.playerInRangeTool = self;
		g_currentMission.playerInRangeExtras = {};
	else
		if g_currentMission.playerInRangeTool.scriptVersion < self.scriptVersion then
			if g_currentMission.playerInRangeTool ~= self then
				print("playerInRangeTool - Notice: playerInRangeTool v" .. g_currentMission.playerInRangeTool.scriptVersion .. " have been replaced with a newer v" .. self.scriptVersion .. " from the mod " .. playerInRangeTool.modName);
				g_currentMission.playerInRangeTool = self;
			end;
		end;
	end;
end;

function playerInRangeTool:deleteMap()
	if g_currentMission.playerInRangeTool ~= nil and g_currentMission.playerInRangeTool == self then
		g_currentMission.playerInRangeExtras = nil;
		g_currentMission.playerInRangeTool = nil;
	end;
end;

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

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

function playerInRangeTool:update(dt)
	if g_currentMission.playerInRangeTool ~= nil and g_currentMission.playerInRangeTool == self then
		if g_currentMission.player ~= nil then
			local tableEmpty = true;
			
			if g_currentMission.player.isControlled and g_currentMission.player.isEntered then
				if g_currentMission.player.activeTool == nil then
					local px, py, pz = getWorldTranslation(g_currentMission.player.rootNode);
					
					for _, e in pairs({g_currentMission.vehicles, g_currentMission.playerInRangeExtras}) do
						for i, vehicle in pairs(e) do
							local playerInRangeTool = nil;
							
							-- only check distance on mods that are set for it for now.
							if vehicle.playerInRangeTool ~= nil then
								playerInRangeTool = vehicle.playerInRangeTool;
							end;
							
							if playerInRangeTool ~= nil then
								for locationId, v in ipairs(playerInRangeTool) do
									if v.node ~= nil then
										local x, y, z = getWorldTranslation(v.node);
										local distance = Utils.vector3Length(x - px, y - py, z - pz);
										local update = false;
										local playerDistance = Utils.getNoNil(v.playerDistance, self.playerMinDistance);
										
										if distance < playerDistance then
											if g_currentMission.player.closestTool == nil then
												update = true;
											else
												if g_currentMission.player.closestToolDistance > distance then
													update = true;
												end;
												if g_currentMission.player.closestTool == vehicle and g_currentMission.player.closestToolLocationId == locationId then
													update = true;
												end;
											end;
											
											if update then
												g_currentMission.player.closestTool = vehicle;
												g_currentMission.player.closestToolLocationId = locationId;
												g_currentMission.player.closestToolDistance = distance;
											end;
											
											tableEmpty = false;
										end;
									end;
								end;
							end;
						end;
					end;
				end;
			end;
			
			if tableEmpty then
				if g_currentMission.player.closestTool ~= nil then
					g_currentMission.player.closestTool = nil;
					g_currentMission.player.closestToolLocationId = nil;
					g_currentMission.player.closestToolDistance = nil;
				end;
			end;
		end;
	end;
end;

function playerInRangeTool:draw()
end;