--------------------------------------------------------------------------
-- Menu Frame (contains Menu Panel and specifc Display Panels)
--------------------------------------------------------------------------
local menuFrame = CreateFrame("Frame", "WoWStudyTabbedFrame", UIParent, "BasicFrameTemplateWithInset")
menuFrame:SetSize(807, 529)
menuFrame:SetPoint("CENTER")
menuFrame:EnableMouse(true)
menuFrame:Hide()
menuFrame:SetMovable(true)
menuFrame:SetClampedToScreen(true)
menuFrame:RegisterForDrag("LeftButton")
menuFrame:SetScript("OnDragStart", menuFrame.StartMoving)
menuFrame:SetScript("OnDragStop", menuFrame.StopMovingOrSizing)

if menuFrame.TitleText then
    menuFrame.TitleText:SetText("WoWStudy")
    menuFrame.TitleText:SetTextColor(1, 0.82, 0, 1)  -- Gold color
    menuFrame.TitleText:ClearAllPoints()
    menuFrame.TitleText:SetPoint("CENTER", menuFrame.TitleBg, "CENTER", 6, 0)
end

--------------------------------------------------------------------------
-- Menu Panel inside Menu Frame
--------------------------------------------------------------------------

local menuPanel2 = CreateFrame("Frame", "WoWStudyMenuPanel", menuFrame, "ShadowOverlayTemplate")
menuPanel2:SetSize(164, 489)
menuPanel2:SetPoint("LEFT", menuFrame, "LEFT", 10, -10)
menuPanel2:EnableMouse(true)

-- Init menu panels
----------------------------
local flashcardCreationPanel
local decksPanel
local flashcardTriggersPanel
local flashcardDesignPanel
local flashcardStatsPanel
local analyticsPanel
local spacedRepetitionPanel

function WoWStudyFunction.InitMenuPanel()
    flashcardCreationPanel = WoWStudyFunction.SetupFlashcardCreationPanel(menuFrame, menuPanel2)
    decksPanel = WoWStudyFunction.SetupDecksPanel(menuFrame, menuPanel2)
    flashcardTriggersPanel = WoWStudyFunction.SetupFlashcardTriggersPanel(menuFrame, menuPanel2)
    flashcardDesignPanel = WoWStudyFunction.SetupFlashcardDesignPanel(menuFrame, menuPanel2)
    flashcardStatsPanel = WoWStudyFunction.SetupFlashcardStatsPanel(menuFrame, menuPanel2)
    analyticsPanel = WoWStudyFunction.SetupAnalyticsPanel(menuFrame, menuPanel2)
    spacedRepetitionPanel = WoWStudyFunction.SetupSpacedRepetitionPanel(menuFrame, menuPanel2)
end


function WoWStudyFunction.RefreshAnalyticsPanel()
    analyticsPanel = WoWStudyFunction.SetupAnalyticsPanel(menuFrame, menuPanel2)
end

-- Menu options inside of the menu panel (Decks, Flashcard triggers etc)
--------------------------------------------------------------------------

local function ShowSelectedPanel(panel)

    if flashcardCreationPanel then
        flashcardCreationPanel:Hide()
    end

    if decksPanel then
        decksPanel:Hide()
    end

    if flashcardTriggersPanel then
        flashcardTriggersPanel:Hide()
    end

    if flashcardDesignPanel then
        flashcardDesignPanel:Hide()
    end

    if flashcardStatsPanel then
        flashcardStatsPanel:Hide()
    end

    if analyticsPanel then
        analyticsPanel:Hide()
    end

    if spacedRepetitionPanel then
        spacedRepetitionPanel:Hide()
    end

    if panel == flashcardStatsPanel then
        if flashcardStatsPanel then
            flashcardStatsPanel:Hide()
        end
    end

    if panel then
        panel:Show()
    end
end

-- Create a clickable title for each option in the menu, function hides all panels then shows the selected panel
local menuOptions = {
    {name = "Flashcard Creation", func = function() ShowSelectedPanel(flashcardCreationPanel) end},
    {name = "Decks", func = function() ShowSelectedPanel(decksPanel) end},
    {name = "Flashcards", func = function() ShowSelectedPanel(flashcardStatsPanel) end},
    {name = "Flashcard Triggers", func = function() ShowSelectedPanel(flashcardTriggersPanel) end},
    {name = "Flashcard Design", func = function() ShowSelectedPanel(flashcardDesignPanel) end},
    {name = "Analytics", func = function() ShowSelectedPanel(analyticsPanel) end},
    {name = "Spaced Repetition", func = function() ShowSelectedPanel(spacedRepetitionPanel) end},
}

-- Variable to track the currently selected button
local selectedButton = nil
local defaultOption = nil

-- Add a light blue glow to the mouseover option
local function OnEnter(self)
    if self ~= selectedButton then
        self.bg:SetColorTexture(0, 0, 1, 0.3) -- Light blue glow
    end
end

local function OnLeave(self)
    if self ~= selectedButton then
        self.bg:SetColorTexture(0, 0, 0, 0) -- Transparent
    end
end

-- Function to handle button clicks
local function OnClick(self, optionFunc)
    -- Deselect the currently selected button
    if selectedButton then
        selectedButton.bg:SetColorTexture(0, 0, 0, 0) -- Reset to transparent
    end

    -- Set the clicked button as selected
    selectedButton = self
    self.bg:SetColorTexture(0, 0, 1, 0.6) -- Brighter blue to indicate selection

    -- Execute the option's function
    optionFunc()
end

-- Create a clickable title for each option in the menu
for i, option in ipairs(menuOptions) do
    -- Create a button to encapsulate the text and its background
    local button = CreateFrame("Button", nil, menuPanel2)
    button:SetSize(144, 20) -- Adjust size as needed
    button:SetPoint("TOPLEFT", 10, -20 - (i-1) * 30)

    -- Create the background texture
    local bg = button:CreateTexture(nil, "BACKGROUND")
    bg:SetAllPoints(button)
    bg:SetColorTexture(0, 0, 0, 0) -- Initially transparent
    button.bg = bg

    -- Create the font string for the title
    local title = button:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    title:SetPoint("LEFT", 5, 0)
    title:SetText(option.name)

    -- Set up mouseover and click functionality
    button:SetScript("OnEnter", OnEnter)
    button:SetScript("OnLeave", OnLeave)
    button:SetScript("OnMouseDown", function() OnClick(button, option.func) end)

    -- If this is the first optin, show it by default
    if i == 1 then
        defaultOption = { button = button, option = option }
        OnClick(button, option.func)
    end
end

menuFrame:SetScript("OnHide", function(self)
    flashcardCreationPanel:Hide()
    decksPanel:Hide()
    flashcardTriggersPanel:Hide()
    flashcardDesignPanel:Hide()
    flashcardStatsPanel:Hide()
    analyticsPanel:Hide()
    spacedRepetitionPanel:Hide()
end)

menuFrame:SetScript("OnShow", function(self)
    OnClick(defaultOption.button, defaultOption.option.func)
end)

WoWStudyUI.MenuFrame = menuFrame