-- Warn on every savegame start when FS25_VisualMudTracks is active.
-- The mods overlap in wheel pressure, grip, sink and stuck physics. No external
-- setting is changed automatically and no compatibility behavior is installed.

VisualMudTracksConflictDialog = {}
VisualMudTracksConflictDialog.MOD_NAMES = {
    "FS25_VisualMudTracks_v25",
    "FS25_VisualMudTracks",
    "FS25_VisualMudTracks_v25_MudPressureIntegration"
}

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

    if g_i18n ~= nil and g_i18n.getText ~= nil then
        local value = g_i18n:getText(key)
        if value ~= nil and value ~= key then
            return value
        end
    end

    return fallback or key
end

function VisualMudTracksConflictDialog:isVisualMudTracksActive()
    for _, modName in ipairs(self.MOD_NAMES) do
        if g_modIsLoaded ~= nil and g_modIsLoaded[modName] then
            return true, modName
        end
    end

    local mission = g_currentMission
    local dynamicInfo = (mission ~= nil and mission.missionDynamicInfo)
        or (g_mpLoadingScreen ~= nil and g_mpLoadingScreen.missionDynamicInfo)
    local mods = dynamicInfo ~= nil and dynamicInfo.mods or nil

    if mods ~= nil then
        for _, modInfo in pairs(mods) do
            if modInfo ~= nil then
                for _, modName in ipairs(self.MOD_NAMES) do
                    if modInfo.modName == modName then
                        return true, modName
                    end
                end
            end
        end
    end

    if rawget(_G, "VisualMudTracks") ~= nil
        or rawget(_G, "VMTTirePressureSystem") ~= nil then
        return true, "runtime"
    end

    return false, nil
end

function VisualMudTracksConflictDialog:isClient()
    local mission = g_currentMission
    if mission ~= nil and mission.getIsClient ~= nil then
        return mission:getIsClient()
    end

    return g_gui ~= nil
end

function VisualMudTracksConflictDialog:showDialog(onComplete)
    local text = vmtConflictGetText(
        "mudsys_vmt_conflict_text",
        "FS25_VisualMudTracks was detected. Both mods control the same game physics parameters: tire pressure, traction, wheel sink and getting stuck. Running them together is not supported and may cause duplicated physics, unstable vehicle behavior and other problems. Disable one of the mods to avoid problems."
    )
    local okText = vmtConflictGetText("mudsys_vmt_conflict_ok", "OK")

    local function closeNotice()
        if onComplete ~= nil then
            onComplete()
        end
    end

    if InfoDialog ~= nil
        and InfoDialog.show ~= nil
        and InfoDialog.INSTANCE ~= nil then
        InfoDialog.show(
            text,
            closeNotice,
            nil,
            DialogElement ~= nil and DialogElement.TYPE_WARNING or nil,
            okText
        )
        return true
    end

    Logging.warning("[MudSystemPhysics] %s", text)
    closeNotice()
    return false
end

function VisualMudTracksConflictDialog:loadMap()
    if not self:isClient() then
        return
    end

    local active, detectedAs = self:isVisualMudTracksActive()
    if not active then
        return
    end

    Logging.warning(
        "[MudSystemPhysics] Conflicting FS25_VisualMudTracks detected (%s)",
        tostring(detectedAs)
    )

    if MudSystemDialogQueue ~= nil
        and MudSystemDialogQueue.enqueue ~= nil then
        MudSystemDialogQueue:enqueue(
            "visualMudTracksConflict",
            95,
            function(onComplete)
                return VisualMudTracksConflictDialog:showDialog(onComplete)
            end
        )
    else
        self:showDialog(nil)
    end
end

addModEventListener(VisualMudTracksConflictDialog)
