local _, addonTable = ...
local WipedQoL = addonTable.WipedQoL
local TRANSLATE = addonTable.TRANSLATE or function(msg) return msg end

local LDBIcon = LibStub("LibDBIcon-1.0", true)

local function ClampColorComponent(value, fallback)
    return math.max(0, math.min(1, tonumber(value) or fallback))
end

local function GetLegacyCustomColor(profile)
    local modules = profile and profile.modules
    if type(modules) ~= "table" then
        return nil
    end

    local topAccent = modules.topAccentBar
    if type(topAccent) == "table" and topAccent.colorMode == "custom" and type(topAccent.color) == "table" then
        return topAccent.color
    end

    local raidLoot = modules.raidLootHistory
    if type(raidLoot) == "table" and raidLoot.timerColorMode == "custom" and type(raidLoot.timerColor) == "table" then
        return raidLoot.timerColor
    end

    return nil
end

function WipedQoL:GetAddonColorSettings()
    local profile = self.db and self.db.profile
    if type(profile) ~= "table" then
        return nil
    end

    if type(profile.addonColor) ~= "table" then
        profile.addonColor = {}
    end

    local color = profile.addonColor
    local fallbackR, fallbackG, fallbackB = 1, 0.82, 0
    if addonTable.GetPlayerClassColor then
        fallbackR, fallbackG, fallbackB = addonTable.GetPlayerClassColor()
    end

    local legacyColor = GetLegacyCustomColor(profile)
    if color.mode ~= "class" and color.mode ~= "custom" then
        color.mode = "class"
    end
    color.r = ClampColorComponent(color.r, legacyColor and legacyColor.r or fallbackR)
    color.g = ClampColorComponent(color.g, legacyColor and legacyColor.g or fallbackG)
    color.b = ClampColorComponent(color.b, legacyColor and legacyColor.b or fallbackB)
    return color
end

function WipedQoL:GetAddonColorMode()
    local color = self:GetAddonColorSettings()
    return color and color.mode or "class"
end

function WipedQoL:GetAddonCustomColor()
    local color = self:GetAddonColorSettings()
    if color then
        return color.r, color.g, color.b
    end
    return 1, 0.82, 0
end

function WipedQoL:GetAddonColor()
    local color = self:GetAddonColorSettings()
    if color and color.mode == "custom" then
        return color.r, color.g, color.b
    end

    if addonTable.GetPlayerClassColor then
        return addonTable.GetPlayerClassColor()
    end
    return 1, 0.82, 0
end

local function RefreshAddonColor(self)
    if addonTable.RefreshAddonColorTargets then
        addonTable.RefreshAddonColorTargets()
    end
    if self.RefreshTopAccentBars then
        self:RefreshTopAccentBars()
    end
    if self.ApplyRaidLootHistoryTimerColor then
        self:ApplyRaidLootHistoryTimerColor()
    end
    if self.RefreshDatapanel then
        self:RefreshDatapanel()
    end
    if self.RefreshExpansionVendorControls then
        self:RefreshExpansionVendorControls()
    end
    if self.RefreshAchievementNavigationAppearance then
        self:RefreshAchievementNavigationAppearance()
    end
    if self.RefreshAddonColorUI then
        self:RefreshAddonColorUI()
    end
end

function WipedQoL:SetAddonColorMode(mode)
    local color = self:GetAddonColorSettings()
    if not color then
        return
    end

    color.mode = mode == "custom" and "custom" or "class"
    RefreshAddonColor(self)
end

function WipedQoL:SetAddonColor(r, g, b)
    local color = self:GetAddonColorSettings()
    if not color then
        return
    end

    color.r = ClampColorComponent(r, color.r)
    color.g = ClampColorComponent(g, color.g)
    color.b = ClampColorComponent(b, color.b)
    RefreshAddonColor(self)
end

function WipedQoL:IsMinimapHidden()
    return self.db and self.db.profile and self.db.profile.minimap and self.db.profile.minimap.hide == true
end

function WipedQoL:SetMinimapHidden(hidden)
    if not self.db or not self.db.profile then
        return
    end

    self.db.profile.minimap.hide = hidden and true or false

    if LDBIcon then
        if self.db.profile.minimap.hide then
            LDBIcon:Hide("WipedQoL")
        else
            LDBIcon:Show("WipedQoL")
        end
    end

    self:PrintDebug(self.db.profile.minimap.hide and TRANSLATE("Minimap button hidden.") or TRANSLATE("Minimap button shown."))
end
