require "Vehicles/VehicleDistributions"
require "Items/ProceduralDistributions"

VFEDistro = {}

function VFEDistro.Insert(baseItem, chance, newItem)
    local script = ScriptManager.instance:getItem(baseItem)
    if not script then return end
    for _, lootTable in pairs({ ProceduralDistributions.list, VehicleDistributions, SuburbsDistributions, BagsAndContainers }) do
        for _, data in pairs(lootTable) do
            if data.items then
                for i, item in pairs(data.items) do
                    if item == script:getName() or item == script:getFullName() then
                        table.insert(data.items, newItem)
                        table.insert(data.items, data.items[i + 1] * (chance or 1))
                    end
                end
            end
        end
    end
end

function VFEDistro.InsertMany(baseItem, chance, ...)
    for _, item in ipairs({ ... }) do
        VFEDistro.Insert(baseItem, chance, item)
    end
end

function VFEDistro.RemoveEverywhere(item)
    local script = ScriptManager.instance:getItem(item)
    local a, b = item, nil
    if script then a, b = script:getName(), script:getFullName() end

    for _, lootTable in pairs({ ProceduralDistributions.list, VehicleDistributions, SuburbsDistributions, BagsAndContainers }) do
        for _, data in pairs(lootTable) do
            if data.items then
                for i = #data.items - 1, 1, -2 do
                    if data.items[i] == a or data.items[i] == b then
                        table.remove(data.items, i + 1)
                        table.remove(data.items, i)
                    end
                end
            end
        end
    end
end

function VFEDistro.RemoveMany(...)
    for _, item in ipairs({ ... }) do
        VFEDistro.RemoveEverywhere(item)
    end
end

function VFEDistro.SetChance(item, chance)
    local script = ScriptManager.instance:getItem(item)
    local a, b = item, nil
    if script then a, b = script:getName(), script:getFullName() end

    for _, lootTable in pairs({ ProceduralDistributions.list, VehicleDistributions, SuburbsDistributions, BagsAndContainers }) do
        for _, data in pairs(lootTable) do
            if data.items then
                for i = 1, #data.items - 1, 2 do
                    if data.items[i] == a or data.items[i] == b then
                        data.items[i + 1] = chance
                    end
                end
            end
        end
    end
end

function VFEDistro.SetChanceMany(chance, ...)
    for _, item in ipairs({ ... }) do
        VFEDistro.SetChance(item, chance)
    end
end
