--[[
============================================================
Decor Vendor
WelcomePanel.lua
============================================================
]]

local addonName, dv = ...

local C = dv.CONSTANTS or {}
local CatSizing = C.CatalogSizing or {}

-------------------------------------------------
-- Helpers
-------------------------------------------------
local function HexToRGB(hex, fallback)
    fallback = fallback or {1, 0.82, 0}

    hex = tostring(hex or "")
    hex = hex:gsub("#", "")

    if #hex ~= 6 then
        return fallback[1], fallback[2], fallback[3]
    end

    local r = tonumber(hex:sub(1, 2), 16)
    local g = tonumber(hex:sub(3, 4), 16)
    local b = tonumber(hex:sub(5, 6), 16)

    if not r or not g or not b then
        return fallback[1], fallback[2], fallback[3]
    end

    return r / 255, g / 255, b / 255
end

local function CreateFeatureCard(parent, titleText, bodyText, accentHex)
    local card = CreateFrame("Frame", nil, parent, "BackdropTemplate")

    local r, g, b = HexToRGB(accentHex or "FFD100")

    card:SetBackdrop({
        bgFile = "Interface\\Buttons\\WHITE8x8",
        edgeFile = "Interface\\Buttons\\WHITE8x8",
        edgeSize = 1,
    })

    card:SetBackdropColor(0.08, 0.08, 0.11, 0.94)
    card:SetBackdropBorderColor(0.22, 0.16, 0.45, 1)

    local accent = card:CreateTexture(nil, "ARTWORK")
    accent:SetPoint("TOPLEFT", 1, -1)
    accent:SetPoint("TOPRIGHT", -1, -1)
    accent:SetHeight(3)
    accent:SetColorTexture(r, g, b, 0.95)

local title = card:CreateFontString(nil, "OVERLAY")
title:SetFont(STANDARD_TEXT_FONT, 14, "OUTLINE")
title:SetPoint("TOPLEFT", 12, -10)
title:SetPoint("TOPRIGHT", -12, -10)
title:SetJustifyH("LEFT")
title:SetTextColor(r, g, b)
title:SetText(titleText)

local body = card:CreateFontString(nil, "OVERLAY")
body:SetFont(STANDARD_TEXT_FONT, 12, "OUTLINE")
body:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 0, -8)
body:SetPoint("RIGHT", card, "RIGHT", -12, 0)
body:SetJustifyH("LEFT")
body:SetJustifyV("TOP")
body:SetTextColor(0.90, 0.90, 0.92)
body:SetWordWrap(true)
body:SetText(bodyText)

    return card
end

local function LayoutCards(content, cards)
    local gap = 14
    local columns = 3
    local contentWidth = content:GetWidth()

    if not contentWidth or contentWidth <= 0 then
        contentWidth = (CatSizing.FrameWidth or 1100) - 48
    end

    local cardWidth = math.floor((contentWidth - ((columns - 1) * gap)) / columns)
    local cardHeight = 118

    for index, card in ipairs(cards) do
        local col = (index - 1) % columns
        local row = math.floor((index - 1) / columns)

        card:ClearAllPoints()
        card:SetSize(cardWidth, cardHeight)
        card:SetPoint("TOPLEFT", content, "TOPLEFT", col * (cardWidth + gap), -(row * (cardHeight + gap)))
    end
end

-------------------------------------------------
-- Main Creation
-------------------------------------------------

