-- https://teardowngame.com/modding/api.html

#include "../../script/helpers/color.lua"
#include "../../script/helpers/utils.lua"
#include "../../script/money.lua"
#include "../../config/input.lua"
#include "../../config/platforms.lua"
#include "../../config/car_info.lua"
#include "../../config/maps.lua"

#include "computer/components/ui_slider.lua"
#include "computer/components/ui_header.lua"
#include "computer/components/page_components.lua"
#include "computer/pages/ui_mainMenu.lua"
#include "computer/pages/ui_career.lua"
#include "computer/pages/ui_championship.lua"
#include "computer/pages/ui_cr.lua"
#include "computer/pages/ui_missionMenu.lua"


UI_COMPUTER = {
    page = "MAIN_MENU",
}

PAGE_STATE = {
    ["MAIN_MENU"] = CAMPAIGN,
    ["CAREER"] = CAMPAIGN,
}

function init()
    PAGE_COMPONENTS:countTotalStars()
end

function draw(dt)
    if GetInt("level.garage.open") > 0 then return end
    local x, y = UiGetMousePos()
    SetInt("level.pc_x_pos", x)
    SetInt("level.pc_y_pos", y)

    PC_Screen = PC_Screen or UiGetScreen()
    UI_COMPUTER:draw(dt)
end

local case = {
    ["MAIN_MENU"] = function(dt)
        UI_MAIN_MENU:draw(dt, 1089, 632)
    end,
    ["CAREER"] = function(dt)
        SetBool("game.disablepause", GetPlayerScreen() == PC_Screen)
        UI_CAREER:draw(dt, UiWidth(), 794/900*UiHeight(), PAGE_STATE[UI_COMPUTER.page])
    end,
    ["CHAMPIONSHIP"] = function(dt)
        SetBool("game.disablepause", GetPlayerScreen() == PC_Screen)
        UI_CHAMPIONSHIP:draw(dt, UiWidth(), 794/900*UiHeight(), PAGE_STATE[UI_COMPUTER.page])
    end,
    ["CR"] = function(dt)
        SetBool("game.disablepause", GetPlayerScreen() == PC_Screen)
        UI_CR:draw(dt)
    end,
    ["MISSION_MENU"] = function(dt)
        SetBool("game.disablepause", GetPlayerScreen() == PC_Screen)
        UI_MISSION_MENU:draw(dt)
    end,
}

function UI_COMPUTER:draw(dt)
    self:drawBackground()
    UI_HEADER:draw(self.page)

    if case[self.page] then
        case[self.page](dt)
    end

    if not self.modal_trigger_time then return end
    local tag = GetTagValue(UiGetScreen(), "modal_after")
    for modal in tag:gmatch"[^;]+" do
        local trigger_delay, stage = modal:match"%w+,?([%d.]+),?(.*)"
        if stage == GetString("savegame.mod.tutorial_current_stage") and GetTime() - self.modal_trigger_time > tonumber(trigger_delay) then
            TriggerEvent(EVENTS.SHOW_MODAL)
            self.modal_trigger_time = nil
        end
    end
end

local case_dir = {
    MAIN_MENU = 0,
    CAREER = 1,
    CHAMPIONSHIP = 2,
    CR = 1,
    MISSION_MENU = 1,
}

function UI_COMPUTER:setPage(inPage, inContent)
    local old = case_dir[self.page]
    local new = case_dir[inPage]

    if old > new then
        UiSound("MOD/snd/level.ogg", 1, 0.9)
    else
        UiSound("MOD/snd/level.ogg", 1, 1.1)
    end

    self.page = inPage
    if inContent then
        PAGE_STATE[self.page] = inContent
    end

    if not GetBool("savegame.mod.tutorial_enabled") or GetBool("savegame.mod.tutorial.nitro") then return end
    local tag = GetTagValue(UiGetScreen(), "modal_after")
    for modal in tag:gmatch"[^;]+" do
        local page, stage = modal:match"(%w+),?[%d.]+,?(.*)"
        if page == inPage and stage == GetString("savegame.mod.tutorial_current_stage") then
            self.modal_trigger_time = self.modal_trigger_time or GetTime()
            break
        end
    end
end

function UI_COMPUTER:drawBackground()
    local pathBackground = "MOD/ui/pc/main_back.png"
    UiColor(1,1,1)
    UiImage(pathBackground)
end

function drawTextWithShadow(text)
    UiPush()
        UiPush()
            UiTranslate(2, 3)
            UiColor(1, 0.1953, 0.289)
            UiText(text)
        UiPop()
        UiText(text)
    UiPop()
end

function resizePict(path, width, height)
    UiPush()
        local w, h = UiGetImageSize(path)
        width = width or w
        height = height or h
        if height/h < width/w then
            local scale = width/w
            UiScale(scale)
            UiImage(path, 0, h-(height/scale), w, h)
        else
            local scale = height/h
            UiScale(scale)
            UiImage(path, w-(width/scale), 0, w, h)
        end
    UiPop()
end