-- ============================================================
-- GameSlayve Collection Flex
-- Clean UI Foundation
-- ============================================================

local PANEL_WIDTH = 760
local PANEL_HEIGHT = 580

local BACKDROP_TEXTURE =
    "Interface\\AddOns\\GameSlayve_CollectionFlex\\Media\\GameSlayve_CollectionFlex_defaultPanel.png"

local CollectionFlexPanel

-- ============================================================
-- Helpers
-- ============================================================

local function CF_Print(msg)
    print("|cffffcc33Collection Flex:|r " .. tostring(msg))
end

local function CreateText(parent, text, size, x, y, color)
    local fs = parent:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    fs:SetPoint("TOPLEFT", parent, "TOPLEFT", x, y)
    fs:SetFont("Fonts\\FRIZQT__.TTF", size or 12, "OUTLINE")
    fs:SetText(text or "")

    if color == "gold" then
        fs:SetTextColor(1, 0.82, 0.25, 1)
    elseif color == "blue" then
        fs:SetTextColor(0.65, 0.9, 1, 1)
    elseif color == "purple" then
        fs:SetTextColor(0.85, 0.45, 1, 1)
    elseif color == "red" then
        fs:SetTextColor(1, 0.25, 0.2, 1)
    else
        fs:SetTextColor(1, 1, 1, 1)
    end

    return fs
end

local function CreateButton(parent, text, width, height, x, y, onClick)
    local btn = CreateFrame("Button", nil, parent, "UIPanelButtonTemplate")
    btn:SetSize(width or 100, height or 24)
    btn:SetPoint("TOPLEFT", parent, "TOPLEFT", x, y)
    btn:SetText(text or "Button")
    btn:SetScript("OnClick", function()
        if onClick then
            onClick()
        end
    end)
    return btn
end

local function CreateCheck(parent, text, x, y, checked, onClick)
    local cb = CreateFrame("CheckButton", nil, parent, "UICheckButtonTemplate")
    cb:SetSize(24, 24)
    cb:SetPoint("TOPLEFT", parent, "TOPLEFT", x, y)
    cb:SetChecked(checked or false)

    cb.text = cb:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
    cb.text:SetPoint("LEFT", cb, "RIGHT", 0, 0)
    cb.text:SetText(text or "")

    cb:SetScript("OnClick", function(self)
        if onClick then
            onClick(self:GetChecked())
        end
    end)

    return cb
end

-- ============================================================
-- Main Panel
-- ============================================================

