--------------------------------------------------------------------
-- Analytics panel shows the last 50 sessions and the top 50 sessions
--------------------------------------------------------------------
function WoWStudyFunction.SetupAnalyticsPanel(menuFrame, menuPanel2)
    -- Main panel for analytics
    local analyticsPanel = CreateFrame("Frame", "WoWStudyAnalyticsPanel", menuFrame, "ShadowOverlayTemplate")
    analyticsPanel:SetPoint("LEFT", menuPanel2, "RIGHT", 10, 0)
    analyticsPanel:SetSize(600, 489)
    analyticsPanel:Hide()

    local title = analyticsPanel:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
    title:SetPoint("TOP", analyticsPanel, "TOP", 0, -10)
    title:SetText("Analytics")

    local flashcardCountText = analyticsPanel:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    flashcardCountText:SetPoint("TOPLEFT", analyticsPanel, "TOPLEFT", 20, -40)
    flashcardCountText:SetText("Flashcards done all time: " .. WoWStudySaved.userInformation.lifetimeAnswered)

    --------------------------------------------------------
    -- Last 50 Sessions Scroll Frame
    --------------------------------------------------------

    local last50ScrollFrame = CreateFrame("ScrollFrame", nil, analyticsPanel, "UIPanelScrollFrameTemplate")
    last50ScrollFrame:SetPoint("TOPLEFT", analyticsPanel, "TOPLEFT", 20, -100)
    last50ScrollFrame:SetSize(250, 300) -- Adjust size as needed

    local scrollChildLast50ScrollFrame = CreateFrame("Frame", nil, last50ScrollFrame)
    scrollChildLast50ScrollFrame:SetSize(last50ScrollFrame:GetWidth(), 1) -- Height will be adjusted dynamically
    last50ScrollFrame:SetScrollChild(scrollChildLast50ScrollFrame)

    local last50Title = scrollChildLast50ScrollFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    last50Title:SetPoint("TOPLEFT", 0, 0)
    last50Title:SetText("Last 50 Sessions:")

    local offsetY = -20
    local spacing = 20

    local count = 0

    for i = #WoWStudySaved.sessions, 1, -1 do
        if count >= 50 then
            break
        end
        count = count + 1

        local session = WoWStudySaved.sessions[i]
        local sessionText = scrollChildLast50ScrollFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        sessionText:SetPoint("TOPLEFT", 0, offsetY)
        sessionText:SetText("Correct: " .. session.totalCorrect .. " Total: " .. session.totalAnswered .. " (" .. math.floor((session.totalCorrect / session.totalAnswered) * 100) .. "%)")
        offsetY = offsetY - spacing
    end

    -- Adjust the scroll child's height to match the content
    scrollChildLast50ScrollFrame:SetHeight(math.abs(offsetY))

    --------------------------------------------------------
    -- Top 50 Scores Scroll Frame
    --------------------------------------------------------

    local top50ScrollFrame = CreateFrame("ScrollFrame", nil, analyticsPanel, "UIPanelScrollFrameTemplate")
    top50ScrollFrame:SetPoint("TOPLEFT", last50ScrollFrame, "TOPRIGHT", 40, 0)
    top50ScrollFrame:SetSize(250, 300) -- Adjust size as needed

    local scrollChildTop50ScrollFrame = CreateFrame("Frame", nil, top50ScrollFrame)
    scrollChildTop50ScrollFrame:SetSize(top50ScrollFrame:GetWidth(), 1) -- Height will be adjusted dynamically
    top50ScrollFrame:SetScrollChild(scrollChildTop50ScrollFrame)

    local top50Title = scrollChildTop50ScrollFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    top50Title:SetPoint("TOPLEFT", 0, 0)
    top50Title:SetText("Top 50 (Correct count):")

    offsetY = -20
    local sortTop50Sessions = function(a, b)
        return a.totalCorrect > b.totalCorrect
    end

    local sessionCopy = {}
    for i, session in ipairs(WoWStudySaved.sessions) do
        sessionCopy[i] = session
    end

    table.sort(sessionCopy, sortTop50Sessions)

    for i, session in ipairs(sessionCopy) do
        if i > 50 then
            break
        end
        local sessionText = scrollChildTop50ScrollFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
        sessionText:SetPoint("TOPLEFT", 0, offsetY)
        sessionText:SetText("Correct: " .. session.totalCorrect .. " Total: " .. session.totalAnswered .. " (" .. math.floor((session.totalCorrect / session.totalAnswered) * 100) .. "%)")
        offsetY = offsetY - spacing
    end

    -- Adjust the scroll child's height to match the content
    scrollChildTop50ScrollFrame:SetHeight(math.abs(offsetY))

    function WoWStudyFunction.RefreshAnalyticsPanel()
        flashcardCountText:SetText("Flashcards done all time: " .. WoWStudySaved.userInformation.lifetimeAnswered)

        -- Update last 50 sessions
        local last50ScrollChildHeight = 20
        local count = 0
        for i = #WoWStudySaved.sessions, 1, -1 do
            if count >= 50 then
                break
            end
            count = count + 1
            local session = WoWStudySaved.sessions[i]
            local sessionText = scrollChildLast50ScrollFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
            sessionText:SetPoint("TOPLEFT", 0, -last50ScrollChildHeight)
            sessionText:SetText("Correct: " .. session.totalCorrect .. " Total: " .. session.totalAnswered .. " (" .. math.floor((session.totalCorrect / session.totalAnswered) * 100) .. "%)")
            last50ScrollChildHeight = last50ScrollChildHeight + spacing
        end
        scrollChildLast50ScrollFrame:SetHeight(last50ScrollChildHeight)

        -- Update top 50 sessions
        local top50ScrollChildHeight = 20
        local sortTop50Sessions = function(a, b)
            return a.totalCorrect > b.totalCorrect
        end
    
        local sessionCopy = {}
        for i, session in ipairs(WoWStudySaved.sessions) do
            sessionCopy[i] = session
        end

        table.sort(sessionCopy, sortTop50Sessions)

        for i, session in ipairs(sessionCopy) do
            if i > 50 then
                break
            end
            local sessionText = scrollChildTop50ScrollFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
            sessionText:SetPoint("TOPLEFT", 0, -top50ScrollChildHeight)
            sessionText:SetText("Correct: " .. session.totalCorrect .. " Total: " .. session.totalAnswered .. " (" .. math.floor((session.totalCorrect / session.totalAnswered) * 100) .. "%)")
            top50ScrollChildHeight = top50ScrollChildHeight + spacing
        end
        
        scrollChildTop50ScrollFrame:SetHeight(top50ScrollChildHeight)
    end

    analyticsPanel:SetScript("OnShow", function()
        WoWStudyFunction.RefreshAnalyticsPanel()
    end)

    return analyticsPanel
end