function p.strip_option(option)
    if option:find("^x00ff00", 1, true) then
        local new = string.sub(option, 9)
        return new
    end
    return option
end

function p.on_start(wid)
    ga_init_i("temp.xar.stats7.win_num", 1)
    ga_init_i("temp.xar.stats7.wep_mode", 1)
    p.init_list_widget(wid, 1)
end

function p.render(wid)
    ga_init_i("temp.xar.stats7.win_num", 1)
    ga_init_i("temp.xar.stats7.wep_mode", 1)
    local win_num = ga_get_i("temp.xar.stats7.win_num")
    local mode = ga_get_i("temp.xar.stats7.wep_mode")

    ga_win_set_background(wid, std.vec(0.0, 0.0, 0.0), 0.95)
    
    ga_win_set_char_size(wid, 0.02, 0.04)
    ga_win_txt_center(wid, 0.92, "WEAPON MODES")

    ga_win_set_char_size(wid, 0.01, 0.02)
    ga_win_txt_center(wid, 0.88, "USE LEFT / RIGHT ARROWS TO SELECT A WEAPON")
    local wep_name = game_wep_modes.get_wep_name(win_num % 10)
    ga_win_txt_center(wid, 0.84, "WEAPON #" .. tostring(win_num) .. " (" .. wep_name .. ")")

    ga_win_txt_center(wid, 0.76, "USE UP / DOWN KEYS TO SELECT A WEAPON \"MODE\"")
    ga_win_txt_center(wid, 0.72, "PRESS ENTER TO EQUIPP THE WEAPON MODE (ONLY TWO CAN BE EQUIPPED)")

    if win_num == 1 then p.bar_graphs_1(wid, mode) end
    if win_num == 2 then p.bar_graphs_2(wid, mode) end
    --more!!!
    if win_num == 4 then p.bar_graphs_4(wid, mode) end
    --more!!!
    if win_num == 7 then p.bar_graphs_7(wid, mode) end
    --more!!!
    if win_num == 9 then p.bar_graphs_9(wid, mode) end
    --more!!!

    --DEBUGGING!!!
    -- ga_win_txt(wid, 0.2, 0.4, "MODE = " .. tostring(mode))

    ga_win_set_char_size(wid, 0.02, 0.04)

    ga_win_set_char_size(wid, 0.015, 0.03)
    ga_win_txt_center(wid, 0.06, "Go Back (ESC)")
end

function p.init_list_widget(wid, sel_mode)
    local wep_num = ga_get_i("temp.xar.stats7.win_num")
    if wep_num == 10 then wep_num = 0 end
    local min_y = 0.15
    local max_y = 0.5
    local w = 0.02
    local h = 0.04
    local col = {x=0.0, y=0.5, z=0.5}
    --
    options = {}
    local max_mode = game_wep_modes.get_max_mode(wep_num)
    for i = 1,max_mode do
        if game_wep_modes.get_has(wep_num,i) then
            local color_esc = ""
            if game_wep_modes.get_equipped(wep_num, i) then color_esc = "^x00ff00" end
            table.insert(options, color_esc .. game_wep_modes.get_wep_mode_name(wep_num,i))
        end
    end
    --
    ga_win_widget_small_list_start(wid, min_y, max_y, w, h, col, options)
    --
    ga_win_widget_small_list_set_selected(wid, sel_mode-1)
end

function p.draw_bar(wid, min_y, frac)
    local grey = std.vec(0.2, 0.2, 0.2)
    local green = std.vec(0.0, 1.0, 0.0)
    local min_x = 0.37
    local max_x = 0.8
    ga_win_quad_color(wid, min_x, min_y, max_x, min_y+0.03, grey)
    local mid_x = (1.0 - frac) * min_x + frac * max_x
    ga_win_quad_color(wid, min_x, min_y, mid_x, min_y+0.03, green)
end

