require "MS/MS_StatusIndicator"

local isInitialized = false
local _isTABASModActive = nil

local function detectTABASMod()
    if _isTABASModActive ~= nil then
        return _isTABASModActive
    end

    local activatedMods = getActivatedMods()
    for i = 0, activatedMods:size() - 1 do
        local modId = tostring(activatedMods:get(i) or "")
        if modId == "TakeABathAndShowerNew" or modId == "TakeABathAndShower42" then
            _isTABASModActive = true
            return true
        end
    end

    _isTABASModActive = false
    return false
end

local function getTABASBodyGrime(player)
    if not player then return 0 end
    local md = player:getModData()
    if not md then return 0 end

    local value = md.tabas_BodyGrime
    if value == nil then
        value = md.BodyGrime
    end
    value = tonumber(value) or 0
    if value < 0 then value = 0 end
    if value > 100 then value = 100 end
    return value
end

ISTABASBodyGrimeStatusIndicator = MS_StatusIndicator:derive("ISTABASBodyGrimeStatusIndicator")

function ISTABASBodyGrimeStatusIndicator:new(x, y, width, height, player)
    local o = MS_StatusIndicator.new(self, x, y, width, height, player)
    o.baseIconName = "WORDirtiness"
    o.__type = "ISTABASBodyGrimeStatusIndicator"
    o.indicatorColor = {r=0.65, g=0.45, b=0.2}
    return o
end

function ISTABASBodyGrimeStatusIndicator:getValue()
    if not self.player or self.player:isDead() then return 0 end
    return getTABASBodyGrime(self.player) / 100
end

local function integrateToModernStatus()
    if isInitialized then return end
    if not detectTABASMod() then return end

    MS_IndicatorSettingsPanel.IndicatorNames["ISTABASBodyGrimeStatusIndicator"] = "IGUI_ModernStatus_BodyGrime"

    local oldCreateUI = StatusWidget.createUI
    StatusWidget.createUI = function(playerIndex, player)
        oldCreateUI(playerIndex, player)

        if not StatusWidget.indicators[playerIndex] then
            StatusWidget.indicators[playerIndex] = {}
        end

        if StatusWidget.indicators[playerIndex]["ISTABASBodyGrimeStatusIndicator"] then
            return
        end

        local config = MSConfig.getIndicatorConfig(playerIndex, "ISTABASBodyGrimeStatusIndicator")
        local x = config.position.x
        local y = config.position.y
        local width, height
        if config.style == "bar" then
            width = config.barSize.width
            height = config.barSize.height
        else
            width = config.circularSize
            height = config.circularSize
        end

        local indicator = ISTABASBodyGrimeStatusIndicator:new(x, y, width, height, player)
        indicator:initialise()
        indicator:addToUIManager()
        StatusWidget.indicators[playerIndex]["ISTABASBodyGrimeStatusIndicator"] = indicator
    end

    if not MSConfig._originalCreateDefaultConfig_TABASBodyGrime then
        MSConfig._originalCreateDefaultConfig_TABASBodyGrime = MSConfig.createDefaultConfig
        MSConfig.createDefaultConfig = function(playerNum)
            local config = MSConfig._originalCreateDefaultConfig_TABASBodyGrime(playerNum)
            if detectTABASMod() and not config.indicators["ISTABASBodyGrimeStatusIndicator"] then
                config.indicators["ISTABASBodyGrimeStatusIndicator"] = MSConfig.getDefaultIndicatorConfig("ISTABASBodyGrimeStatusIndicator")
                config.indicators["ISTABASBodyGrimeStatusIndicator"].color = {r=0.65, g=0.45, b=0.2}
                config.indicators["ISTABASBodyGrimeStatusIndicator"].animationThreshold = 25
            end
            return config
        end
    end

    isInitialized = true
end

Events.OnGameBoot.Add(integrateToModernStatus)

return {
    isActive = detectTABASMod
}
