#include "gamemodes.lua"
#include "../script/vehicles_registry_methods.lua"
#include "../script/helpers/utils.lua"

local function enablePC()
    local pc = GetInt("level.screen")
    if pc == 0 then pc = FindScreen("pc", true) end
    if pc == 0 then return end -- player has done something with pc, maybe it was deleted

    SetTag(GetShapeBody(GetScreenShape(pc)), "interact")
    SetScreenEnabled(pc, true)
end
local function disablePC()
    local pc = GetInt("level.screen")
    if pc == 0 then pc = FindScreen("pc", true) end
    if pc == 0 then return end -- player has done something with pc, maybe it was deleted

    RemoveTag(GetShapeBody(GetScreenShape(pc)), "interact")
    SetScreenEnabled(pc, false)
end

Missions = OrderedTable()
Missions["gasstation"] =
{
    index = 1,
    stage_activation_delay = 2,
    stage = "gasstation",
    steps = {
        "use_lightswitcher",
        "leave_gasstation",
        "use_lightswitchergas",
        "use_car",
        "leave_parking",
        "enter_workshop",
    },
    step_hints = {
        "lobby=loc@TURN_OFF_LIGHTS",
        "lobby=loc@GO_OUTSIDE",
        "lobby=loc@TURN_OFF_LIGHTS_OUTSIDE",
        "lobby=loc@LEAVE_GAS_STATION",
        "lobby=loc@LEAVE_GAS_STATION",
        "lobby=loc@ENTER_WORKSHOP",
    },
    stage_postcall = function() SetPlayerVelocity({}) end,
}
Missions["registration"] =
{
    index = 2,
    stage = "registration",
    stage_activate_conditions = function() return GetPlayerVehicle() == 0 end,
    -- stage_activation_delay = 1.5,
    stage_activation_delay = 0,
    initial_step_delay = 1,
    steps = {
        "use_pc",
        "complete_race",
    },
    step_hints = {
        "lobby=loc@USE_PC",
    },
}
Missions["upgrade_car"] =
{
    index = 3,
    stage = "upgrade_car",
    stage_precall = ToggleActiveInteracts,
    stage_activate_conditions = function()
        if GetBool("level.is_lobby") then
            if not HasKey("savegame.mod.upgrade_car_completed_races") then
                local n = 0
                for i = 1, #CAMPAIGN do
                    local c = CAMPAIGN[i]
                    for j = 1, #c.Levels do
                        if GetInt("savegame.mod.campaign"..i..".level"..j..".starsReceived") > 0 then
                            n = n+1
                        end
                    end
                end
                SetInt("savegame.mod.upgrade_car_completed_races", n)
            end
            return true
        end
        return
    end,
    stage_postcall = function() ToggleActiveInteracts(true) end,
    steps = {
        "use_pc",
        "buy_ladoga_styles",
    },
    step_hints = {
        "lobby=loc@USE_PC",
    },
}
Missions["garage_features"] =
{
    index = 4,
    stage_activate_conditions = function() return GetBool("game.player.interactive") end,
    stage_activation_delay = 1,
    stage = "garage_features",
    steps = {
        "use_pc",
        "win_campaign_any",
    },
    step_hints = {
        "lobby=loc@CONTINUE_PARTICIPATING",
    },
    stage_precall = disablePC,
    step1_precall = enablePC,
}
Missions["nitro"] =
{
    index = 5,
    stage = "nitro",
    stage_activate_conditions = function()
        if GetBool("level.is_lobby") then
            if not HasKey("savegame.mod.nitro_completed_races") then
                local n = 0
                for i = 1, #CAMPAIGN do
                    local c = CAMPAIGN[i]
                    for j = 1, #c.Levels do
                        if GetInt("savegame.mod.campaign"..i..".level"..j..".starsReceived") > 0 then
                            n = n+1
                        end
                    end
                end
                SetInt("savegame.mod.nitro_completed_races", n)
            end
            return true
        else
            return
        end
    end,
    stage_precall = disablePC,
    stage_postcall = enablePC,
    steps = {
        "use_terminal",
        "exit_terminal",
    },
    step_hints = {
        "lobby=loc@USE_TERMINAL",
    },
}
Missions["won_championship_9"] =
{
    index = 6,
    stage = "won_championship_9",
    stage_activate_conditions = function() return GetInt("savegame.mod.campaign15")>0 end,
    steps = {
        "use_endingsequence",
    },
    step_hints = {
        "lobby=loc@GET_YOUR_REWARD",
    },
    stage_complete_callback = function()
        local ending_after = gFrLevels.lobby_4
        vehiclesRegistry:OpenNewVehicle("baab", 1, 1)
        vehiclesRegistry:SaveGarageVehicle("baab", 1, 1)
        StartLevel(ending_after.id, "MOD/"..ending_after.path, ending_after.bake_layers)
    end,
}

for k, v in pairs(Missions) do
    for j, l in pairs(v) do
        if type(l) == "function" then
            if not _G[j] then _G[j] = {} end
            _G[j][k] = l
        end
    end
end