function p.bar_graphs_1(wid, mode)
    ga_win_txt(wid, 0.2, 0.68, " DAMAGE PER SEC:")
    ga_win_txt(wid, 0.2, 0.64, "DAMAGE PER AMMO:")
    ga_win_txt(wid, 0.2, 0.60, "       VELOCITY:")
    ga_win_txt(wid, 0.2, 0.56, "       ACCURACY:")
    --    
    local max_dps = math.max(
        game_wep_modes.get_dps(1,3),
        game_wep_modes.get_dps(1,4))
    local dps = game_wep_modes.get_dps(1,mode)
    p.draw_bar(wid, 0.68, dps / max_dps)
    --
    local max_dpa = game_wep_modes.get_dpa(1,1)
    local dpa = game_wep_modes.get_dpa(1,mode)
    p.draw_bar(wid, 0.64, dpa / max_dpa)
    --
    local max_vel = game_wep_modes.get_vel(1,4)
    local vel = game_wep_modes.get_vel(1,mode)
    local vel_frac = 0.5 * (vel / max_vel)
    if mode == 5 then vel_frac = 1.0 end
    p.draw_bar(wid, 0.60, vel_frac)
    --
    local accuracy = 1.0 - 2.0 * game_wep_modes.get_spread(1,mode)
    p.draw_bar(wid, 0.56, accuracy)
end

function p.bar_graphs_2(wid, mode)
    ga_win_txt(wid, 0.2, 0.68, " DAMAGE PER SEC:")
    ga_win_txt(wid, 0.2, 0.64, "DAMAGE PER AMMO:")
    ga_win_txt(wid, 0.2, 0.60, "       VELOCITY:")
    ga_win_txt(wid, 0.2, 0.56, "       ACCURACY:")
    --
    local max_dps =
        game_wep_modes.get_dps(2,2)
    local dps = game_wep_modes.get_dps(2,mode)
    p.draw_bar(wid, 0.68, dps / max_dps)
    --
    local max_dpa = game_wep_modes.get_dpa(2,1)
    local dpa = game_wep_modes.get_dpa(2,mode)
    p.draw_bar(wid, 0.64, dpa / max_dpa)
    --
    local max_vel = game_wep_modes.get_vel(2,2)
    local vel = game_wep_modes.get_vel(2,mode)
    local vel_frac = 0.5 * (vel / max_vel)
    p.draw_bar(wid, 0.60, vel_frac)
    --
    local accuracy = 1.0 - 4.0 * game_wep_modes.get_spread(2,mode)
    p.draw_bar(wid, 0.56, accuracy)
end

--more!!!

function p.bar_graphs_4(wid, mode)
    ga_win_txt(wid, 0.2, 0.68, " DAMAGE PER SEC:")
    ga_win_txt(wid, 0.2, 0.64, "DAMAGE PER AMMO:")
    ga_win_txt(wid, 0.2, 0.60, "       VELOCITY:")
    ga_win_txt(wid, 0.2, 0.56, "         RADIUS:")
    -- --
    local max_dps = game_wep_modes.get_dps(4,5)
    local dps = game_wep_modes.get_dps(4,mode)
    p.draw_bar(wid, 0.68, dps / max_dps)
    -- --
    local max_dpa = game_wep_modes.get_dpa(4,5)
    local dpa = game_wep_modes.get_dpa(4,mode)
    p.draw_bar(wid, 0.64, dpa / max_dpa)
    -- --
    local max_vel = game_wep_modes.get_vel(4,2)
    local vel = game_wep_modes.get_vel(4,mode)
    local vel_frac = 2.0 * vel / max_vel
    if mode == 2 then vel_frac = 1.0 end
    p.draw_bar(wid, 0.60, vel_frac)
    -- --
    local max_radius = game_wep_modes.get_radius(4,3)
    local radius = game_wep_modes.get_radius(4,mode)
    p.draw_bar(wid, 0.56, radius / max_radius)
end

function p.bar_graphs_7(wid, mode)
    local wep = 7
    ga_win_txt(wid, 0.2, 0.68, " DAMAGE PER SEC:")
    ga_win_txt(wid, 0.2, 0.64, "DAMAGE PER AMMO:")
    ga_win_txt(wid, 0.2, 0.60, "   HEALTH STEAL:")
    ga_win_txt(wid, 0.2, 0.56, "       ACCURACY:")
    --
    local max_dps =
        game_wep_modes.get_dps(wep,2)
    local dps = game_wep_modes.get_dps(wep,mode)
    p.draw_bar(wid, 0.68, dps / max_dps)
    --
    local max_dpa = game_wep_modes.get_dpa(wep,1)
    local dpa = game_wep_modes.get_dpa(wep,mode)
    p.draw_bar(wid, 0.64, dpa / max_dpa)
    --
    local max_health_steal_max = game_wep_modes.get_health_bonus(wep, 3)
    local health_steal = game_wep_modes.get_health_bonus(wep, mode)
    p.draw_bar(wid, 0.60, health_steal / max_health_steal_max)
    --
    local accuracy = 1.0 - 8.0 * game_wep_modes.get_spread(wep,mode)
    p.draw_bar(wid, 0.56, accuracy)
