--This was written by Xeno.

--Backwards compatibility.
function p.get_name()
    return p.__get_name()
end

function p.__get_name()
    return "Session Time"
end

function time_str_ms_session()
    local time_t = ga_get_sys_f("game.time.total")
    local time_a = time_t - ga_get_f("xar.difficulty.speedrun_start")

    ga_init_f("temp.session_time", time_a)
    local time_s = time_a - ga_get_f("temp.session_time")

    local seconds_total = math.floor(time_s)
    local milliseconds = math.floor((time_s - seconds_total) * 1000)
    local seconds = math.floor( math.fmod(seconds_total, 60.0) )
    local minutes_total = math.floor( (seconds_total - seconds) / 60.0 )
    local minutes = math.floor( math.fmod(minutes_total, 60.0) )
    local hours_total = math.floor( (minutes_total - minutes) / 60.0 )
    return base_local_str.make_3_digit(hours_total) .. ":"
        .. base_local_str.make_2_digit(minutes) .. ":"
        .. base_local_str.make_2_digit(seconds) .. "."
        .. base_local_str.make_3_digit(milliseconds),
       math.ceil(time_s) >= math.floor(time_t)
end

--Backwards compatibility.
function p.render(wid, min_x, min_y, max_x, max_y)
    p.__render(wid, min_x, min_y, max_x, max_y)
end

function p.__render(wid, min_x, min_y, max_x, max_y)
    if ga_exists("xar.difficulty.speedrun_start") then
        local diff_y = max_y - min_y
        local char_h = diff_y * 0.75
        local char_w = char_h * 0.5
        local timeStr,isSame = time_str_ms_session()
        local str = "Session Time: " .. timeStr
        local green = std.vec(0.0, 1.0, 0.0)
        local red = std.vec(1.0, 0.0, 0.0)

        -- if isSame == true then
        --     ga_win_set_front_color(wid, green)
        -- else
        --     ga_win_set_front_color(wid, red)
        -- end
        ga_win_set_front_color(wid, green)

        ga_win_set_char_size(wid, char_w, char_h)
        ga_win_txt(wid, min_x, min_y, str)
    end
end
