-- JackZero:
-- Save several settings (barometer mode, gpu mode, metric alt, 8 khz radios, map declutter) independently of 
-- position saving:

defineProperty("gpu_mode",  globalPropertyi("cl300/gpu_mode"))  -- JackZero: New DataRef: 0 = auto, 1 = manual

defineProperty("baro_pref", globalPropertyi("cl300/baro_pref")) -- JackZero: Save barometer setting (inhg or hpa)
defineProperty("com_pref", globalPropertyi("cl300/com_pref"))   -- JackZero: Save 8 kHz setting
defineProperty("alt_pref", globalPropertyi("cl300/alt_pref"))   -- JackZero: Save metric alt display setting

defineProperty("EFIS_shows_airports", globalPropertyi("sim/cockpit/switches/EFIS_shows_airports"))
defineProperty("EFIS_shows_waypoints", globalPropertyi("sim/cockpit/switches/EFIS_shows_waypoints"))
defineProperty("EFIS_shows_VORs", globalPropertyi("sim/cockpit/switches/EFIS_shows_VORs"))
defineProperty("EFIS_shows_NDBs", globalPropertyi("sim/cockpit/switches/EFIS_shows_NDBs"))

defineProperty("map_range", globalPropertyi("sim/cockpit2/EFIS/map_range"))
defineProperty("map_mode", globalPropertyi("sim/cockpit2/EFIS/map_mode"))
defineProperty("map_mode_is_HSI", globalPropertyi("sim/cockpit2/EFIS/map_mode_is_HSI"))

defineProperty("use_xhsi", globalPropertyi("cl300/use_xhsi")) -- JackZero: XHSI present?
if get(use_xhsi) then
	defineProperty("xhsi_nd_pilot_pos",  		globalPropertyi("xhsi/nd_pilot/pos"))
	defineProperty("xhsi_nd_pilot_data", 		globalPropertyi("xhsi/nd_pilot/data"))
	defineProperty("xhsi_nd_pilot_map_zoomin",  globalPropertyi("xhsi/nd_pilot/map_zoomin"))
    defineProperty("xhsi_nd_copilot_map_mode", 	globalPropertyi("xhsi/nd_copilot/map_mode"))
end

local initframe = 2

settings = {xhsi_nd_pilot_map_zoomin = 0, xhsi_nd_copilot_map_mode = 3, xhsi_nd_pilot_pos = 0, xhsi_nd_pilot_data = 1} -- some defaults in case XHSI isn't used

-- read file
local filename = panelDir .. "/defaults.ini"
local file = io.open(filename, "r")
if file then
	local lines = file:read("*a")
	for k, v in string.gmatch(lines, "([%w_]+) = (%d+)") do
		settings[k] = v
	end
	file:close()
	initframe = 1
end

function update()

if initframe == 1 then
    -- load settings during first frame
    set(gpu_mode,  settings.gpu_mode)
    set(baro_pref, settings.baro_pref)
    set(com_pref,  settings.com_pref)
    set(alt_pref,  settings.alt_pref)
    
    set(map_range,       settings.map_range)
    set(map_mode,        settings.map_mode)
    set(map_mode_is_HSI, settings.map_mode_is_HSI)
    
    set(EFIS_shows_airports,  settings.EFIS_shows_airports)
    set(EFIS_shows_waypoints, settings.EFIS_shows_waypoints)
    set(EFIS_shows_VORs,      settings.EFIS_shows_VORs)
    set(EFIS_shows_NDBs,      settings.EFIS_shows_NDBs)
    
    if get(use_xhsi) then
        set(xhsi_nd_pilot_pos,         settings.xhsi_nd_pilot_pos)
        set(xhsi_nd_pilot_data,        settings.xhsi_nd_pilot_data)
        set(xhsi_nd_pilot_map_zoomin,  settings.xhsi_nd_pilot_map_zoomin)
        set(xhsi_nd_copilot_map_mode,  settings.xhsi_nd_copilot_map_mode)
    end
        
    initframe = 2

else
    
    -- update settings
    settings.gpu_mode  = get(gpu_mode)
    settings.baro_pref = get(baro_pref)
    settings.com_pref  = get(com_pref)
    settings.alt_pref  = get(alt_pref)
    
    settings.map_range       = get(map_range)
    settings.map_mode        = get(map_mode)
    settings.map_mode_is_HSI = get(map_mode_is_HSI)
    
    settings.EFIS_shows_airports  = get(EFIS_shows_airports)
    settings.EFIS_shows_waypoints = get(EFIS_shows_waypoints)
    settings.EFIS_shows_VORs      = get(EFIS_shows_VORs)
    settings.EFIS_shows_NDBs      = get(EFIS_shows_NDBs)
    
    if get(use_xhsi) then
        settings.xhsi_nd_pilot_pos         = get(xhsi_nd_pilot_pos)
        settings.xhsi_nd_pilot_data        = get(xhsi_nd_pilot_data)
        settings.xhsi_nd_pilot_map_zoomin  = get(xhsi_nd_pilot_map_zoomin)
        settings.xhsi_nd_copilot_map_mode  = get(xhsi_nd_copilot_map_mode)
    end
    
    thisval = settings.gpu_mode 
			.. settings.baro_pref 
			.. settings.com_pref 
			.. settings.alt_pref
            .. settings.map_range 
			.. settings.map_mode 
			.. settings.map_mode_is_HSI
            .. settings.EFIS_shows_airports 
			.. settings.EFIS_shows_waypoints 
			.. settings.EFIS_shows_VORs 
			.. settings.EFIS_shows_NDBs
            .. settings.xhsi_nd_pilot_pos 
			.. settings.xhsi_nd_pilot_data 
			.. settings.xhsi_nd_pilot_map_zoomin 
			.. settings.xhsi_nd_copilot_map_mode
    
	if initframe == 2 then
		lastval = thisval
		initframe = 3
	else
		if lastval ~= thisval then
			lastval = thisval
			-- Save changed settings to file:
			local filename = panelDir .. "/defaults.ini"
			local savefile = io.open(filename, "w")
			for k, v in pairs(settings) do savefile:write(k," = ",v,"\n") end
			savefile:close()
		end
	end
end

end
