---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by my phus cyaches.
--- DateTime: 9/21/2022 9:05 PM
--- Taken from original PZcode removeburntvehicle in vehicle lua


require "Vehicles/ISUI/ISVehicleMenu"
require "luautils"

local enableSalvage = ISVehicleMenu.FillMenuOutsideVehicle

local function predicateWeldingMask(item)
    return item:hasTag("WeldingMask") or item:getType() == "WeldingMask"
end

local function predicateBlowTorch(item)
    return (item ~= nil) and
            (item:hasTag("BlowTorch") or item:getType() == "BlowTorch") and
            (item:getDrainableUsesInt() >= 10)
end

local function valhallaclaimed(vehicle)
    ---Thanks Udderly for letting me poke around your code for ideas---
    if not (Valhalla and Valhalla.VehicleClaims) then
        return false
    end

    local claimed = nil

    if Valhalla:getVehicleModData(vehicle) then
        claimed = Valhalla:getVehicleModData(vehicle)
    else
        return false
    end

    if claimed then
        local owner = claimed["owner"]
        if owner and owner ~= "" then
            return claimed["owner"]
        end
    end
end

function ISVehicleMenu.onVehicleSalvage(player, vehicle)
    if luautils.walkAdj(player, vehicle:getSquare()) then
        ISWorldObjectContextMenu.equip(player, player:getPrimaryHandItem(), predicateBlowTorch, true);
        local mask = player:getInventory():getFirstEvalRecurse(predicateWeldingMask);
        if mask then
            ISInventoryPaneContextMenu.wearItem(mask, player:getPlayerNum());
        end
        ISTimedActionQueue.add(ISVehicleSalvage:new(player, vehicle));
    end
end

function ISVehicleMenu.FillMenuOutsideVehicle(player, context, vehicle, test)

    local playerObj = getSpecificPlayer(player)
    local MWSkill = 4
    local MWSkillDisplay = playerObj:getPerkLevel(Perks.MetalWelding)
    local MecSkill = 3
    local MecSkillDisplay = playerObj:getPerkLevel(Perks.Mechanics)
    local isClaimed = valhallaclaimed(vehicle)
    --[[
        local subOption = context:addOption("Vehicle")
        local vehicleMenu = ISContextMenu:getNew(context)
        context:addSubMenu(subOption, vehicleMenu)
        vehicleMenu:addOption("Info", playerObj, ISVehicleMenu.onInfo, vehicle)
    --]]
    --	context:addOption("Vehicle Info", playerObj, ISVehicleMenu.onInfo, vehicle)

    local option = nil
    --- remove Salvage Vehicles options ---
    if SandboxVars.VehicleRepairOverhaul.DisableVehicleSalvage == true or string.match(vehicle:getScript():getName(), "Burnt") or isClaimed then return enableSalvage(player, context, vehicle, test) end;

    ---Salvage complete vehicles add option to menu---
        option = context:addOption(getText("ContextMenu_SalvageVehicle"), playerObj, ISVehicleMenu.onVehicleSalvage, vehicle);
        local toolTip = ISToolTip:new();
        toolTip:initialise();
        toolTip:setVisible(false);
        option.toolTip = toolTip;
        toolTip:setName(getText("ContextMenu_SalvageVehicle"));
        toolTip.description = getText("Tooltip_SalvageVehicle") .. " <LINE> <LINE> " .. getText("Tooltip_craft_Needs") .. " : <LINE> ";

        if playerObj:getPerkLevel(Perks.MetalWelding) >= MWSkill then
            toolTip.description = toolTip.description .. " <LINE> <RGB:1,1,1> " .. getText("IGUI_perks_MetalWelding") .. " " .. MWSkillDisplay .. "/" .. 4;
        else
            toolTip.description = toolTip.description .. " <LINE> <RGB:1,0,0> " .. getText("IGUI_perks_MetalWelding") .. " " .. MWSkillDisplay .. "/" .. 4;
            option.notAvailable = true;
        end

        if playerObj:getPerkLevel(Perks.Mechanics) >= MecSkill then
            toolTip.description = toolTip.description .. " <LINE> <RGB:1,1,1> " .. getText("IGUI_perks_Mechanics") .. " " .. MecSkillDisplay .. "/" .. 3;
        else
            toolTip.description = toolTip.description .. " <LINE> <RGB:1,0,0> " .. getText("IGUI_perks_Mechanics") .. " " .. MecSkillDisplay .. "/" .. 3;
            option.notAvailable = true;
        end

        if playerObj:getInventory():containsEvalRecurse(predicateWeldingMask) then
            toolTip.description = toolTip.description .. " <LINE> <RGB:1,1,1> " .. getItemNameFromFullType("Base.WeldingMask") .. " 1/1";
        else
            toolTip.description = toolTip.description .. " <LINE> <RGB:1,0,0> " .. getItemNameFromFullType("Base.WeldingMask") .. " 0/1";
            option.notAvailable = true;
        end

---@diagnostic disable-next-line: undefined-global
        local blowTorch = ISBlacksmithMenu.getBlowTorchWithMostUses(playerObj:getInventory());
        if blowTorch then
            local blowTorchUseLeft = blowTorch:getDrainableUsesInt();
            if blowTorchUseLeft >= 10 then
                toolTip.description = toolTip.description .. " <LINE> <RGB:1,1,1> " .. getItemNameFromFullType("Base.BlowTorch") .. " " .. getText("ContextMenu_Uses") .. " " .. blowTorchUseLeft .. "/" .. 10;
            else
                toolTip.description = toolTip.description .. " <LINE> <RGB:1,0,0> " .. getItemNameFromFullType("Base.BlowTorch") .. " " .. getText("ContextMenu_Uses") .. " " .. blowTorchUseLeft .. "/" .. 10;
                option.notAvailable = true;
            end
        else
            toolTip.description = toolTip.description .. " <LINE> <RGB:1,0,0> " .. getItemNameFromFullType("Base.BlowTorch") .. " 0/10";
            option.notAvailable = true;
        end

    enableSalvage(player, context, vehicle, test)

end