local ADDON_NAME, ns = ...

local HomeView = {}
ns.HomeView = HomeView

local TILE_ASPECT = 96 / 174 -- match the battleground map tiles

-- Mirrors the website PvP Hub cards; art comes straight from the site assets.
local function CategoryDefs()
    return {
        { id = "blitz", title = "Battleground Blitz Guides", art = "blitz" },
        { id = "terms", title = "Blitz Terminology", art = "terms" },
        { id = "macros", title = "Blitz Macros", art = "macros" },
    }
end

function HomeView:Hide()
    if self.hero then self.hero:Hide() end
    if ns.AdventureGuide and ns.AdventureGuide.HideHomeTitle then
        ns.AdventureGuide:HideHomeTitle()
    end
    if self.tiles then
        for _, t in ipairs(self.tiles) do t:Hide() end
    end
    if ns.MainFrame and ns.MainFrame.HideHomeHeaderSeam then
        ns.MainFrame:HideHomeHeaderSeam()
    end
end

local function EnsureHero(self, parent)
    if self.hero then return self.hero end
    local h = CreateFrame("Frame", nil, parent)
    h.title = h:CreateFontString(nil, "OVERLAY", "GameFontNormalHuge")
    h.title:SetJustifyH("LEFT")
    -- Gold header title, matching Blizzard's own tab headers ("Suggested
    -- Content", "Raids"). This is Adventure Guide chrome, not parchment content.
    h.title:SetTextColor(1.0, 0.82, 0.0)
    h.sub = h:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
    h.sub:SetJustifyH("LEFT")
    h.sub:SetTextColor(0.75, 0.75, 0.78)
    self.hero = h
    return h
end

-- Artwork tile matching EncounterInstanceButtonTemplate.
local function CreateTile(parent)
    local t = CreateFrame("Button", nil, parent, "EncounterInstanceButtonTemplate")
    if t.name then t.name:SetTextColor(1, 0.82, 0) end
    return t
end

local function LayoutTile(t, cat, art, tileW, tileH)
    if t.bgImage then
        if art then
            t.bgImage:SetTexture(art)
        else
            t.bgImage:SetColorTexture(0.12, 0.12, 0.14)
        end
        t.bgImage:SetTexCoord(0, 1, 0, 1)
        local ix = math.max(4, math.floor(tileW * 0.045))
        local iy = math.max(4, math.floor(tileH * 0.09))
        t.bgImage:ClearAllPoints()
        t.bgImage:SetPoint("TOPLEFT", ix, -iy)
        t.bgImage:SetPoint("BOTTOMRIGHT", -ix, iy)
    end

    if t.name then
        t.name:SetWidth(tileW - 24)
        t.name:SetText(cat.title)
    end
    if t.heroicIcon then t.heroicIcon:Hide() end
    if t.range then t.range:Hide() end
    if t.ModifiedInstanceIcon then t.ModifiedInstanceIcon:Hide() end
    t:Show()
end

function HomeView:Show()
    local mf = ns.MainFrame
    mf:ClearContent()
    mf:SetSubnav()

    local parent = mf.content
    local width = mf:ContentWidth()

    -- "Field Manual" section title + divider are drawn as fixed Adventure Guide
    -- chrome (host level), anchored to the EJ inset at the EXACT offsets Blizzard
    -- uses for the "Raids"/"Dungeons" title and its content boundary. The tiles
    -- below start at the same offset as Blizzard's instance grid.
    if self.hero then self.hero:Hide() end
    if ns.AdventureGuide and ns.AdventureGuide.ShowHomeTitle then
        ns.AdventureGuide:ShowHomeTitle()
    end
    mf:ShowHomeHeaderSeam()

    -- Blizzard's instance grid starts at (14, -50) inside instanceSelect = -52
    -- from the inset. Our scroll content top is at inset -8, so tiles begin ~44
    -- below the content top to line up with Blizzard.
    local y = 44

    local cats = CategoryDefs()

    -- Match the battleground browser tile size (~170px wide, 4-ish columns).
    local gap = 12
    local cols = math.max(2, math.floor((width + gap) / (170 + gap)))
    local tileW = math.floor((width - (cols - 1) * gap) / cols)
    local tileH = math.floor(tileW * TILE_ASPECT)

    self.tiles = self.tiles or {}
    for _, t in ipairs(self.tiles) do t:Hide() end

    for i, cat in ipairs(cats) do
        local t = self.tiles[i]
        if not t then
            t = CreateTile(parent)
            self.tiles[i] = t
        end
        local col = (i - 1) % cols
        local row = math.floor((i - 1) / cols)
        t:ClearAllPoints()
        t:SetPoint("TOPLEFT", col * (tileW + gap), -(y + row * (tileH + gap)))
        t:SetSize(tileW, tileH)
        local art = ns.GetCardArt and ns.GetCardArt(cat.art)
        LayoutTile(t, cat, art, tileW, tileH)
        local id = cat.id
        t:SetScript("OnClick", function() ns.MainFrame:SelectSection(id) end)
    end

    local rows = math.ceil(#cats / cols)
    parent:SetHeight(math.max(y + rows * (tileH + gap) + 16, 10))
    mf:ScrollToTop()
end
