MudExtraParticlePresets = {}

MudExtraParticlePresets.PRESETS = {
    { id = 1, key = "mud",       file = "particleSystems/mud.i3d",       l10n = "mudsys_extraParticlePreset_standard", fallback = "Standard" },
    { id = 2, key = "mudField",  file = "particleSystems/mudField.i3d",  l10n = "mudsys_extraParticlePreset_dark",     fallback = "Dark standard" },
    { id = 3, key = "mudField2", file = "particleSystems/mudField2.i3d", l10n = "mudsys_extraParticlePreset_small",    fallback = "Small particles" },
    { id = 4, key = "mudField3", file = "particleSystems/mudField3.i3d", l10n = "mudsys_extraParticlePreset_medium",        fallback = "Medium particles" },
    { id = 5, key = "mudField4", file = "particleSystems/mudField4.i3d", l10n = "mudsys_extraParticlePreset_mediumHeavy",   fallback = "Medium heavy particles" }
}

local function getTextSafe(key, fallback)
    if g_i18n ~= nil and key ~= nil and g_i18n.hasText ~= nil and g_i18n:hasText(key) then
        return g_i18n:getText(key)
    end

    return fallback or key or ""
end

local function safeDeleteNode(node)
    if node == nil or node == 0 then
        return
    end

    if type(mpSafeDelete) == "function" then
        mpSafeDelete(node)
    elseif type(delete) == "function" then
        delete(node)
    end
end

function MudExtraParticlePresets.getPresetById(id)
    id = math.floor(tonumber(id) or 1)

    for _, preset in ipairs(MudExtraParticlePresets.PRESETS) do
        if preset.id == id then
            return preset
        end
    end

    return MudExtraParticlePresets.PRESETS[1]
end

function MudExtraParticlePresets.getSettingValues()
    local values = {}

    for _, preset in ipairs(MudExtraParticlePresets.PRESETS) do
        values[#values + 1] = preset.id
    end

    return values
end

function MudExtraParticlePresets.getSettingStrings()
    local strings = {}

    for _, preset in ipairs(MudExtraParticlePresets.PRESETS) do
        strings[#strings + 1] = getTextSafe(preset.l10n, preset.fallback)
    end

    return strings
end

function MudExtraParticlePresets.clearCache(target)
    if target == nil or target._extraPS == nil then
        return
    end

    if target._extraPS.referencePS ~= nil then
        safeDeleteNode(target._extraPS.referencePS.shape)
        target._extraPS.referencePS = nil
    end

    if target._extraPS.referenceShape ~= nil then
        safeDeleteNode(target._extraPS.referenceShape)
        target._extraPS.referenceShape = nil
    end

    target._extraPS.sourcePsRoot = nil
    target._extraPS.sourceShapeRoot = nil
    target._extraPS.sourcePsNode = nil
    target._extraPS = nil
end

function MudExtraParticlePresets.applyPreset(target, presetId)
    if target == nil then
        return nil
    end

    local preset = MudExtraParticlePresets.getPresetById(presetId or target.extraParticlesPreset)
    target.extraParticlesPreset = preset.id
    target.extraParticlesI3D = preset.file

    return preset
end

function MudExtraParticlePresets.applyMudPhysics(target)
    return MudExtraParticlePresets.applyPreset(target or rawget(_G, "MudPhysics"))
end

function MudExtraParticlePresets.applyFieldGroundMudPhysics(target)
    return MudExtraParticlePresets.applyPreset(target or rawget(_G, "FieldGroundMudPhysics"))
end
