#include "../../../../config/input.lua"
#include "../../../../config/constants.lua"
#include "../../../../script/helpers/utils.lua"
#include "ui_cr_map.lua"
#include "ui_cr_opponents.lua"
#include "ui_cr_race.lua"

UI_CR = {
    -- pages presets
    pages = {
        { name = "loc@PC_MAP" },
        { name = "loc@PC_OPPONENTS" },
        { name = "loc@RACE" },
    },

    -- variables
    notification = "ui/menu/promo-notification.png",
    currentPage = 1,
    initOver = false,
}

-- Custom race params
CUSTOM_RACE = {
    is_custom_race  = true, 
    level           = nil,
    gamemode        = nil,
    environment     = nil,
    participants    = nil,
    variation       = nil,
    racingParams    = {},
    carClass        = {},
    carTier         = {},
    clickedBtn      = 0,
}

local casePages = {
    [1] = function(dt)
        return UI_CR_MAP:draw(dt)
    end,
    [2] = function(dt)
        return UI_CR_OPPONENTS:draw(dt)
    end,
    [3] = function(dt)
        return UI_CR_RACE:draw(dt)
    end,
}

local caseUpdateCRParams = {
    [1] = function(dt)
        UI_CR_MAP:UpdateCustomRaceParams()
    end,
    [2] = function(dt)
        UI_CR_OPPONENTS:UpdateCustomRaceParams()
    end,
    [3] = function(dt)
        UI_CR_RACE:UpdateCustomRaceParams()
    end,
}

function UI_CR:init()
    self.initOver = true
    CUSTOM_RACE.participants = 6
    CUSTOM_RACE.carTier = { "B" }
    CUSTOM_RACE.carClass = { "wagon" }

    UnregisterListener(EVENTS.UI_COMPUTER_HOME_BTN, "CRPressBack")
    RegisterListenerTo(EVENTS.UI_COMPUTER_HOME_BTN, "CRPressBack")
end

function UI_CR:draw(dt)
    if not self.initOver then
        self:init()
    end

    if INPUT.MENU_PREV() then
        UI_CR:UpdateCustomRaceParams(math:Clamp(self.currentPage - 1, 1, #self.pages))
    end
    
    if INPUT.MENU_NEXT() then
        UI_CR:UpdateCustomRaceParams(math:Clamp(self.currentPage + 1, 1, #self.pages))
    end

    self.clickedBtn = PLATFORMS:drawControls("CUSTOM_RACE", 0, 40, 0, { true })
    UiPush()
        local needDrawNotif = false
        local returnValue = nil

        -- Draw switch between pages
        UiPush()
            UiTranslate(UiCenter(), 172)
            UiAlign("center middle")
            pageCallback = function(newPageIndex)
                if newPageIndex == UI_CR.currentPage then return end
                UI_CR:UpdateCustomRaceParams(newPageIndex)
            end
            PAGE_COMPONENTS:drawClassesSlider(self.pages, self.currentPage, not isUseDpadInput(), pageCallback)
        UiPop()
        
        -- Draw page
        UiPush()
            if casePages[self.currentPage] then
                returnValue = casePages[self.currentPage](dt)
            end
        UiPop()

        if returnValue then
            self:UpdateCustomRaceParams(returnValue)
        end
    UiPop()
    if self.clickedBtn == 1 then
        CRPressBack("CR")
    end
end

function UI_CR:UpdateCustomRaceParams(newPage)
    if caseUpdateCRParams[self.currentPage] then
        caseUpdateCRParams[self.currentPage]()
    end
    self.currentPage = newPage
end

function UI_CR:resetCustomRace()
    UI_CR_MAP:reset()
    UI_CR_OPPONENTS:reset()
    UI_CR_RACE:reset()
    for k, v in pairs(CUSTOM_RACE) do
        CUSTOM_RACE[k] = nil
    end
    CUSTOM_RACE.is_custom_race = true
end

function CRPressBack(title)
    if title ~= "CR" then return end

    UI_COMPUTER:setPage("MAIN_MENU")
    UI_CR.initOver = false
    UI_CR.currentPage = 1
    UI_CR:resetCustomRace()
end