--[[
    UI/Theme.lua

    Shared visual language for the collection window.  It deliberately uses
    only client-shipped primitives: flat colour textures, Blizzard fonts and
    BackdropTemplate.  That keeps the addon independent from copied Blizzard
    art while matching the warm, layered materials of the current stable UI.
]]

local _, ns = ...

local Theme = {}
ns.UI = ns.UI or {}
ns.UI.Theme = Theme

Theme.color = {
    ink       = { 0.025, 0.018, 0.015, 0.84 },
    panel     = { 0.070, 0.045, 0.035, 0.88 },
    panelSoft = { 0.145, 0.085, 0.060, 0.86 },
    card      = { 0.120, 0.073, 0.055, 0.92 },
    cardHover = { 0.205, 0.115, 0.078, 0.96 },
    gold      = { 0.98, 0.78, 0.15, 1.00 },
    goldSoft  = { 0.52, 0.36, 0.10, 0.90 },
    line      = { 0.42, 0.29, 0.17, 0.82 },
    text      = { 0.94, 0.89, 0.80, 1.00 },
    muted     = { 0.58, 0.52, 0.47, 1.00 },
    tracker   = { 0.27, 0.72, 0.68, 1.00 },
}

Theme.trackerTexture = "Interface\\AddOns\\ShinyHunter\\Assets\\UI\\hunter-tracker.png"
Theme.bestiaryShellTexture = "Interface\\AddOns\\ShinyHunter\\Assets\\UI\\hunter-bestiary-shell.png"
Theme.sightingAlertTexture = "Interface\\AddOns\\ShinyHunter\\Assets\\UI\\hunter-sighting-alert.png"

local TRACKER_CENTER_U_MIN = 0.285
local TRACKER_CENTER_U_MAX = 0.835
-- Authored brass route details measured from the 1024 px source. The live
-- turquoise indicator snaps to these instead of floating between ornaments.
local TRACKER_RAIL_STOPS_U = {
    355 / 1024,
    459 / 1024,
    568 / 1024,
    680 / 1024,
    782.5 / 1024,
}

local BACKDROP = {
    bgFile = "Interface\\Buttons\\WHITE8X8",
    edgeFile = "Interface\\Buttons\\WHITE8X8",
    edgeSize = 1,
}

local function Unpack(color)
    return color[1], color[2], color[3], color[4]
end

function Theme:CreatePanel(parent, color, borderColor)
    local panel = CreateFrame("Frame", nil, parent, "BackdropTemplate")
    panel:SetBackdrop(BACKDROP)
    panel:SetBackdropColor(Unpack(color or self.color.panel))
    panel:SetBackdropBorderColor(Unpack(borderColor or self.color.line))
    return panel
end

function Theme:CreateRule(parent, color)
    local rule = parent:CreateTexture(nil, "ARTWORK")
    rule:SetColorTexture(Unpack(color or self.color.line))
    return rule
end

function Theme:ApplyButton(button, selected)
    local fill = selected and self.color.panelSoft or self.color.ink
    local edge = selected and self.color.gold or self.color.line

    if not button.SetBackdrop then
        return
    end
    button:SetBackdrop(BACKDROP)
    button:SetBackdropColor(Unpack(fill))
    button:SetBackdropBorderColor(Unpack(edge))

    local font = button:GetFontString()
    if font then
        local color = selected and self.color.gold or self.color.text
        font:SetTextColor(Unpack(color))
    end
end

--- Adds the client-native menu chevron. A text glyph was deliberately avoided:
-- FRIZQT does not contain the Unicode black triangle and renders it as a
-- square on several locales. `bag-arrow` is a Retail atlas, not addon artwork.
function Theme:CreateMenuArrow(button)
    local arrow = button:CreateTexture(nil, "OVERLAY")
    arrow:SetSize(14, 14)
    arrow:SetPoint("RIGHT", button, "RIGHT", -8, 0)
    arrow:SetAtlas("bag-arrow")
    arrow:SetRotation(math.pi / 2)
    arrow:SetVertexColor(0.90, 0.70, 0.20, 1)
    return arrow
end

