local _, addonTable = ...
local WipedQoL = addonTable.WipedQoL

local WHITE_TEXTURE = "Interface\\Buttons\\WHITE8X8"

local function SetOnePhysicalPixelHeight(region)
    if not region then
        return
    end

    local effectiveScale = region.GetEffectiveScale and region:GetEffectiveScale() or 1
    effectiveScale = tonumber(effectiveScale) or 1
    if effectiveScale <= 0 then
        effectiveScale = 1
    end

    local pixelToUIUnitFactor
    if PixelUtil and PixelUtil.GetPixelToUIUnitFactor then
        pixelToUIUnitFactor = PixelUtil.GetPixelToUIUnitFactor()
    elseif GetPhysicalScreenSize then
        local _, physicalHeight = GetPhysicalScreenSize()
        physicalHeight = tonumber(physicalHeight) or 0
        if physicalHeight > 0 then
            pixelToUIUnitFactor = 768 / physicalHeight
        end
    end

    pixelToUIUnitFactor = tonumber(pixelToUIUnitFactor)
    if pixelToUIUnitFactor and pixelToUIUnitFactor > 0 then
        region:SetHeight(pixelToUIUnitFactor / effectiveScale)
    else
        region:SetHeight(1)
    end
end

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

    if type(modules.topAccentBar) ~= "table" then
        modules.topAccentBar = {}
    end

    local settings = modules.topAccentBar
    settings.enabled = settings.enabled ~= false
    return settings
end

function WipedQoL:IsTopAccentBarEnabled()
    local settings = self:GetTopAccentBarSettings()
    return settings and settings.enabled == true or false
end

function WipedQoL:GetTopAccentBarColor()
    if addonTable.GetAddonColor then
        return addonTable.GetAddonColor()
    end
    return 1, 0.82, 0
end

function WipedQoL:ApplyTopAccentBar(frame)
    local accent = frame and frame._wqolAccent
    if not accent then
        return
    end

    local r, g, b = self:GetTopAccentBarColor()
    SetOnePhysicalPixelHeight(accent)
    accent:SetVertexColor(r, g, b, 1)
    accent:SetShown(self:IsTopAccentBarEnabled())
end

function WipedQoL:CreateTopAccentBar(frame)
    if not frame then
        return nil
    end
    if frame._wqolAccent then
        self:ApplyTopAccentBar(frame)
        return frame._wqolAccent
    end

    local accent = frame:CreateTexture(nil, "BORDER", nil, 7)
    accent:SetPoint("TOPLEFT", frame, "TOPLEFT", 1, -1)
    accent:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -1, -1)
    accent:SetTexture(WHITE_TEXTURE)
    frame._wqolAccent = accent
    self:ApplyTopAccentBar(frame)
    return accent
end

function WipedQoL:RefreshTopAccentBars()
    self:ApplyTopAccentBar(_G.GroupLootHistoryFrame)
    self:ApplyTopAccentBar(self.weeklyTaskReminderFrame)
    self:ApplyTopAccentBar(self.expansionVendorFrame)
end

function WipedQoL:SetTopAccentBarEnabled(enabled)
    local settings = self:GetTopAccentBarSettings()
    if not settings then
        return
    end
    settings.enabled = enabled == true
    self:RefreshTopAccentBars()
    if self.RefreshTopAccentBarSkinningUI then
        self:RefreshTopAccentBarSkinningUI()
    end
end