function dv:CreateWelcomePanel(parent)
    if self.welcomePanel then
        return self.welcomePanel
    end

    local panel = CreateFrame("Frame", "DV_WelcomePanel", parent, "BackdropTemplate")
    panel:SetAllPoints(parent)
    panel:SetFrameStrata("DIALOG")
    panel:SetFrameLevel(parent:GetFrameLevel() + 50)
    panel:Hide()

    panel:SetBackdrop({
        bgFile = "Interface\\Buttons\\WHITE8x8",
        edgeFile = "Interface\\Buttons\\WHITE8x8",
        edgeSize = 1,
    })

    panel:SetBackdropColor(0.015, 0.015, 0.035, 0.975)
    panel:SetBackdropBorderColor(0.35, 0.22, 0.75, 1)

    -------------------------------------------------
    -- Header
    -------------------------------------------------

    local titleBar = CreateFrame("Frame", nil, panel, "BackdropTemplate")
    titleBar:SetPoint("TOPLEFT", 14, -14)
    titleBar:SetPoint("TOPRIGHT", -14, -14)
    titleBar:SetHeight(58)

    titleBar:SetBackdrop({
        bgFile = "Interface\\Buttons\\WHITE8x8",
        edgeFile = "Interface\\Buttons\\WHITE8x8",
        edgeSize = 1,
    })

    titleBar:SetBackdropColor(0.07, 0.07, 0.105, 0.96)
    titleBar:SetBackdropBorderColor(0.22, 0.16, 0.45, 1)

    local logo = titleBar:CreateTexture(nil, "ARTWORK")
    logo:SetSize(26, 26)
    logo:SetPoint("LEFT", 14, 0)
    logo:SetTexture("Interface\\AddOns\\DecorVendor\\Assets\\decorvendoricon")

    local title = titleBar:CreateFontString(nil, "OVERLAY", "GameFontNormalHuge")
    title:SetPoint("LEFT", logo, "RIGHT", 14, 0)
    title:SetJustifyH("LEFT")
    title:SetTextColor(1.0, 0.9, 0.45)
    title:SetText("Welcome to Decor Vendor")

    local subtitle = titleBar:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
    subtitle:SetPoint("RIGHT", titleBar, "RIGHT", -18, 0)
    subtitle:SetWidth(520)
    subtitle:SetJustifyH("RIGHT")
    subtitle:SetTextColor(0.88, 0.88, 0.92)
    subtitle:SetText("Your companion for decor discovery, collection tracking, and previewing.")

    -------------------------------------------------
    -- Content
    -------------------------------------------------

    local content = CreateFrame("Frame", nil, panel)
    content:SetPoint("TOPLEFT", titleBar, "BOTTOMLEFT", 0, -14)
    content:SetPoint("TOPRIGHT", titleBar, "BOTTOMRIGHT", 0, -14)
    content:SetHeight(382)

    local cards = {}

   table.insert(cards, CreateFeatureCard(
    content,
    "Interactive 3D Preview",
    "Preview decor and vendors in 3D with zoom and rotate controls.",
    "66D9EF"
))

table.insert(cards, CreateFeatureCard(
    content,
    "Vendor & Source Tracking",
    "See where decor comes from across vendors, achievements, quests, professions, boss drops, treasures, and events.",
    "66B2FF"
))

table.insert(cards, CreateFeatureCard(
    content,
    "Collection Progress",
    "Track what you have collected and what you still need across multiple tabs.",
    "00FF66"
))

table.insert(cards, CreateFeatureCard(
    content,
    "Sidebar Filters",
    "Use expansions, factions, categories, and unlock filters to narrow results faster.",
    "C084FC"
))

table.insert(cards, CreateFeatureCard(
    content,
    "Map Integration",
    "Use waypoint and map support to find vendors, boss drops, treasures, and other decor sources.",
    "44DD88"
))

table.insert(cards, CreateFeatureCard(
    content,
    "Helpful Extras",
    "Check owned decor, preview items, view prices, and browse different content tabs from one place.",
    "FFD100"
))

table.insert(cards, CreateFeatureCard(
    content,
    "Gallery Browser",
    "Created to make decor easier to browse in 3D, compare prices, and see every known source in one place. It is still being actively improved and may take a few seconds to load the first time.",
    "FF66CC"
))

table.insert(cards, CreateFeatureCard(
    content,
    "Good to Know",
    "Some decor sources can have both an original unlock source and a vendor for repeat purchases.",
    "FFAA44"
))

table.insert(cards, CreateFeatureCard(
    content,
    "Made for Collectors",
    "Decor Vendor is built to help you find, track, preview, and collect housing decor across Azeroth.",
    "9955CC"
))

    panel.cards = cards
    panel.content = content

    content:SetScript("OnSizeChanged", function()
        LayoutCards(content, cards)
    end)

    C_Timer.After(0, function()
        if content and content:IsShown() then
            LayoutCards(content, cards)
        end
    end)
-------------------------------------------------
-- Development Warning Banner
-------------------------------------------------
local devWarning = CreateFrame("Frame", nil, panel, "BackdropTemplate")
devWarning:SetPoint("LEFT", panel, "LEFT", 16, 0)
devWarning:SetPoint("RIGHT", panel, "RIGHT", -16, 0)
devWarning:SetPoint("BOTTOM", panel, "BOTTOM", 0, 100)
devWarning:SetHeight(46)

devWarning:SetBackdrop({
    bgFile = "Interface\\Buttons\\WHITE8x8",
    edgeFile = "Interface\\Buttons\\WHITE8x8",
    edgeSize = 1,
    insets = { left = 1, right = 1, top = 1, bottom = 1 },
})

devWarning:SetBackdropColor(0.10, 0.04, 0.04, 0.92)
devWarning:SetBackdropBorderColor(0.85, 0.25, 0.25, 1)