function Theme:ApplyJournalTab(button, selected)
    if not button.SetBackdrop then
        return
    end

    button:SetBackdrop(BACKDROP)
    if selected then
        button:SetBackdropColor(0.115, 0.070, 0.045, 0.96)
        button:SetBackdropBorderColor(0.78, 0.55, 0.18, 0.95)
        button.TabMarker:SetColorTexture(0.27, 0.82, 0.75, 1)
        button.TabMarkerGlow:Show()
    else
        button:SetBackdropColor(0.025, 0.020, 0.018, 0.80)
        button:SetBackdropBorderColor(0.30, 0.21, 0.12, 0.78)
        button.TabMarker:SetColorTexture(0.36, 0.25, 0.13, 0.90)
        button.TabMarkerGlow:Hide()
    end

    local font = button:GetFontString()
    if font then
        local color = selected and self.color.gold or self.color.text
        font:SetTextColor(Unpack(color))
    end
end

function Theme:ApplyAlertAction(button, disabled)
    local font = button:GetFontString()
    if font then
        if disabled then
            font:SetTextColor(0.52, 0.48, 0.43)
        else
            font:SetTextColor(0.92, 0.82, 0.58)
        end
    end

    if not button.AlertHighlight then
        local highlight = button:CreateTexture(nil, "HIGHLIGHT")
        highlight:SetPoint("TOPLEFT", button, "TOPLEFT", 2, -2)
        highlight:SetPoint("BOTTOMRIGHT", button, "BOTTOMRIGHT", -2, 2)
        highlight:SetColorTexture(0.20, 0.85, 0.76, disabled and 0.03 or 0.10)
        button.AlertHighlight = highlight
    end
end

--- Applies the original hunter tracking instrument as a horizontal three-slice
-- so its compass and clawed end caps keep their proportions at any panel width.
function Theme:ApplyTrackerArtwork(frame, leftWidth, rightWidth)
    local left = frame:CreateTexture(nil, "BACKGROUND")
    left:SetTexture(self.trackerTexture)
    left:SetTexCoord(0, 0.285, 0, 1)
    left:SetPoint("TOPLEFT", frame, "TOPLEFT", 0, 0)
    left:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 0, 0)
    left:SetWidth(leftWidth)

    local center = frame:CreateTexture(nil, "BACKGROUND")
    center:SetTexture(self.trackerTexture)
    center:SetTexCoord(0.285, 0.835, 0, 1)
    center:SetPoint("TOPLEFT", left, "TOPRIGHT", 0, 0)
    center:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -rightWidth, 0)

    local right = frame:CreateTexture(nil, "BACKGROUND")
    right:SetTexture(self.trackerTexture)
    right:SetTexCoord(0.835, 1, 0, 1)
    right:SetPoint("TOPRIGHT", frame, "TOPRIGHT", 0, 0)
    right:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", 0, 0)
    right:SetWidth(rightWidth)

    frame.TrackerArtwork = { left, center, right }
    return frame.TrackerArtwork
end

--- Returns the frame-space X coordinate of the authored rail detail nearest
-- to a 0..1 progress value. It mirrors ApplyTrackerArtwork's three-slice math,
-- so resizing the leather centre cannot separate the indicator from the art.
function Theme:GetTrackerRailStop(frameWidth, leftWidth, rightWidth, progress)
    progress = math.max(0, math.min(1, progress or 0))
    local stopIndex = math.floor(progress * (#TRACKER_RAIL_STOPS_U - 1) + 0.5) + 1
    local sourceU = TRACKER_RAIL_STOPS_U[stopIndex]
    local centerFraction = (sourceU - TRACKER_CENTER_U_MIN)
        / (TRACKER_CENTER_U_MAX - TRACKER_CENTER_U_MIN)
    local centerWidth = math.max(0, frameWidth - leftWidth - rightWidth)
    return leftWidth + centerFraction * centerWidth
end

function Theme:ApplyHuntButton(button, active)
    if not button.SetBackdrop then
        return
    end
    button:SetBackdrop(BACKDROP)
    if active then
        button:SetBackdropColor(0.16, 0.055, 0.035, 0.96)
        button:SetBackdropBorderColor(0.82, 0.30, 0.16, 0.95)
        button:GetFontString():SetTextColor(1.00, 0.72, 0.42)
    else
        button:SetBackdropColor(0.035, 0.105, 0.10, 0.96)
        button:SetBackdropBorderColor(self.color.tracker[1],
            self.color.tracker[2], self.color.tracker[3], 0.95)
        button:GetFontString():SetTextColor(0.72, 1.00, 0.92)
    end
end