function CreateCollectionFlexPanel()
    if CollectionFlexPanel then
        return CollectionFlexPanel
    end

    CollectionFlexPanel = CreateFrame("Frame", "GameSlayve_CollectionFlexPanel", UIParent)
    CollectionFlexPanel:SetSize(PANEL_WIDTH, PANEL_HEIGHT)
    CollectionFlexPanel:SetPoint("CENTER", UIParent, "CENTER", 0, 0)
    CollectionFlexPanel:SetFrameStrata("DIALOG")
    CollectionFlexPanel:EnableMouse(true)
    CollectionFlexPanel:SetMovable(true)
    CollectionFlexPanel:RegisterForDrag("LeftButton")
    CollectionFlexPanel:Hide()

    CollectionFlexPanel:SetScript("OnDragStart", function(self)
        if not self.locked then
            self:StartMoving()
        end
    end)

    CollectionFlexPanel:SetScript("OnDragStop", function(self)
        self:StopMovingOrSizing()
    end)

    -- ========================================================
    -- Backdrop
    -- ========================================================

    CollectionFlexPanel.fallback = CollectionFlexPanel:CreateTexture(nil, "BACKGROUND")
    CollectionFlexPanel.fallback:SetAllPoints(CollectionFlexPanel)
    CollectionFlexPanel.fallback:SetColorTexture(0.02, 0.02, 0.04, 0.92)

    CollectionFlexPanel.background = CollectionFlexPanel:CreateTexture(nil, "ARTWORK")
    CollectionFlexPanel.background:SetAllPoints(CollectionFlexPanel)
    CollectionFlexPanel.background:SetTexture(BACKDROP_TEXTURE)
    CollectionFlexPanel.background:SetTexCoord(0, 1, 0, 1)
    CollectionFlexPanel.background:SetVertexColor(1, 1, 1, 1)

    -- ========================================================
    -- Header
    -- ========================================================

    CreateText(CollectionFlexPanel, "COLLECTION FLEX", 24, 90, -28, "gold")
    CreateText(CollectionFlexPanel, "Mounts. Pets. Friends. Flex Together.", 12, 92, -58, "white")

    local close = CreateFrame("Button", nil, CollectionFlexPanel, "UIPanelCloseButton")
    close:SetSize(28, 28)
    close:SetPoint("TOPRIGHT", CollectionFlexPanel, "TOPRIGHT", -14, -14)
    close:SetScript("OnClick", function()
        CollectionFlexPanel:Hide()
    end)

    -- ========================================================
    -- Top Buttons
    -- ========================================================

    CreateButton(CollectionFlexPanel, "Party", 75, 24, 300, -30, function() CF_Print("Party mode selected.") end)
    CreateButton(CollectionFlexPanel, "Match", 75, 24, 380, -30, function() CF_Print("Match mode selected.") end)
    CreateButton(CollectionFlexPanel, "Chain", 75, 24, 460, -30, function() CF_Print("Chain mode selected.") end)
    CreateButton(CollectionFlexPanel, "Random", 75, 24, 540, -30, function() CF_Print("Random mode selected.") end)
    CreateButton(CollectionFlexPanel, "Primer", 75, 24, 620, -30, function() CF_Print("Primer mode selected.") end)

    -- ========================================================
    -- Left Mode Buttons
    -- ========================================================

    CreateText(CollectionFlexPanel, "FLEX MODE", 13, 28, -92, "blue")

    CreateButton(CollectionFlexPanel, "Mount Flex", 110, 24, 25, -120, function() CF_Print("Mount Flex selected.") end)
    CreateButton(CollectionFlexPanel, "Pet Flex", 110, 24, 25, -150, function() CF_Print("Pet Flex selected.") end)
    CreateButton(CollectionFlexPanel, "Match", 110, 24, 25, -180, function() CF_Print("Match selected.") end)
    CreateButton(CollectionFlexPanel, "Chain", 110, 24, 25, -210, function() CF_Print("Chain selected.") end)
    CreateButton(CollectionFlexPanel, "Random", 110, 24, 25, -240, function() CF_Print("Random selected.") end)
    CreateButton(CollectionFlexPanel, "Settings", 110, 24, 25, -270, function() CF_Print("Settings coming soon.") end)

    -- ========================================================
    -- Main Match Area
    -- ========================================================

    CreateText(CollectionFlexPanel, "TARGET", 14, 210, -96, "blue")
    CreateText(CollectionFlexPanel, "No target selected.", 12, 205, -130, "white")

    CreateText(CollectionFlexPanel, "YOU", 14, 460, -96, "blue")
    CreateText(CollectionFlexPanel, "Your mount/pet here.", 12, 445, -130, "white")

    CreateText(CollectionFlexPanel, "MATCH!", 26, 345, -160, "purple")

    CreateButton(CollectionFlexPanel, "Match Target", 150, 28, 320, -215, function()
        CF_Print("Match Target clicked.")
    end)

    CreateCheck(CollectionFlexPanel, "Auto Accept", 320, -250, false, function(checked)
        CF_Print("Auto Accept: " .. tostring(checked))
    end)

    -- ========================================================
    -- Party Flex
    -- ========================================================

    CreateText(CollectionFlexPanel, "PARTY FLEX", 14, 580, -96, "blue")
    CreateText(CollectionFlexPanel, "0 / 5", 12, 690, -96, "gold")
    CreateText(CollectionFlexPanel, "No party data yet.", 12, 580, -130, "white")

    CreateButton(CollectionFlexPanel, "Invite", 80, 24, 580, -245, function() CF_Print("Invite clicked.") end)
    CreateButton(CollectionFlexPanel, "Disband", 80, 24, 665, -245, function() CF_Print("Disband clicked.") end)

    -- ========================================================
    -- Middle Panels
    -- ========================================================

    CreateText(CollectionFlexPanel, "MOUNT CHAIN", 14, 160, -315, "blue")
    CreateText(CollectionFlexPanel, "Step 0 of 5", 12, 160, -345, "purple")
    CreateText(CollectionFlexPanel, "Chain progress will appear here.", 11, 160, -370, "white")

    CreateText(CollectionFlexPanel, "PRIMER", 14, 400, -315, "blue")
    CreateText(CollectionFlexPanel, "00:00", 24, 405, -350, "purple")
    CreateText(CollectionFlexPanel, "Time Left", 11, 410, -385, "white")

    CreateText(CollectionFlexPanel, "MISSING", 14, 525, -315, "blue")
    CreateText(CollectionFlexPanel, "0", 24, 545, -350, "red")
    CreateText(CollectionFlexPanel, "Missing Mounts", 11, 515, -385, "white")

    -- ========================================================
    -- Chat / History Area
    -- ========================================================

    CreateButton(CollectionFlexPanel, "Party Chat", 90, 24, 585, -315, function() CF_Print("Party Chat tab.") end)
    CreateButton(CollectionFlexPanel, "Flex History", 90, 24, 680, -315, function() CF_Print("Flex History tab.") end)

    CreateText(CollectionFlexPanel, "System: Collection Flex ready.", 11, 590, -355, "purple")
    CreateText(CollectionFlexPanel, "GameSlayve: Let's flex together.", 11, 590, -378, "gold")

    -- ========================================================
    -- Bottom Panels
    -- ========================================================

    CreateText(CollectionFlexPanel, "COLLECTION OVERVIEW", 13, 28, -430, "blue")
    CreateText(CollectionFlexPanel, "Total Mounts: --", 11, 32, -460, "white")
    CreateText(CollectionFlexPanel, "Favorites: --", 11, 32, -480, "white")
    CreateButton(CollectionFlexPanel, "View Collection", 130, 24, 32, -500, function() CF_Print("View Collection clicked.") end)

    CreateText(CollectionFlexPanel, "FAVORITES", 13, 250, -430, "blue")
    CreateText(CollectionFlexPanel, "Favorite mounts and pets will show here.", 11, 250, -460, "white")

    CreateText(CollectionFlexPanel, "QUICK ACTIONS", 13, 500, -430, "blue")
    CreateButton(CollectionFlexPanel, "Summon Favorite", 140, 24, 500, -460, function() CF_Print("Summon Favorite clicked.") end)
    CreateButton(CollectionFlexPanel, "Random Mount", 140, 24, 500, -490, function() CF_Print("Random Mount clicked.") end)

    -- ========================================================
    -- Footer Options
    -- ========================================================

    CreateCheck(CollectionFlexPanel, "Lock", 610, -525, false, function(checked)
        CollectionFlexPanel.locked = checked and true or false
        CF_Print("Panel lock: " .. tostring(CollectionFlexPanel.locked))
    end)

    CreateText(CollectionFlexPanel, "v0.1.0 Foundation", 10, 28, -545, "white")

    return CollectionFlexPanel
end
