#include "../../config/constants.lua"

raceStat = {}

local num_checkpoints
local player_name
local playerPlace
local list_players
local players
local num_players

function raceStat:Init()
    num_checkpoints = GetInt('level.numcheckpoints')
    player_name     = GetString("level.player.name")
    list_players    = ListKeys("level.players")

    timer_manager:CreateTimer("update-stat-table", .5, true, function()
        players = RaceCollectAndSortPlayers()
        num_players = num_players or #players
    end)
end

function raceStat:Draw(dt)
    if not num_players then return end

    self.addWidthByName = 0
    UiPush()
        UiFont("arial.ttf", 22)
        local w, h = UiGetTextSize(GetTranslatedStringByKey(player_name))
        if w > 150 then
            self.addWidthByName = w - 150
        end
    UiPop()

    UiPush()
        UiTranslate(UiWidth() - 24, 104)
        UiAlign("right top")
        UiColor(1,1,1,1)
        for i=1, num_players do
            self:DrawRow(players[i], i)
            UiTranslate(0, 35)
        end
    UiPop()
end

function raceStat:DrawRow(playerData, id)
    UiPush()
        local w, h
        if math.abs(id - playerPlace) == 1 then
            w, h = 185 + self.addWidthByName, 31
        elseif id - playerPlace == 0 then
            w, h = 194 + self.addWidthByName, 31
        else
            w, h = 175 + self.addWidthByName, 31
        end
        PAGE_COMPONENTS:drawRoundedColorRect(w, h, 6, 0, 0, 0, 0.5)

        local name
        if playerData.disq and not playerData.finished then
            name = playerData.name
            SetColorFromHEX("#E25D5D")
        elseif playerData.name == "loc@UI_GP_LAYOUT_PLAYER" then
            name = player_name
            UiPCChooseColor()
        else
            name = playerData.name
        end

        UiTranslate(-w, 0)
        UiAlign("left top")
        UiPush()
            UiFont(DIGIT_FONT_BOLD, 22)
            UiWindow(34, 31)
            UiTranslate(UiCenter(), UiMiddle())
            UiAlign("center middle")
            UiTextUniformHeight(true)
            UiText(id)
        UiPop()

        UiTranslate(34, 0)
        UiPush()
            UiFont("arial.ttf", 22)
            UiWindow(w - 34, 31)
            UiTranslate(0, UiMiddle())
            UiAlign("left middle")
            UiTextUniformHeight(true)
            UiText(name)
        UiPop()
    UiPop()
end

function GetDisqOrderedIndex(player, reg)
    local disqPlayersOrder = reg or GetString("level.disqualified")

    local i = 0
    for n in disqPlayersOrder:gmatch"%S+" do
        i = i+1
        if n == player then
            return i
        end
    end
end

function RaceCollectAndSortPlayers()
    local result, sortResult = {}, {}
    local disq = GetString("level.disqualified")
    local finished, numlaps
    if not isElimination then
        finished, numlaps = GetString("level.finished"), GetInt("level.numlaps")
    end
    local evalCurrentWp = GetInt('level.playerlap') * num_checkpoints + GetInt("level.playercheckpoint")
    for i=1, (num_players or #list_players) do
        result[i] = {}
        result[i].id = i
        result[i].name = GetString("level.players."..i..".name")
        local place = GetInt("level.players."..i..".place")
        result[i].place = place
        result[i].time = GetFloat("checkpoint." .. evalCurrentWp .. '.' .. i)
        if result[i].name == "loc@UI_GP_LAYOUT_PLAYER" then
            playerPlace = place
        end

        local index = list_players[i]
        if disq:find(index) then
            result[i].disq = true
            result[i].disq_order = GetDisqOrderedIndex(index, disq)
            result[i].time = 0
        end
        if not isElimination and finished:count(index) >= numlaps then
            result[i].finished = true
        end

        sortResult[place] = result[i]
    end

    return sortResult
end
