--- Scan Reader Scan
--- @class scan_reader_scan
--- @field Name string The name of the scan to display
--- @field Seen boolean Whether the scan has been seen
--- @field Text string The text content of the scan
--- @field Xstr number The xstr id of the scan name
--- @field Audio string The audio filename associated with the scan
--- @field DisplayName string? The name of the scan to display after using the xstr to decode
--- @field Key string? The key to the element of this scan in the scan reader ui
--- @field Index number? The index of the scan in the scan reader ui

scanreader_utils = {}

--- Show a popup message that a scan was unlocked
--- @return nil
function scanreader_utils:showPopup()
	local subtitle = ba.XSTR("Scan Data Unlocked!!", 8931)
	local Utils = require('lib_utils')
	Utils.runSEXP("show-subtitle-text", subtitle, 0, 20, true, false, 2000, 2000, 400, 255, 255, 255, "Font-HUD-Primary")
	--mn.runSEXP("( show-subtitle-text !" .. subtitle .. "! 0 20 (true) (false) 2000 2000 400 255 255 255 'Font-Title')")
	local ScriptedAudio = require("soundhandler")
	local handle = ScriptedAudio:playSound("available.ogg", false)
end

--- Unlock a scan
--- @param scan scan_reader_scan
--- @return nil
function scanreader_utils:unlockScan(scan)
	local datasaver = require("lib_data_saver")

	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 == scan.Name then
			ba.print("Scan already exists, skipping!\n")
			return
		end
	end

	table.insert(scans[campaign], scan)
	datasaver:saveDataToFile("scanUnlocks", scans, true)
	scanreader_utils:showPopup()
end

--- Get all scans from the player's save file
--- @return scan_reader_scan[] scans The scans
function scanreader_utils:getScans()
	local datasaver = require("lib_data_saver")

	local campaign = ba.getCurrentPlayer():getCampaignFilename()

	local scans = datasaver:loadDataFromFile("scanUnlocks", true) or {}

	if not scans[campaign] then
		scans[campaign] = {}
	end

	return scans[campaign]
end

--- Set a scan as seen
--- @param name string The name of the scan
--- @return nil
function scanreader_utils:setSeen(name)
	local datasaver = require("lib_data_saver")

	local campaign = ba.getCurrentPlayer():getCampaignFilename()

	local scans = datasaver:loadDataFromFile("scanUnlocks", true) or {}
    if not scans[campaign] then
		scans[campaign] = {}
	end

	--- @param v scan_reader_scan
	for _, v in ipairs(scans[campaign]) do
		if v.Name == name then
			v.Seen = true
			datasaver:saveDataToFile("scanUnlocks", scans, true)
			break
		end
	end
end

--- Reset all scan data
--- @return nil
function scanreader_utils:resetData()
	local datasaver = require("lib_data_saver")

	local campaign = ba.getCurrentPlayer():getCampaignFilename()

	local scans = datasaver:loadDataFromFile("scanUnlocks", true) or {}

	scans[campaign] = {}

	datasaver:saveDataToFile("scanUnlocks", scans, true)
end
