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 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},
}

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)
    if value == 1 then
        if params_h.MFCD_BACKUP_Page[2]==Pages.MENU then
            if command == MFCD_click_cmd.BTN1 then
                params_h.MFCD_BACKUP_Page[2] = Pages.SETTING
            elseif command == MFCD_click_cmd.BTN2 then
                params_h.MFCD_BACKUP_Page[2] = Pages.MP3
            elseif command == MFCD_click_cmd.BTN3 then
                params_h.MFCD_BACKUP_Page[2] = Pages.SMS
            elseif command == MFCD_click_cmd.BTN4 then
                params_h.MFCD_BACKUP_Page[2] = Pages.EHSI
            elseif command == MFCD_click_cmd.BTN5 then
                params_h.MFCD_BACKUP_Page[2] = Pages.RADAR
            elseif command == MFCD_click_cmd.BTN6 then
                params_h.MFCD_BACKUP_Page[2] = Pages.FCS
            elseif command == MFCD_click_cmd.BTN7 then
                params_h.MFCD_BACKUP_Page[2] = Pages.EADI
            end
        elseif params_h.MFCD_BACKUP_Page[2] == Pages.EADI then
            if command == MFCD_click_cmd.BTN23 then
                params_h.MFCD_BACKUP_Page[2] = Pages.MENU
            end
        elseif params_h.MFCD_BACKUP_Page[2] == Pages.FCS then
            if command == MFCD_click_cmd.BTN23 then
                params_h.MFCD_BACKUP_Page[2] = Pages.MENU
            end
        elseif params_h.MFCD_BACKUP_Page[2] == Pages.RADAR then
            if command == MFCD_click_cmd.BTN23 then
                params_h.MFCD_BACKUP_Page[2] = Pages.MENU
            end
        elseif params_h.MFCD_BACKUP_Page[2] == Pages.EHSI then
            if command == MFCD_click_cmd.BTN23 then
                params_h.MFCD_BACKUP_Page[2] = Pages.MENU
            end
        elseif params_h.MFCD_BACKUP_Page[2] == Pages.SMS then
            if command == MFCD_click_cmd.BTN23 then
                params_h.MFCD_BACKUP_Page[2] = Pages.MENU
            end
        elseif params_h.MFCD_BACKUP_Page[2] == Pages.MP3 then
            if command == MFCD_click_cmd.BTN23 then
                params_h.MFCD_BACKUP_Page[2] = Pages.MENU
            end
        elseif params_h.MFCD_BACKUP_Page[2] == Pages.SETTING then
            if command == MFCD_click_cmd.BTN23 then
                params_h.MFCD_BACKUP_Page[2] = Pages.MENU
            elseif command == MFCD_click_cmd.BTN7 then
                params_h.UNIT[2] = 1 - params_h.UNIT[2]
            end
        end
    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
