-- Create the main frame
local frame = CreateFrame("Frame", "LootTableFrame", UIParent, "BasicFrameTemplateWithInset")
frame:SetSize(800, 600) -- Width, Height
frame:SetPoint("CENTER") -- Position in the center of the screen

frame.title = frame:CreateFontString(nil, "OVERLAY")
frame.title:SetFontObject("GameFontHighlightLarge")
frame.title:SetPoint("CENTER", frame.TitleBg, "CENTER", 0, 0)
frame.title:SetText("Loot Table UI")

-- Colors for crests
local crestColors = {
    [""] = {r = 0.8, g = 0.8, b = 0.8}, -- Gray for empty
    ["Weathered"] = {r = 0.9, g = 0.8, b = 0.6}, -- Yellow
    ["Carved"] = {r = 0.6, g = 0.8, b = 0.6}, -- Green
    ["Harbinger"] = {r = 0.6, g = 0.6, b = 0.9}, -- Blue
    ["Gilded"] = {r = 0.8, g = 0.6, b = 0.9}, -- Purple
}

-- Define tab names
local tabNames = {"Main Table", "Mythic+ Table", "Dungeons", "Delves Table", "Crafted Gear"}

local contentFrames = {}
-- Function to show content based on selected tab
local function ShowTable(name)
    -- Hide all content frames
    for _, content in pairs(contentFrames) do
        content:Hide()
    end
    -- Show the selected table
    contentFrames[name]:Show()
end
-- Create tabs
local tabButtons = {}
for i, name in ipairs(tabNames) do
    local tab = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
    tab:SetSize(120, 30)
    tab:SetPoint("TOPLEFT", frame, "TOPLEFT", 10 + (130 * (i - 1)), -30)
    tab:SetText(name)
    tab:SetScript("OnClick", function() ShowTable(name) end)
    tabButtons[name] = tab
end






-- Function to create and populate a content frame
local function CreateContentFrame(name, data, headers)
    local frame = CreateFrame("Frame", nil, frame)
    frame:SetSize(780, 500)
    frame:SetPoint("TOPLEFT", 10, -70)
    frame:Hide() -- Initially hidden

    local scrollFrame = CreateFrame("ScrollFrame", nil, frame, "UIPanelScrollFrameTemplate")
    scrollFrame:SetSize(760, 450)
    scrollFrame:SetPoint("TOPLEFT", 10, -10)

    local contentFrame = CreateFrame("Frame", nil, scrollFrame)
    contentFrame:SetSize(760, 800) -- Adjust size based on content
    scrollFrame:SetScrollChild(contentFrame)

    -- Create headers
    for i, header in ipairs(headers) do
        local headerText = contentFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        headerText:SetPoint("TOPLEFT", contentFrame, "TOPLEFT", 100 * (i - 1), -10)
        headerText:SetText(header)
    end

    -- Function to create rows
    local function CreateRow(index, rowData, color)
        local yOffset = -30 * index
        local bgTexture = contentFrame:CreateTexture(nil, "BACKGROUND")
        bgTexture:SetColorTexture(color.r, color.g, color.b, 0.2)
        bgTexture:SetPoint("TOPLEFT", contentFrame, "TOPLEFT", 0, yOffset)
        bgTexture:SetSize(760, 30)

        for i, value in ipairs(rowData) do
            local text = contentFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
            text:SetPoint("TOPLEFT", contentFrame, "TOPLEFT", 100 * (i - 1), yOffset)
            text:SetText(value)
        end
    end

    -- Populate table with data
    for i, rowData in ipairs(data) do
        local crestName = rowData[2] -- Assuming crest is in the second column
        local color = crestColors[crestName] or {r = 0.8, g = 0.8, b = 0.8} -- Default to gray if crest not found
        CreateRow(i, rowData, color)
    end

    contentFrames[name] = frame
end

-- Example data for each table
local mainTableData = {
    {567, "", "Explorer 1", "Explorer 1"},
    {567, "", "Explorer 2", "Explorer 2"},
    {567, "", "Explorer 3", "Explorer 3"},
    {567, "", "Explorer 4", "Explorer 4"},
    {580, "", "Explorer 5", "Adventurer 1"},
    {580, "", "Explorer 6", "Adventurer 2"},
    {580, "", "Explorer 7", "Adventurer 3"},
    {580, "", "Explorer 8", "Adventurer 4"},
    {584, "Weathered", "LFR", "Adventurer 5, Veteran 1"},
    {587, "Weathered", "LFR", "Adventurer 6, Veteran 2"},
    {590, "Weathered", "LFR", "Adventurer 7, Veteran 3"},
    {593, "Weathered", "LFR", "Adventurer 8, Veteran 4"},
    {597, "Carved", "Normal", "Veteran 5, Champion 1"},
    {600, "Carved", "Normal", "LFR", "Veteran 6, Champion 2"},
    {603, "Carved", "Normal", "Veteran 7, Champion 3"},
    {606, "Carved", "Normal", "Veteran 8, Champion 4"},
    {610, "Harbinger", "Heroic", "Champion 5, Hero 1"},
    {613, "Harbinger", "Heroic", "Normal", "Champion 6, Hero 2"},
    {616, "Harbinger", "Heroic", "Champion 7, Hero 3"},
    {619, "Harbinger", "Heroic", "Champion 8, Hero 4"},
    {623, "Gilded", "Mythic", "Hero 5, Myth 1"},
    {626, "Gilded", "Mythic", "Heroic", "Hero 6, Myth 2"},
    {629, "Gilded", "Mythic", "Myth 3"},
    {632, "Gilded", "Mythic", "Myth 4"},
    {635, "Gilded", "Mythic", "Myth 5"},
    {639, "Gilded", "Mythic", "Myth 6"},
}

local mythicTableData = {
    {2, 597, 606, "Carved"},
    {3, 597, 610, "Carved"},
    {4, 600, 610, "Harbinger"},
    {5, 603, 613, "Harbinger"},
    {6, 606, 613, "Harbinger"},
    {7, 610, 616, "Harbinger"},
    {8, 610, 619, "Harbinger"},
    {9, 613, 619, "Gilded"},
    {10, 613, 623, "Gilded"},
}

local dungeonsData = {
    {"Normal", 554, "-"},
    {"Heroic", 580, 593},
    {"Mythic 0", 593, 603},
}

local delvesData = {
    {2, "?", 584},
    {3, "?", 587},
    {4, "?", 597},
    {5, "?", 600},
    {6, "?", 606},
    {7, "?", 610},
    {8, "?", 616},
    {9, "?", 619},
    {10, "?", 623},
    {11, "?", "?"},
    {12, "?", "?"},
}

local craftedGearData = {
    {"Spark", 584, 597},
    {"Harbinger", 606, 619},
    {"Gilded", 616, 629},
}

-- Create content frames for each table
CreateContentFrame("Main Table", mainTableData, {"iLevel", "Crests", "Raid", "Raid (Very Rare)", "Upgrade Track"})
CreateContentFrame("Mythic+ Table", mythicTableData, {"Key", "Loot", "Vault", "Crests"})
CreateContentFrame("Dungeons", dungeonsData, {"Level", "Loot", "Vault"})
CreateContentFrame("Delves Table", delvesData, {"Level", "Loot", "Vault"})
CreateContentFrame("Crafted Gear", craftedGearData, {"Level", "Loot", "Vault"})