scanreader_utils = {}

function scanreader_utils:unlockScan(scan)
	local datasaver = require("datasaver")
	
	local campaign = ba.getCurrentPlayer():getCampaignFilename()
	
	local scans = datasaver:loadDataFromFile("scanUnlocks", true) or {}
    if not scans[campaign] then
		scans[campaign] = {}
	end
	
	local exists = false
	for _, v in ipairs(scans[campaign]) do
		if v.name == scan.name then
			exists = true
			break
		end
	end
	
	if exists == false then
		table.insert(scans[campaign], scan)
	else
		ba.print("Scan already exists, skipping!\n")
	end
	
	datasaver:saveDataToFile("scanUnlocks", scans, true)
end

function scanreader_utils:getScans()
	local datasaver = require("datasaver")
	
	local campaign = ba.getCurrentPlayer():getCampaignFilename()
	
	local scans = datasaver:loadDataFromFile("scanUnlocks", true) or {}
	
	if not scans[campaign] then
		scans[campaign] = {}
	end
	
	return scans[campaign]
end

function scanreader_utils:setSeen(name)
	local datasaver = require("datasaver")
	
	local campaign = ba.getCurrentPlayer():getCampaignFilename()
	
	local scans = datasaver:loadDataFromFile("scanUnlocks", true) or {}
    if not scans[campaign] then
		scans[campaign] = {}
	end
	
	for _, v in ipairs(scans[campaign]) do
		if v.name == name then
			v.seen = true
			datasaver:saveDataToFile("scanUnlocks", scans, true)
			break
		end
	end
end

function scanreader_utils:ResetData()
	local datasaver = require("datasaver")
	
	local campaign = ba.getCurrentPlayer():getCampaignFilename()
	
	local scans = datasaver:loadDataFromFile("scanUnlocks", true) or {}
	
	scans[campaign] = {}
	
	datasaver:saveDataToFile("scanUnlocks", scans, true)
end