--Helper function.
function p.price_helper(price)
    local num = 10
    if (price == 1) then num = 100 end
    if (price == 2) then num = 50 end
    return num
end

function p.render(wid)
    ga_win_set_background_default(wid)

    local price = ga_get_i("temp.xar.ent.buy_marker.price")
    local num = p.price_helper(price)
    local cost = num * price
    ga_win_txt_center(wid, 0.5,
        "Press ENTER: buy " .. num .. " markers for " .. cost .. " gold")

    local gold = ga_get_i("xar.player.gold.amount")
    ga_win_txt_center(wid, 0.9, "GOLD IN INVENTORY = " .. gold)

    local inv_markers = ga_get_i("xar.player.markers.num_short")
    ga_win_txt_center(wid, 0.8, "MARKERS IN INVENTORY = " .. inv_markers)

    ga_win_set_char_size(wid, 0.02, 0.04)
    ga_win_txt_center(wid, 0.12, "Go Back (ESC or F)")
end

function p.process_input(wid)
    if( ga_win_key_pressed(wid, "ESC") or
        ga_win_key_pressed(wid, "F") or
        ga_win_mouse_pressed(wid, true) )
    then
        ga_play_sound("menu_back")
        ga_window_pop()
    end

    if game_key.pressed_or_spammed(wid, "ENTER") then
        local price = ga_get_i("temp.xar.ent.buy_marker.price")
        local num = p.price_helper(price)
        local cost = num * price
        local gold = ga_get_i("xar.player.gold.amount")
        if (gold >= cost) then
            local new_gold = gold - cost
            ga_set_i("xar.player.gold.amount", new_gold)
            std.set_bydelta_i("xar.player.markers.num_short", num)
            ga_play_sound("buy")
        end
    end
end
