-- Copyright (C) 2025  snoutie
-- Authors: snoutie (copyright@achtarmig.org)
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as published
-- by the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.

-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU Affero General Public License for more details.

-- You should have received a copy of the GNU Affero General Public License
-- along with this program.  If not, see <https://www.gnu.org/licenses/>.

local M = {}

M.dependencies = { 'career_modules_inventory', 'freeroam_facilities', "career_vehicleSaveSystem" }

local career_modules_inventory_removeVehicleObject
local vehicleObjectsToRemove = {}

local function spawnVehicle(inventoryId, callback)
    if career_modules_inventory.getVehicleIdFromInventoryId(inventoryId) then
        callback()
    else
        career_modules_inventory.spawnVehicle(inventoryId, nil, callback)
    end
end

local function Retrieve(inventoryId)
    extensions.core_jobsystem.create(
        function(job)
            spawnVehicle(inventoryId, function()
                local veh = be:getObjectByID(career_modules_inventory.getVehicleIdFromInventoryId(inventoryId))
                local location = { pos = veh:getPosition(), rot = quat(0, 0, 1, 0) * quat(veh:getRefNodeRotation()) }
                local garage = career_modules_inventory.getClosestGarage(location.pos)
                job.sleep(1)
                career_vehicleSaveSystem.LoadVehicle(inventoryId)
                freeroam_facilities.teleportToGarage(garage.id, veh, false)
            end)
        end
    )
end

local function onComputerAddFunctions(menuData, computerFunctions)
    if menuData.computerFacility.functions["vehicleInventory"] then
        local favouriteVehicleId = career_modules_inventory.getFavoriteVehicle()

        local computerFunctionData = {
            id = "retrieve_damaged",
            label = "Retrieve Favourite Damaged",
            callback = function() Retrieve(favouriteVehicleId) end,
            order = 1
        }

        if menuData.tutorialPartShoppingActive or menuData.tutorialTuningActive then
            computerFunctionData.disabled = true
            computerFunctionData.reason = career_modules_computer.reasons.tutorialActive
        end
        computerFunctions.general[computerFunctionData.id] = computerFunctionData
    end
end

local function RemoveVehicleObject(inventoryId)
    table.insert(vehicleObjectsToRemove, inventoryId)
    career_vehicleSaveSystem.QueueVehicleToSave(inventoryId)
    career_vehicleSaveSystem.SaveVehicle(inventoryId)
    career_vehicleSaveSystem.CheckSavedAsync(function()
        career_modules_inventory_removeVehicleObject(inventoryId)
    end)
end

local function onCareerActive()
    career_modules_inventory_removeVehicleObject = career_modules_inventory.removeVehicleObject
    career_modules_inventory.removeVehicleObject = RemoveVehicleObject
end

M.onComputerAddFunctions = onComputerAddFunctions
M.onCareerActive = onCareerActive

return M