end

function p.bar_graphs_9(wid, mode)
    ga_win_txt(wid, 0.2, 0.68, " DAMAGE PER SEC:")
    ga_win_txt(wid, 0.2, 0.64, "DAMAGE PER AMMO:")
    ga_win_txt(wid, 0.2, 0.60, "       VELOCITY:")
    ga_win_txt(wid, 0.2, 0.56, "         RADIUS:")
    -- --
    local max_dps = game_wep_modes.get_dps(9,3)
    local dps = game_wep_modes.get_dps(9,mode)
    p.draw_bar(wid, 0.68, dps / max_dps)
    -- --
    local max_dpa = game_wep_modes.get_dpa(9,1)
    local dpa = game_wep_modes.get_dpa(9,mode)
    p.draw_bar(wid, 0.64, dpa / max_dpa)
    -- --
    local max_vel = game_wep_modes.get_vel(9,2)
    local vel = game_wep_modes.get_vel(9,mode)
    local vel_frac = vel / max_vel
    p.draw_bar(wid, 0.60, vel_frac)
    -- --
    local max_radius = game_wep_modes.get_radius(9,2)
    local radius = game_wep_modes.get_radius(9,mode)
    p.draw_bar(wid, 0.56, radius / max_radius)
end

--more!!!

function p.list_sel_to_wep_mode(wid, sel)
    local sel_name1 = ga_win_widget_small_list_get_entry(wid, sel)
    local sel_name2 = p.strip_option(sel_name1)
    local wep_num = ga_get_i("temp.xar.stats7.win_num")
    local sel_mode = game_wep_modes.wep_mode_name_to_num(wep_num, sel_name2)
    return sel_mode
end

function p.process_input(wid)
    if ga_win_key_pressed(wid, "ESC") then
        ga_window_pop()
        return
    end

    ga_init_i("temp.xar.stats7.win_num", 1)
    local wep_num = ga_get_i("temp.xar.stats7.win_num")

    local sel = ga_win_widget_small_list_process_input(wid)
    if sel >= 0 then
        local sel_mode = p.list_sel_to_wep_mode(wid, sel)
        game_wep_modes.equip_mode(wep_num, sel_mode)
        p.init_list_widget(wid, sel_mode)
    end

    if ga_win_key_pressed(wid, "F1") then
        --Opening the xar menu.
        ga_window_pop()
        ga_main_menu_push("main_menu")
        return
    end
    if ga_win_key_pressed(wid, "F2") then
        if( not ga_genesis() ) then
            ga_window_pop()
            ga_window_push("win_stats2")
            return
        end
    end
    if ga_win_key_pressed(wid, "F3") then
        if( not ga_genesis() ) then
            ga_window_pop()
            ga_window_push("win_stats3")
            return
        end
    end
    if ga_win_key_pressed(wid, "F4") then
        if( not ga_genesis() ) then
            ga_window_pop()
            ga_window_push("win_stats4")
            return
        end
    end
    if ga_win_key_pressed(wid, "F5") then
        ga_window_pop()
        ga_window_push("win_stats5")
        return
    end
    if ga_win_key_pressed(wid, "F6") then
        ga_window_pop()
        ga_window_push("win_stats6")
        return
    end
    --

    --Switching sub windows.
    if ga_win_key_pressed(wid, "LEFT")  and wep_num > 1 then
        wep_num = wep_num - 1
        ga_set_i("temp.xar.stats7.win_num", wep_num)
        p.init_list_widget(wid, wep_num)
    end
    if ga_win_key_pressed(wid, "RIGHT") and wep_num < 10 then
        wep_num = wep_num + 1
        ga_set_i("temp.xar.stats7.win_num", wep_num)
        p.init_list_widget(wid, wep_num)
    end

    local sel = ga_win_widget_small_list_get_selected(wid)
    local sel_mode = p.list_sel_to_wep_mode(wid, sel)
    if( sel_mode < 1 ) then sel_mode = 1 end --Uh oh!

    ga_init_i("temp.xar.stats7.wep_mode", 1)
    ga_set_i("temp.xar.stats7.wep_mode", sel_mode)
end
