local _, ns = ...
local L = ns.L or {}

local PvePreyView = {}
ns.UIPvePreyView = PvePreyView

function PvePreyView.CreateCard(parent, helpers)
    helpers = helpers or {}
    local createSectionFrame = helpers.createSectionFrame
    local createLabel = helpers.createLabel
    local createProgressBar = helpers.createProgressBar
    local createWeeklyStatusRow = helpers.createWeeklyStatusRow

    local card = createSectionFrame(parent, "card")
    card:Hide()

    card.title = createLabel(card, "GameFontNormalLarge", "LEFT")
    card.title:SetPoint("TOPLEFT", 14, -18)
    card.title:Hide()

    card.subtitle = createLabel(card, "GameFontHighlightSmall", "LEFT")
    card.subtitle:SetPoint("TOPLEFT", card.title, "BOTTOMLEFT", 0, -8)
    card.subtitle:SetPoint("TOPRIGHT", -14, 0)
    card.subtitle:Hide()

    card.totalLabel = createLabel(card, "GameFontHighlightSmall", "LEFT")
    card.totalLabel:SetPoint("TOPLEFT", 14, -18)

    card.totalValue = createLabel(card, "GameFontHighlight", "RIGHT")
    card.totalValue:SetPoint("RIGHT", -14, 0)
    card.totalValue:SetPoint("CENTER", card.totalLabel, "CENTER", 0, 0)

    card.totalBar = createProgressBar(card)
    card.totalBar:SetPoint("TOPLEFT", card.totalLabel, "BOTTOMLEFT", 0, -8)
    card.totalBar:SetPoint("TOPRIGHT", -14, 0)
    card.totalBar:SetHeight(14)

    card.normalLabel = createLabel(card, "GameFontHighlightSmall", "LEFT")
    card.normalLabel:SetPoint("TOPLEFT", card.totalBar, "BOTTOMLEFT", 0, -20)

    card.normalValue = createLabel(card, "GameFontHighlight", "RIGHT")
    card.normalValue:SetPoint("RIGHT", -14, 0)
    card.normalValue:SetPoint("CENTER", card.normalLabel, "CENTER", 0, 0)

    card.normalBar = createProgressBar(card)
    card.normalBar:SetPoint("TOPLEFT", card.normalLabel, "BOTTOMLEFT", 0, -8)
    card.normalBar:SetPoint("TOPRIGHT", -14, 0)
    card.normalBar:SetHeight(12)

    card.hardLabel = createLabel(card, "GameFontHighlightSmall", "LEFT")
    card.hardLabel:SetPoint("TOPLEFT", card.normalBar, "BOTTOMLEFT", 0, -18)

    card.hardValue = createLabel(card, "GameFontHighlight", "RIGHT")
    card.hardValue:SetPoint("RIGHT", -14, 0)
    card.hardValue:SetPoint("CENTER", card.hardLabel, "CENTER", 0, 0)

    card.hardBar = createProgressBar(card)
    card.hardBar:SetPoint("TOPLEFT", card.hardLabel, "BOTTOMLEFT", 0, -8)
    card.hardBar:SetPoint("TOPRIGHT", -14, 0)
    card.hardBar:SetHeight(12)

    card.nightmareLabel = createLabel(card, "GameFontHighlightSmall", "LEFT")
    card.nightmareLabel:SetPoint("TOPLEFT", card.hardBar, "BOTTOMLEFT", 0, -18)

    card.nightmareValue = createLabel(card, "GameFontHighlight", "RIGHT")
    card.nightmareValue:SetPoint("RIGHT", -14, 0)
    card.nightmareValue:SetPoint("CENTER", card.nightmareLabel, "CENTER", 0, 0)

    card.nightmareBar = createProgressBar(card)
    card.nightmareBar:SetPoint("TOPLEFT", card.nightmareLabel, "BOTTOMLEFT", 0, -8)
    card.nightmareBar:SetPoint("TOPRIGHT", -14, 0)
    card.nightmareBar:SetHeight(12)

    card.weeklyRow = createWeeklyStatusRow(card)
    card.weeklyRow:SetPoint("TOPLEFT", card.nightmareBar, "BOTTOMLEFT", 0, -28)
    card.weeklyRow:SetPoint("TOPRIGHT", -14, 0)

    card.preferredRow = createWeeklyStatusRow(card)
    card.preferredRow:SetPoint("TOPLEFT", card.weeklyRow, "BOTTOMLEFT", 0, -8)
    card.preferredRow:SetPoint("TOPRIGHT", -14, 0)

    return card
end

local function setPreyDifficultyRow(labelFrame, valueFrame, barFrame, difficulty, difficultyCap, helpers)
    difficulty = difficulty or {}
    local labelText = difficulty.label or ""
    local entry = difficulty.entry or {}
    local color = difficulty.color or {}
    local unlocked = entry.unlocked ~= false
    local count = math.max(0, tonumber(entry.count) or 0)
    local cap = math.max(0, tonumber(entry.cap) or difficultyCap)
    local r, g, b = color[1] or 0.92, color[2] or 0.74, color[3] or 0.30

    labelFrame:SetText(labelText)
    if unlocked then
        valueFrame:SetText(string.format("%d/%d", count, cap))
    else
        valueFrame:SetText(L.STATUS_LOCKED or "Locked")
        count = 0
    end

    helpers.setProgressBar(barFrame, unlocked and cap > 0 and (count / cap) or 0, r, g, b)
    helpers.setProgressBreakpoints(barFrame, { cap }, cap)
end

function PvePreyView.RefreshCard(ui, snapshot, helpers)
    if not ui.frame or not ui.frame.pvePreyCard then
        return
    end

    helpers = helpers or {}
    local buildPvePreyView = helpers.buildPvePreyView or function()
        return {}
    end

    local card = ui.frame.pvePreyCard
    local character = ns:GetSelectedCharacter()
    local view = buildPvePreyView(character, snapshot) or {}
    local difficultyCap = tonumber(view.difficultyCap) or 4

    card.title:SetText(view.title or (L.PVE_PREY_TITLE or "Prey"))
    card.subtitle:SetText(view.subtitle or (L.PVE_PREY_SUBTITLE or "Defeated prey targets by difficulty, plus the linked weekly quest."))
    card.title:Hide()
    card.subtitle:Hide()

    card.totalLabel:SetText(view.totalLabel or (L.WEEKLY_PREY_TOTAL or "Defeated Prey Targets"))
    card.totalValue:SetText(view.totalValue or "0/12")
    helpers.setProgressBar(card.totalBar, tonumber(view.totalRatio) or 0, 0.92, 0.74, 0.30)
    helpers.setProgressBreakpoints(card.totalBar, view.totalThresholds or {}, tonumber(view.totalCap) or 12)

    local difficulties = view.difficulties or {}
    setPreyDifficultyRow(card.normalLabel, card.normalValue, card.normalBar, difficulties.normal, difficultyCap, helpers)
    setPreyDifficultyRow(card.hardLabel, card.hardValue, card.hardBar, difficulties.hard, difficultyCap, helpers)
    setPreyDifficultyRow(card.nightmareLabel, card.nightmareValue, card.nightmareBar, difficulties.nightmare, difficultyCap, helpers)

    helpers.setWeeklyStatusRow(card.weeklyRow, view.weeklyQuest or {}, view.weeklyLabel or (L.PVE_PREY_WEEKLY_NAME or "A Nightmarish Task"))
    helpers.setWeeklyStatusRow(card.preferredRow, view.preferredQuest or {}, view.preferredLabel or (L.PVE_PREY_PREFERRED_NAME or "Preferred Prey"))
end
