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

derbyStat = {}

local player_name
local list_players
local players
local num_players

function derbyStat:Init()
    player_name     = GetString("level.player.name")
    player_id       = GetInt("level.player.id")
    list_players    = ListKeys("level.players")

    RegisterListenerTo(EVENTS.GAME_OVER, "DerbyCollectAndSortPlayers")
    timer_manager:CreateTimer("update-stat-table", 1.0, true, function()
        players = DerbyCollectAndSortPlayers()
        num_players = num_players or #players
    end)
end

function derbyStat: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, 112)

        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 derbyStat:DrawRow(playerData, id)
    UiPush()
        local w, h
        local playerPlace = GetInt("level.playerplace")
        if math.abs(id - playerPlace) == 1 then
            w, h = 242 + self.addWidthByName, 31
        elseif id - playerPlace == 0 then
            w, h = 251 + self.addWidthByName, 31
        else
            w, h = 232 + self.addWidthByName, 31
        end
        PAGE_COMPONENTS:drawRoundedColorRect(w, h, 6, 0, 0, 0, 0.5)

        local name
        if playerData.disq 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)

            UiFont(DIGIT_FONT_BOLD, 22)
            UiTranslate(UiWidth() - 18, 0)
            UiAlign("right middle")
            UiText(playerData.damage)
        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 DerbyCollectAndSortPlayers()
    local result = {}
    local disq_reg = GetString("level.disqualified")
    for i=1, (num_players or #list_players) do
        result[i] = {}
        result[i].id = i
        result[i].name = GetString("level.players."..i..".name")
        result[i].damage = GetInt("level.players."..i..".damage")

        local index = list_players[i]
        if disq_reg:find_exact(index) then
            result[i].disq = true
            result[i].disq_order = GetDisqOrderedIndex(index, disq_reg)
        end
    end

    table.sort(result, function(a, b)
        if a.disq and b.disq then return a.disq_order > b.disq_order end
        if a.disq then return false end
        if b.disq then return true end
        return a.damage > b.damage
    end)

    return result
end