function p.bool_to_yes_no(x)
    if x then return "YES"
    else return "NO"
    end
end

function p.on_start(wid)
    p.make_list(wid)
end

function p.on_end(wid)
    --Nothing to do.
end

--Helper function.
function p.make_list(wid)
    local show_health = ga_get_b("xar.hud.show_health")
    local show_ammo = ga_get_b("xar.hud.show_ammo")
    local show_markers = ga_get_b("xar.hud.show_markers")
    local show_gold = ga_get_b("xar.hud.show_gold")
    local show_experience = ga_get_b("xar.hud.show_experience")
    local show_icons = ga_get_b("xar.hud.show_icons")
    --
    local min_y = 0.2
    local max_y = 0.7
    local char_w = 0.015
    local char_h = 0.03
    local col = {x=0.0, y=0.5, z=0.5}
    local options = {
        "SHOW HEALTH: " .. p.bool_to_yes_no(show_health),
        "SHOW AMMO: " .. p.bool_to_yes_no(show_ammo),
        "SHOW MARKERS: " .. p.bool_to_yes_no(show_markers),
        "SHOW GOLD: " .. p.bool_to_yes_no(show_gold),
        "SHOW EXPERIENCE: " .. p.bool_to_yes_no(show_experience),
        "SHOW ICONS: " .. p.bool_to_yes_no(show_icons)}
    ga_win_widget_small_list_start(
        wid, min_y, max_y, char_w, char_h,
        col, options)
    ga_win_widget_small_list_set_use_nums(wid, false)
end

function p.render(wid)
    ga_win_set_background(wid, std.vec(0.0, 0.0, 0.0), 0.95)

    ga_win_set_char_size(wid, 0.03, 0.06)
    ga_win_txt_center(wid, 0.85, "HUD")

    ga_win_set_char_size(wid, 0.02, 0.04)
    ga_win_txt_center(wid, 0.06, "Go Back (ESC)")
end

function p.process_input(wid)
    if ga_win_key_pressed(wid, "ESC") then
        ga_play_sound("menu_back")
        ga_main_menu_pop()
    end

    local selection = ga_win_widget_small_list_process_input(wid)
    local sel_name = ga_win_widget_small_list_get_entry(wid, selection)
    --
    if sel_name:find("^SHOW HEALTH") ~= nil then
        ga_toggle_b("xar.hud.show_health")
        p.make_list(wid)
        ga_win_widget_small_list_set_selected(wid, 0) --Fragile!!!
    end
    if sel_name:find("^SHOW AMMO") ~= nil then
        ga_toggle_b("xar.hud.show_ammo")
        p.make_list(wid)
        ga_win_widget_small_list_set_selected(wid, 1) --Fragile!!!
    end
    if sel_name:find("^SHOW MARKERS") ~= nil then
        ga_toggle_b("xar.hud.show_markers")
        p.make_list(wid)
        ga_win_widget_small_list_set_selected(wid, 2) --Fragile!!!
    end
    if sel_name:find("^SHOW GOLD") ~= nil then
        ga_toggle_b("xar.hud.show_gold")
        p.make_list(wid)
        ga_win_widget_small_list_set_selected(wid, 3) --Fragile!!!
    end
    if sel_name:find("^SHOW EXPERIENCE") ~= nil then
        ga_toggle_b("xar.hud.show_experience")
        p.make_list(wid)
        ga_win_widget_small_list_set_selected(wid, 4) --Fragile!!!
    end
    if sel_name:find("^SHOW ICONS") ~= nil then
        ga_toggle_b("xar.hud.show_icons")
        p.make_list(wid)
        ga_win_widget_small_list_set_selected(wid, 5) --Fragile!!!
    end
end

function p.set_true_up(use_true_up)
    trans = std.vec(0.0, 0.0, 0.0)
    radius = ga_get_sys_f("game.player.move.fly.radius")
    ga_move_set_body_fly(trans, radius, use_true_up)
end
