local addonName, addon = ...

addon.UI = addon.UI or {}
local UI = addon.UI

UI.COLORS = {
    background = { 0.025, 0.015, 0.040, 0.98 },
    header = { 0.075, 0.025, 0.095, 1 },
    panel = { 0.035, 0.025, 0.055, 0.97 },
    panelAlt = { 0.055, 0.025, 0.075, 0.94 },
    card = { 0.070, 0.025, 0.095, 0.82 },
    border = { 0.38, 0.12, 0.50, 1 },
    borderBright = { 0.58, 0.20, 0.75, 1 },
    gold = { 1.00, 0.72, 0.12, 1 },
    text = { 0.96, 0.94, 0.98, 1 },
    muted = { 0.68, 0.62, 0.72, 1 },
    purple = { 0.78, 0.34, 1.00, 1 },
    selected = { 0.32, 0.12, 0.43, 1 },
    today = { 0.22, 0.08, 0.30, 1 },
    warning = { 1.00, 0.28, 0.12, 1 },
}

function UI:T(key, fallback)
    local localization = addon.Localization
    if localization and localization.Get then
        return localization:Get(key, fallback)
    end
    return fallback or key
end

function UI:LT(text)
    local localization = addon.Localization
    if localization and localization.Literal then
        return localization:Literal(text)
    end
    return text
end

function UI:AddItemNameEquivalentToTooltip(itemID, localizedName)
    local query = addon.LootTracker and addon.LootTracker.Query
    if not query or not query.GetItemNameParts then return end
    local _, secondary, secondaryCode = query:GetItemNameParts(itemID, localizedName)
    if not secondary or secondary == "" then return end
    GameTooltip:AddLine(" " )
    GameTooltip:AddLine((secondaryCode or "") .. ": " .. secondary, 0.66, 0.72, 0.82)
end

function UI:GetToday()
    local d = date("*t")
    return { year = d.year, month = d.month, day = d.day }
end

function UI:SameDate(a, b)
    return a and b
        and a.year == b.year
        and a.month == b.month
        and a.day == b.day
end

function UI:FormatDate(dateData)
    if not dateData then
        return self:T("NO_DATE", "NO DATE")
    end

    return string.format(
        "%02d.%02d.%04d",
        dateData.day,
        dateData.month,
        dateData.year
    )
end

function UI:GetTaskDateText(task)
    if not task or not task.dueDate then
        return self:T("NO_DATE", "NO DATE")
    end

    local d = task.dueDate
    local today = self:GetToday()

    if self:SameDate(d, today) then
        return string.format("%s - %02d.%02d.%04d", self:T("TODAY", "TODAY"), d.day, d.month, d.year)
    end

    local tomorrowTimestamp = time({
        year = today.year,
        month = today.month,
        day = today.day + 1,
        hour = 12,
    })
    local tomorrow = date("*t", tomorrowTimestamp)

    if self:SameDate(d, tomorrow) then
        return string.format("%s - %02d.%02d.%04d", self:T("TOMORROW", "TOMORROW"), d.day, d.month, d.year)
    end

    return self:FormatDate(d)
end

function UI:GetTaskTimeText(task)
    if not addon.DB:TaskHasTime(task) then
        return ""
    end

    return string.format("%02d:%02d", task.dueTime.hour, task.dueTime.minute)
end

function UI:FormatRemaining(seconds)
    if not seconds then
        return ""
    end
    if seconds <= 0 then
        return self:T("NOW", "NOW")
    end

    local days = math.floor(seconds / 86400)
    seconds = seconds - days * 86400
    local hours = math.floor(seconds / 3600)
    seconds = seconds - hours * 3600
    local minutes = math.floor(seconds / 60)
    local secs = math.floor(seconds % 60)

    if days > 0 then
        return string.format("%dd %02dh %02dm", days, hours, minutes)
    elseif hours > 0 then
        return string.format("%02dh %02dm", hours, minutes)
    elseif minutes > 0 then
        return string.format("%02dm %02ds", minutes, secs)
    end

    return string.format("%02ds", secs)
end

function UI:ClearFrame(frame)
    if not frame then
        return
    end

    local children = { frame:GetChildren() }
    for _, child in ipairs(children) do
        child:Hide()
        child:SetParent(nil)
    end

    local regions = { frame:GetRegions() }
    for _, region in ipairs(regions) do
        region:Hide()
    end
end

function UI:ApplyPanelBackdrop(frame)
    frame:SetBackdrop({
        bgFile = "Interface\\Buttons\\WHITE8X8",
        edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
        edgeSize = 11,
    })
    frame:SetBackdropColor(unpack(self.COLORS.panel))
    frame:SetBackdropBorderColor(unpack(self.COLORS.border))
end

function UI:ApplyRedButton(button)
    -- Native Blizzard button artwork only. Compatibility no-op.
    return button
end

function UI:CreateBlizzardDropdown(parent, width, defaultText, generator)
    local dropdown = CreateFrame("DropdownButton", nil, parent, "WowStyle1DropdownTemplate")
    dropdown:SetWidth(width or 180)
    dropdown:SetDefaultText(defaultText or "SELECT")
    if generator then
        dropdown:SetupMenu(generator)
    end
    return dropdown
end

function UI:SetBlizzardDropdownText(dropdown, text)
    if not dropdown then return end
    if dropdown.OverrideText then
        dropdown:OverrideText(text or "SELECT")
    else
        dropdown:SetDefaultText(text or "SELECT")
    end
end

function UI:AddBlizzardMenuEntry(rootDescription, entry)
    if not rootDescription or not entry then return nil end

    local description = rootDescription:CreateButton(
        tostring(entry.text or ""),
        function()
            if entry.func then entry.func() end
        end
    )

    if entry.disabled and description.SetEnabled then
        description:SetEnabled(false)
    end

    if (entry.itemID or entry.tooltipText) and description.SetTooltip then
        description:SetTooltip(function(tooltip)
            if entry.itemID and tooltip.SetHyperlink then
                tooltip:SetHyperlink("item:" .. tostring(entry.itemID))
            elseif entry.tooltipText then
                if GameTooltip_SetTitle then
                    GameTooltip_SetTitle(tooltip, tostring(entry.tooltipText))
                elseif tooltip.SetText then
                    tooltip:SetText(tostring(entry.tooltipText))
                end
            end
        end)
    end

    return description
end

function UI:OpenBlizzardContextMenu(owner, entries)
    if not owner or not entries or #entries == 0 then return end
    MenuUtil.CreateContextMenu(owner, function(_, rootDescription)
        for _, entry in ipairs(entries) do
            self:AddBlizzardMenuEntry(rootDescription, entry)
        end
    end)
end
