#include "ui_carStore.lua"
#include "ui_carGarage.lua"
#include "ui_carWorkshop.lua"
#include "../../../config/platforms.lua"
#include "../../../config/events.lua"
#include "../../../config/input.lua"
#include "../../../script/helpers/color.lua"
#include "../../../script/vehicles_registry_methods.lua"
#include "../../../script/money.lua"
#include "../../../script/ui_game/ui_carSelector.lua"
#include "../computer/components/page_components.lua"
#include "../computer/components/ui_header.lua"
#include "components/ui_carStatus.lua"
#include "components/ui_carTitle.lua"

UI_CARMAIN = {}

local case = {
    ["garage"] = function(dt, carIndex, style_id, mod_id)
        return UI_CARGARAGE:draw(dt, carIndex, style_id, mod_id)
    end,
    ["store"] = function(dt, carIndex, style_id, mod_id)
        return UI_CARSTORE:draw(dt, carIndex, style_id, mod_id)
    end,
    ["workshop"] = function(dt, carIndex, style_id, mod_id)
        return UI_WORKSHOP:draw(dt, carIndex, style_id, mod_id)
    end,
}

function UI_CARMAIN:init(pPlaceId)
    if pPlaceId == "garage" then
        self.acquireSnd = LoadSound("valuable.ogg")
        RegisterListenerTo(EVENTS.UPDATE_GARAGE, "StartUpdate")
        RegisterListenerTo(EVENTS.SET_SPECIAL_VEH, "SetSpecialVeh")
        RegisterListenerTo(EVENTS.UI_SWITCH_CLASS, "SwitchClassGarage")
        UI_CARGARAGE:init()
    elseif pPlaceId == "store" then
        RegisterListenerTo(EVENTS.UI_SWITCH_CLASS, "SwitchClassStore")
        UI_CARSTORE:init()
    elseif pPlaceId == "workshop" then
        UI_WORKSHOP:init()
    end
end

function StartUpdate()
    PlaySound(UI_CARMAIN.acquireSnd)
    UI_CARGARAGE.updateGarage = true
end

function SetSpecialVeh(forced_spawn)
    forced_spawn = forced_spawn == "true"
    local self = UI_CARGARAGE
    self.PostInit = false
    local id = vehiclesRegistry:LoadGarageVehicle()
    local class = VEHICLES_REGISTRY[id].class
    if not self.PostInit then
        self:drawInit("ladoga")
    end
    if self.updateGarage then
        self:update()
    end
    self:InitCarSelector()
    self:UpdateClasses(class)
    self:UpdateShowCarList()
    UI_CARSELECTOR:SetNewActiveCar(self:FindNewActiveCar())
    if HasKey("level.carlift_obstacle") or forced_spawn then
        DeleteCurrentVeh()
        if forced_spawn then
            SpawnNewVehicle(vehiclesRegistry:LoadVehicle(id))
        else
            self.updateSpawnCar = false
        end
        self.waitUpdateSpawnCar = function() return not HasKey("level.carlift_obstacle") and (SpawnNewVehicle(vehiclesRegistry:LoadVehicle(id)) or true) end
    end
end

function SwitchClassGarage(data)
    if not data then return end

    local garage, class = data:match("(garage);(.+)")
    if not garage then return end

    local self = UI_CARGARAGE

    local garage_vehicle
    if class == "current" then
        garage_vehicle = VEHICLES_REGISTRY[GetString("savegame.mod.available_vehicles.garage_vehicle"):match("[%w_]+")]
        class = garage_vehicle and garage_vehicle.class or class
    end
    if not self.PostInit then
        self:drawInit("ladoga")
    end
    self:update()
    self:InitCarSelector()
    self:UpdateClasses(class)
    self:UpdateShowCarList()
    UI_CARSELECTOR:SetNewActiveCar(self:FindNewActiveCar())
    if not garage_vehicle and HasKey("level.carlift_obstacle") then
        DeleteCurrentVeh()
        self.updateSpawnCar = false
        self.waitUpdateSpawnCar = function() return not HasKey("level.carlift_obstacle") end
        TriggerEvent(EVENTS.UI_EFFECT, "fade;delay=0.5;duration=1.5;pre=0.4;post=0.6")
    end
end

function SwitchClassStore(data)
    if not data then return end

    local store, class = data:match("(store);(.+)")
    if not store then return end

    local self = UI_CARSTORE
    self:draw(GetTimeStep(), "ladoga", nil, 1)
    self:UpdateClasses(class)
end

function UI_CARMAIN:draw(dt, pPlaceId, carIndex, style_id, mod_id)
    UiMakeInteractive()
    if case[pPlaceId] and case[pPlaceId](dt, carIndex, style_id, mod_id) ~= nil and not isUseDpadInput() then
        UiSetCursorState(UI_CURSOR_HIDE)
        UiPush()
            local x = GetInt("level.pc_x_pos")
            local y = GetInt("level.pc_y_pos")
            UiTranslate(x, y)
            UiImage("gfx/cursor.png")
        UiPop()
    end

    if INPUT.LOBBY_CANCEL() and pPlaceId then
        TriggerEvent(EVENTS.UI_COMPUTER_HOME_BTN, pPlaceId)
    end
end
