-- Warn about running Mud System tire pressure together with FS25_VariableTirePressure.

TirePressureConflictDialog = {}
TirePressureConflictDialog.MOD_NAME = "FS25_VariableTirePressure"
TirePressureConflictDialog.delayMs = 3000
TirePressureConflictDialog._pending = false
TirePressureConflictDialog._shown = false

local function tpcGetText(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 text = g_i18n:getText(key)
        if text ~= nil and text ~= key then
            return text
        end
    end

    return fallback or key
end

function TirePressureConflictDialog:loadMap()
    self._pending = true
    self._shown = false
    self._queued = false
    self._delay = 100
end

function TirePressureConflictDialog:deleteMap()
    self._pending = false
    self._shown = false
    self._queued = false
end

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

    return g_gui ~= nil
end

function TirePressureConflictDialog:isVariableTirePressureActive()
    local modName = self.MOD_NAME

    if g_modIsLoaded ~= nil and g_modIsLoaded[modName] then
        return true, "g_modIsLoaded"
    end

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

    if mods ~= nil then
        for _, modInfo in pairs(mods) do
            if modInfo ~= nil and modInfo.modName == modName then
                return true, "missionDynamicInfo.mods"
            end
        end
    end

    if rawget(_G, "FS25_VTP_Global") ~= nil
        or rawget(_G, "FS25_VariableTirePressure") ~= nil
        or rawget(_G, "VariableTirePressure") ~= nil then
        return true, "global"
    end

    return false, nil
end

function TirePressureConflictDialog:getTirePressureSettingItem()
    if MudSystemSettings == nil or MudSystemSettings.items == nil then
        return nil
    end

    for _, item in ipairs(MudSystemSettings.items) do
        if item.id == "tp_enabled" then
            return item
        end
    end

    return nil
end

function TirePressureConflictDialog:disableMudSystemTirePressure()
    local item = self:getTirePressureSettingItem()
    if MudSystemSettings ~= nil and item ~= nil and MudSystemSettings.applyOneItemValue ~= nil then
        MudSystemSettings:applyOneItemValue(item, false, true)

        if MudSystemSettings.getIsAdminUser ~= nil and MudSystemSettings:getIsAdminUser()
            and MudSystemSettingsSyncEvent ~= nil and MudSystemSettingsSyncEvent.sendSnapshot ~= nil
            and MudSystemSettings.collectSnapshot ~= nil then
            MudSystemSettingsSyncEvent.sendSnapshot(MudSystemSettings:collectSnapshot())
        end
    else
        if TirePressureSystem ~= nil then
            TirePressureSystem.enabled = false
            if TirePressureSystem.onSettingsChanged ~= nil then
                TirePressureSystem:onSettingsChanged()
            end
        end

        if MudSystemSettings ~= nil and MudSystemSettings.writeSettings ~= nil then
            MudSystemSettings:writeSettings()
        end
    end

    print("[MudSystemPhysics] Tire pressure mode disabled because FS25_VariableTirePressure is active")
end

function TirePressureConflictDialog:showDialog(onComplete)
    if self._shown == true then
        return
    end

    if TirePressureSystem == nil or TirePressureSystem.enabled ~= true then
        return false
    end

    local active, via = self:isVariableTirePressureActive()
    if not active then
        return false
    end

    self._shown = true
    print(string.format("[MudSystemPhysics] FS25_VariableTirePressure conflict detected (%s)", tostring(via)))

    local title = tpcGetText("mudsys_tp_conflict_title", "Tire pressure conflict")
    local text = tpcGetText(
        "mudsys_tp_conflict_text",
        "FS25_VariableTirePressure is active in this savegame. To avoid strong conflicts, disable the Mud System tire pressure mode or disable FS25_VariableTirePressure."
    )
    local yesText = tpcGetText("mudsys_tp_conflict_disable", "Disable mode")
    local noText = tpcGetText("mudsys_tp_conflict_ok", "OK")

    if YesNoDialog ~= nil
        and YesNoDialog.show ~= nil
        and YesNoDialog.INSTANCE ~= nil then
        YesNoDialog.show(function(yes)
            if yes then
                TirePressureConflictDialog:disableMudSystemTirePressure()
            end
            if onComplete ~= nil then
                onComplete()
            end
        end, nil, text, title, yesText, noText, DialogElement ~= nil and DialogElement.TYPE_WARNING or nil)
        return true
    elseif InfoDialog ~= nil
        and InfoDialog.show ~= nil
        and InfoDialog.INSTANCE ~= nil then
        InfoDialog.show(text, onComplete)
        return true
    end

    return false
end

function TirePressureConflictDialog:update(dt)
    if self._pending ~= true or self._queued == true then
        return
    end

    if not self:isClient() or g_gui == nil then
        self._pending = false
        return
    end

    self._delay = (self._delay or 0) - (dt or 0)
    if self._delay <= 0 then
        self._pending = false
        local active = self:isVariableTirePressureActive()
        if TirePressureSystem ~= nil
            and TirePressureSystem.enabled == true
            and active then
            self._queued = true
            if MudSystemDialogQueue ~= nil
                and MudSystemDialogQueue.enqueue ~= nil then
                MudSystemDialogQueue:enqueue(
                    "tirePressureConflict",
                    100,
                    function(onComplete)
                        return TirePressureConflictDialog:showDialog(
                            onComplete
                        )
                    end
                )
            else
                self:showDialog(nil)
            end
        end
    end
end

addModEventListener(TirePressureConflictDialog)
