sysmap_utils = {
	unseenChecked = {},
	unseenCache = {}
}

function sysmap_utils:GetCurrentMission()
	local missionname

	--Get current mission
	local oldState = ""
	if hv.OldState ~= nil then
		oldState = hv.OldState.Name
	end
	if mn.getMissionFilename() ~= "" and oldState ~= "GS_STATE_MAIN_MENU" then
		missionname = mn.getMissionFilename() .. ".fs2"
	else
		missionname = ca.getNextMissionFilename()
	end
	
	return missionname
end
	

function sysmap_utils:GetCampaignConfig()
	
	local filename = "sysview_" .. ba.getCurrentPlayer():getCampaignFilename() .. "_config.cfg"
	
	local config = nil
	
	local utils = require("utils")

	if cf.fileExists(filename, "data/config", true) then
		return utils.loadConfig(filename)
	else
		return nil
	end
end

function sysmap_utils:GetSystemData(config, missionname, systemname)
	local utils = require("utils")
	
	if cf.fileExists(config.File, "data/config", true) then
		systemdata = utils.loadConfig(config.File)
	else
		return nil
	end
	
	if systemname == nil then
	
		--Find a match
		if missionname ~= "" then
			for key, value in pairs(config) do
				if missionname == key then
					systemname = value
				end	
			end
		end
		
		--Or just default
		if not systemname and config.Default then
			systemname = config.Default
		end
		
	end
	
	return systemdata[systemname]
end

function sysmap_utils:GetSystemName(config, missionname)
	local utils = require("utils")
	
	if cf.fileExists(config.File, "data/config", true) then
		systemdata = utils.loadConfig(config.File)
	else
		return nil
	end
	
	--Find a match
	if missionname ~= "" then
		for key, value in pairs(config) do
			if missionname == key then
				systemname = value
			end	
		end
	end
	
	--Or just default
	if not systemname and config.Default then
		systemname = config.Default
	end
	
	return systemname
end

function sysmap_utils:ClearCache()
	self.unseenCache = {}
	self.unseenChecked = {}
end

function sysmap_utils:HasUnseen(systemname, systemdata)
	for k, v in pairs(systemdata.Elements) do
		--Careful! This could end up looping forever!
		if v.ZoomTo then
			local utils = require("utils")
			
			--This could recursively check itself forever, so limit it to 20 and warn to give an out
			if #self.unseenChecked > 20 then
				ba.warning("System Map has checked over 20 systems for new entries! Are you sure your map isn't recursive?")
			end
			
			local val = nil
			if not utils.table.contains(self.unseenChecked, v.ZoomTo) then
				table.insert(self.unseenChecked, v.ZoomTo)
				val = self:CheckNew(v.ZoomTo)
				self.unseenCache[v.ZoomTo] = val
			else
				val = self.unseenCache[v.ZoomTo]
			end
			
			if val == true then
				self:ClearCache()
				return true
			end
		else
			if v.ShowNew == true or v.ShowNewPersist == true then
				if self:GetIconSeen(systemname, v) ~= true then
					self:ClearCache()
					return true
				end
			end
		end
	end
	self:ClearCache()
	return false
end

function sysmap_utils:CheckNew(systemname)

	local config = self:GetCampaignConfig()
	
	if config == nil then
		return false
	end
	
	local mission = nil
	
	if systemname == nil then
		systemname = self:GetSystemName(config, self:GetCurrentMission())
	else
		mission = self:GetCurrentMission()
	end
	
	local systemdata = self:GetSystemData(config, mission, systemname)
	
	if systemdata == nil then
		return false
	end
	
	return self:HasUnseen(systemname, systemdata)
end
	
function sysmap_utils:LoadSaveData()
	local datasaver = require("datasaver")

	local saveLocation = "sysmap_" .. ba.getCurrentPlayer():getCampaignFilename()
	local saveData = datasaver:loadDataFromFile(saveLocation, true)
	
	if saveData == nil then
		saveData = self:CreateSave()
	end
	
	return saveData
end

function sysmap_utils:SaveData(data)
	if data == nil then
		data = self:CreateSave()
	end
	
	local saveLocation = "sysmap_" .. ba.getCurrentPlayer():getCampaignFilename()
	local datasaver = require("datasaver")
	datasaver:saveDataToFile(saveLocation, data, true)
end

function sysmap_utils:ResetData()
	self:SaveData(self:CreateSave())
end

function sysmap_utils:CreateSave()
	local t = {}
	t.Visbility = {}
	t.Maps = {}
	t.Persistent = {}
	
	return t
end

function sysmap_utils:SetIconSeen(map, icon, seen)
	local data = sysmap_utils:LoadSaveData()
	
	if data.Maps[map] == nil then
		data.Maps[map] = {}
	end
	
	if data.Persistent == nil then
		data.Persistent = {}
	end
	
	--only save a boolean
	if seen ~= true then
		seen = false
	end
	
	if icon.ShowNewPersist == true then
		data.Persistent[icon.ObjectName] = seen
	else
		data.Maps[map][icon.ObjectName] = seen
	end
	
	sysmap_utils:SaveData(data)
end

function sysmap_utils:GetIconSeen(map, icon)
	local data = sysmap_utils:LoadSaveData()
	
	if data.Maps[map] == nil then
		data.Maps[map] = {}
	end
	
	if data.Persistent == nil then
		data.Persistent = {}
	end
	
	local val
	if icon.ShowNewPersist == true then
		val = data.Persistent[icon.ObjectName]
		
	else
		val = data.Maps[map][icon.ObjectName]
	end
	
	if val == nil then
		val = false
	end
	
	return val
end

function sysmap_utils:SetIconVisibility(icon, visibility)
	local data = sysmap_utils:LoadSaveData()
	
	data.Visbility[icon] = visibility
	
	sysmap_utils:SaveData(data)
end

function sysmap_utils:GetIconVisibility(icon)
	local data = sysmap_utils:LoadSaveData()
	
	if data.Visbility[icon] ~= nil then
		return data.Visbility[icon]
	end
	
	return nil
end