dofile(LockOn_Options.script_path.."command_defs.lua")
dofile(LockOn_Options.script_path.."devices.lua")
dofile(LockOn_Options.script_path.."Systems/electric_system_api.lua")
dofile(LockOn_Options.script_path.."utils.lua")
dofile(LockOn_Options.script_path.."displays/MFD-backUp/indicator/pages.lua")

local update_time_step = 0.02  --每秒50次刷新
make_default_activity(update_time_step)

local dayNight = 0
local dayNight_h = get_param_handle('DAY_NIGHT')
local Enable_MP3Player = get_aircraft_property("MP3Player")
local dev = GetSelf()
local params_h = {
    MFCD_BACKUP_BRT = {get_param_handle('MFCD-BACKUP_BRT'),0,0,0.01},
    MFCD_BACKUP_Page = {get_param_handle('MFCD-BACKUP_PAGE'),Pages.MENU},
    UNIT = {get_param_handle('UNIT'),0},
    VR_MODE = {get_param_handle('VR_MODE'),1}, -- 1:VR, 0:非VR
}

function post_initialize()
    params_h.UNIT[2] = 1
	params_h.MFCD_BACKUP_BRT[2] = 1
    params_h.MFCD_BACKUP_BRT[3] = 0.98
end

function SetCommand(command, value)
    -- 1. 仅在按键按下时处理
    if value ~= 1 then
        return
    end

    local pageHandle = params_h.MFCD_BACKUP_Page
    local curPage   = pageHandle[2]
    local Pages     = Pages              -- 全局 Pages 枚举
    local ClickCmd  = MFCD_click_cmd     -- 全局点击命令枚举


    -- 2. 主菜单页：BTN1–BTN7 切换到不同子页
    if curPage == Pages.MENU then
        -- 按钮→页面的映射表
        local menuMap = {
            --[ClickCmd.BTN1] = Pages.SETTING,
            --[ClickCmd.BTN2] = Pages.MP3,
            --[ClickCmd.BTN3] = Pages.SMS,
            --[ClickCmd.BTN4] = Pages.EHSI,
            --[ClickCmd.BTN5] = Pages.RADAR,
            --[ClickCmd.BTN6] = Pages.FCS,
            --[ClickCmd.BTN7] = Pages.EADI,
        }
		if Enable_MP3Player==1 then
			menuMap ={
            [ClickCmd.BTN1] = Pages.SETTING,
            [ClickCmd.BTN2] = Pages.MP3,
            [ClickCmd.BTN3] = Pages.SMS,
            [ClickCmd.BTN4] = Pages.EHSI,
            [ClickCmd.BTN5] = Pages.RADAR,
            [ClickCmd.BTN6] = Pages.FCS,
            [ClickCmd.BTN7] = Pages.EADI,
			}
		else
			menuMap ={
            [ClickCmd.BTN1] = Pages.SETTING,
            --[ClickCmd.BTN2] = Pages.MP3,
            [ClickCmd.BTN3] = Pages.SMS,
            [ClickCmd.BTN4] = Pages.EHSI,
            [ClickCmd.BTN5] = Pages.RADAR,
            [ClickCmd.BTN6] = Pages.FCS,
            [ClickCmd.BTN7] = Pages.EADI,
			}
		end
        local gotoPage = menuMap[command]
        if gotoPage then
            pageHandle[2] = gotoPage
        end
        return
    end

    -- 3. 非主菜单页：按 BTN23 返回主菜单
    if command == ClickCmd.BTN23 then
        pageHandle[2] = Pages.MENU
        return
    end

    -- 4. SETTING 页的额外逻辑：BTN7 切换单位
    if curPage == Pages.SETTING and command == ClickCmd.BTN7 then
        params_h.UNIT[2] = 1 - params_h.UNIT[2]
    end
    -- 5. SETTING 页的额外逻辑：BTN6 切换VR/非VR
    if curPage == Pages.SETTING and command == ClickCmd.BTN6 then
        params_h.VR_MODE[2] = 1 - params_h.VR_MODE[2]
    end
end

function update()
    dayNight=dayNight_h:get()
    if (get_elec_primary_dc_ok() or get_elec_primary_ac_ok()) and params_h.MFCD_BACKUP_BRT[2] == 0 then
        params_h.MFCD_BACKUP_BRT[2] = 1
    elseif (not get_elec_primary_dc_ok() and not get_elec_primary_ac_ok()) and params_h.MFCD_BACKUP_BRT[2] > 0 then
        params_h.MFCD_BACKUP_BRT[2] = 0
        params_h.MFCD_BACKUP_BRT[3] = 0.02
    end
	for k, v in pairs(params_h) do
        if type(v[3])~="nil" then
            if v[2] > v[3] then
                v[3] = Limit(v[3]+v[4],0,1)
            elseif v[2]< v[3] then
                v[3] = Limit(v[3]-v[4],0,1)
            end
            if k == 'MFCD_BACKUP_BRT' then
                v[1]:set(v[3]*(1-0.8*dayNight))
            else
                v[1]:set(v[3])
            end
        else
            v[1]:set(v[2])
        end
    end
end

need_to_be_closed = false