local devIcon = devWarning:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
devIcon:SetPoint("LEFT", devWarning, "LEFT", 12, 0)
devIcon:SetText("|cffff4444!|r")

local devText = devWarning:CreateFontString(nil, "OVERLAY")
devText:SetFont(STANDARD_TEXT_FONT, 12, "OUTLINE")
devText:SetPoint("LEFT", devIcon, "RIGHT", 10, 0)
devText:SetPoint("RIGHT", devWarning, "RIGHT", -12, 0)
devText:SetJustifyH("LEFT")
devText:SetJustifyV("MIDDLE")
devText:SetText(
    "|cffff6666Gallery Browser Notice:|r "
    .. "|cffffffffThe Gallery is still in active development. "
    .. "It may take a few seconds to load the first time, "
    .. "and some data or features may be improved in future updates.|r"
)
    -------------------------------------------------
    -- Footer Tip
    -------------------------------------------------

    local footer = CreateFrame("Frame", nil, panel, "BackdropTemplate")
    footer:SetPoint("LEFT", panel, "LEFT", 16, 0)
    footer:SetPoint("RIGHT", panel, "RIGHT", -16, 0)
    footer:SetPoint("BOTTOM", panel, "BOTTOM", 0, 52)
    footer:SetHeight(42)

    footer:SetBackdrop({
        bgFile = "Interface\\Buttons\\WHITE8x8",
        edgeFile = "Interface\\Buttons\\WHITE8x8",
        edgeSize = 1,
    })

    footer:SetBackdropColor(0.06, 0.06, 0.10, 0.94)
    footer:SetBackdropBorderColor(0.22, 0.16, 0.45, 1)

    local noteIcon = footer:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
    noteIcon:SetPoint("LEFT", 12, 0)
    noteIcon:SetTextColor(1.0, 0.82, 0.0)
    noteIcon:SetText("!")

    local note = footer:CreateFontString(nil, "OVERLAY")
	note:SetFont(STANDARD_TEXT_FONT, 12, "OUTLINE")
    note:SetPoint("LEFT", noteIcon, "RIGHT", 10, 0)
    note:SetPoint("RIGHT", footer, "RIGHT", -12, 0)
    note:SetJustifyH("LEFT")
    note:SetTextColor(0.92, 0.92, 0.92)
    note:SetText("Tip: Use the bottom tabs to switch between Vendors, Professions, Quests, Achievements, and Boss Drops.")



-------------------------------------------------
-- Bottom Buttons
-------------------------------------------------

local decorBtn = CreateFrame("Button", nil, panel, "UIPanelButtonTemplate")
decorBtn:SetSize(190, 28)
decorBtn:SetPoint("BOTTOM", panel, "BOTTOM", -105, 14)
decorBtn:SetText("Explore Decor Vendor")

decorBtn:SetScript("OnClick", function()
    vendorSettings = vendorSettings or {}
    vendorSettings.welcomeSeen = true

    panel:Hide()

    if dv and dv.frame then
        dv.frame:Show()
    end
end)

local galleryBtn = CreateFrame("Button", nil, panel, "UIPanelButtonTemplate")
galleryBtn:SetSize(190, 28)
galleryBtn:SetPoint("LEFT", decorBtn, "RIGHT", 20, 0)
galleryBtn:SetText("Open Gallery")

galleryBtn:SetScript("OnClick", function()
    vendorSettings = vendorSettings or {}
    vendorSettings.welcomeSeen = true

    panel:Hide()

    if dv and dv.frame and dv.frame:IsShown() then
        dv.frame:Hide()
    end

    if dv and dv.OpenGalleryAddon then
        dv.OpenGalleryAddon()
    else
        print("|cffff4040Decor Vendor:|r Gallery loader function is missing.")
    end
end)

    self.welcomePanel = panel
    return panel
end

-------------------------------------------------
-- Show / Hide
-------------------------------------------------

function dv:ShowWelcomePanel(forceShow)
    if not self.frame then return end

    if not self.welcomePanel then
        self:CreateWelcomePanel(self.frame)
    end

    vendorSettings = vendorSettings or {}

    if forceShow or not vendorSettings.welcomeSeen then
        self.welcomePanel:Show()

        if self.welcomePanel.content and self.welcomePanel.cards then
            C_Timer.After(0, function()
                LayoutCards(self.welcomePanel.content, self.welcomePanel.cards)
            end)
        end
    end
end

function dv:HideWelcomePanel()
    if self.welcomePanel then
        self.welcomePanel:Hide()
    end
end