--[[ Regional rare-hunt progress, presented as pages in the hunter journal. ]]

local _, ns = ...

local RegionProgressPanel = ns.NewModule("RegionProgressPanel")
ns.UI = ns.UI or {}
ns.UI.RegionProgressPanel = RegionProgressPanel

local Theme = ns.UI.Theme
local ROW_HEIGHT = 66

function RegionProgressPanel:Create(parent)
    local panel = {}

    local title = parent:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
    title:SetPoint("TOPLEFT", parent, "TOPLEFT", 12, -10)
    title:SetText(ns.L["REGION_PROGRESS_TITLE"])
    title:SetTextColor(Theme.color.gold[1], Theme.color.gold[2], Theme.color.gold[3])

    local subtitle = parent:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
    subtitle:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 0, -7)
    subtitle:SetPoint("RIGHT", parent, "RIGHT", -20, 0)
    subtitle:SetJustifyH("LEFT")
    subtitle:SetText(ns.L["REGION_PROGRESS_SUBTITLE"])
    subtitle:SetTextColor(Theme.color.muted[1], Theme.color.muted[2], Theme.color.muted[3])

    local scroll = CreateFrame("ScrollFrame", nil, parent)
    scroll:SetPoint("TOPLEFT", subtitle, "BOTTOMLEFT", 0, -14)
    scroll:SetPoint("BOTTOMRIGHT", parent, "BOTTOMRIGHT", -8, 8)
    scroll:EnableMouseWheel(true)

    local child = CreateFrame("Frame", nil, scroll)
    child:SetWidth(math.max(parent:GetWidth() - 32, 700))
    child:SetHeight(1)
    scroll:SetScrollChild(child)

    scroll:SetScript("OnMouseWheel", function(self, delta)
        local maximum = math.max(0, child:GetHeight() - self:GetHeight())
        self:SetVerticalScroll(math.max(0, math.min(maximum,
            self:GetVerticalScroll() - delta * ROW_HEIGHT)))
    end)

    panel.rows = {}

    local function AcquireRow(index)
        if panel.rows[index] then return panel.rows[index] end
        local row = Theme:CreatePanel(child, Theme.color.card, Theme.color.line)
        row:SetHeight(ROW_HEIGHT - 6)
        row:SetPoint("TOPLEFT", child, "TOPLEFT", 0, -(index - 1) * ROW_HEIGHT)
        row:SetPoint("RIGHT", child, "RIGHT", 0, 0)
        row:EnableMouse(true)
        row:SetScript("OnEnter", function(self)
            self:SetBackdropBorderColor(0.32, 0.82, 0.76, 0.90)
        end)
        row:SetScript("OnLeave", function(self)
            self:SetBackdropBorderColor(Theme.color.line[1], Theme.color.line[2],
                Theme.color.line[3], Theme.color.line[4] or 1)
        end)
        row:SetScript("OnMouseUp", function(self, button)
            if button == "LeftButton" and self.regionID then
                ns.UI.MainWindow:OpenRegion(self.regionID)
            end
        end)

        row.Emblem = row:CreateTexture(nil, "ARTWORK")
        row.Emblem:SetSize(36, 36)
        row.Emblem:SetPoint("LEFT", row, "LEFT", 14, 0)
        row.Emblem:SetTexture("Interface\\Icons\\Ability_Hunter_BeastCall")
        row.Emblem:SetTexCoord(0.08, 0.92, 0.08, 0.92)

        row.Name = row:CreateFontString(nil, "ARTWORK", "GameFontNormal")
        row.Name:SetPoint("TOPLEFT", row.Emblem, "TOPRIGHT", 14, -2)
        row.Name:SetJustifyH("LEFT")

        row.Detail = row:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
        row.Detail:SetPoint("TOPLEFT", row.Name, "BOTTOMLEFT", 0, -5)
        row.Detail:SetTextColor(Theme.color.muted[1], Theme.color.muted[2], Theme.color.muted[3])

        row.Bar = CreateFrame("StatusBar", nil, row)
        row.Bar:SetSize(260, 6)
        row.Bar:SetPoint("RIGHT", row, "RIGHT", -118, 0)
        row.Bar:SetStatusBarTexture("Interface\\Buttons\\WHITE8X8")
        local bg = row.Bar:CreateTexture(nil, "BACKGROUND")
        bg:SetAllPoints(row.Bar)
        bg:SetColorTexture(0.04, 0.03, 0.025, 0.95)

        row.Counter = row:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
        row.Counter:SetPoint("LEFT", row.Bar, "RIGHT", 12, 0)

        row.Arrow = row:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
        row.Arrow:SetPoint("RIGHT", row, "RIGHT", -14, 0)
        row.Arrow:SetText("›")
        row.Arrow:SetTextColor(0.32, 0.82, 0.76)
        panel.rows[index] = row
        return row
    end

    function panel:Refresh()
        local regions = ns.Collection.RegionProgress:GetRegions()
        for _, row in ipairs(self.rows) do row:Hide() end
        for index, region in ipairs(regions) do
            local row = AcquireRow(index)
            row.regionID = region.id
            row.Name:SetText(region.name)
            row.Detail:SetText(region.complete
                and ns.L["REGION_COMPLETE"] or ns.L["REGION_IN_PROGRESS"])
            row.Counter:SetText(ns.L["PROGRESS_FORMAT"]:format(region.collected, region.total))
            row.Bar:SetMinMaxValues(0, math.max(1, region.total))
            row.Bar:SetValue(region.collected)
            if region.complete then
                row.Bar:SetStatusBarColor(0.22, 0.85, 0.70, 1)
                row.Emblem:SetDesaturated(false)
                row.Emblem:SetVertexColor(1, 0.88, 0.48)
            else
                row.Bar:SetStatusBarColor(0.88, 0.60, 0.12, 1)
                row.Emblem:SetDesaturated(true)
                row.Emblem:SetVertexColor(0.55, 0.55, 0.55)
            end
            row:Show()
        end
        child:SetHeight(math.max(1, #regions * ROW_HEIGHT))
    end

    panel:Refresh()
    return panel
end
