-- Create placeholders for the required variables
local decksPanel
local flashcardCountText
local deckPanelTitle
local deckScrollFrame
local deckScrollChild

-----------------------------
-- POP FOR DELETION OF DECK
-----------------------------
StaticPopupDialogs["WOWSTUDY_CONFIRM_DELETE_DECK"] = {
    text = "Are you sure you want to delete this deck?\nAll cards in it will be lost along with their statistics.",
    button1 = "Yes",
    button2 = "No",
    OnAccept = function(self, data)
        -- data will contain deckName
        local deckName = data.deckName
        WoWStudySaved.decks[deckName] = nil

        -- Possibly reload checkboxes or refresh the panel
        WoWStudyFunction.UpdateSelectedFlashcards()
        WoWStudyFunction.CreateDeckCheckboxes()
        WoWStudyFunction.UpdateLoadedFlashcardsDeckPanelDisplay()
    end,
    OnCancel = function(self)
        -- user canceled
    end,
    timeout = 0,
    whileDead = true,
    hideOnEscape = true,
    preferredIndex = 3, -- avoid Blizzard's basic dialogs
}

--------------------------------------------------
-- Deck Selection Panel
--------------------------------------------------

----------------------------------------------------------------
-- Function to create checkboxes inside the scroll child
----------------------------------------------------------------
function WoWStudyFunction.CreateDeckCheckboxes()
    for i, child in ipairs({deckScrollChild:GetChildren()}) do
        child:Hide()
        child:ClearAllPoints()
        child:SetParent(nil)
    end

    local offsetY = -10  -- Start a little down from the top
    for deckName, deck in pairs(WoWStudySaved.decks) do
        local check = CreateFrame("CheckButton", nil, deckScrollChild, "UICheckButtonTemplate")
        check:SetPoint("TOPLEFT", 10, offsetY)

        -- Move down for the next checkbox
        offsetY = offsetY - 30

        -- Label for the checkbox
        check.text = check.Text
        check.text:SetText(deckName)
        check.text:SetFontObject("GameFontNormal")
        -- Load the saved state from WoWStudySaved (true/false)
        check:SetChecked(deck.isSelected)

        -- Handle click
        check:SetScript("OnClick", function(self)
            local isChecked = self:GetChecked()

            if isChecked and WoWStudySaved.decks[deckName] then
                WoWStudySaved.decks[deckName].isSelected = true
            elseif not isChecked and WoWStudySaved.decks[deckName] then
                WoWStudySaved.decks[deckName].isSelected = false
            end

            WoWStudyFunction.UpdateSelectedFlashcards()
            WoWStudyFunction.UpdateLoadedFlashcardsDeckPanelDisplay()
        end)

        if deck.source == "internal" then
            local deleteButton = CreateFrame("Button", nil, deckScrollChild, "UIPanelButtonTemplate")
            deleteButton:SetSize(70, 22)
            deleteButton:SetText("Delete")
            deleteButton:SetPoint("LEFT", check, "RIGHT", 150, 0)

            -- OnClick -> confirm, then delete
            deleteButton:SetScript("OnClick", function()
                StaticPopup_Show("WOWSTUDY_CONFIRM_DELETE_DECK", nil, nil, { deckName = deckName })
            end)
        else
            -- External deck, load message saying loaded externally
            local externalText = deckScrollChild:CreateFontString(nil, "OVERLAY", "GameFontNormal")
            externalText:SetPoint("LEFT", check, "RIGHT", 150, 0)
            externalText:SetText("Loaded from file")
        end
    end

    -- Adjust the scrollChild's height so the scroll bar works
    deckScrollChild:SetHeight(math.abs(offsetY))
end

-- Create the deck selection panel
function WoWStudyFunction.SetupDecksPanel(menuFrame, menuPanel2)

    decksPanel = CreateFrame("Frame", WoWStudyDecksPanel, menuFrame, "ShadowOverlayTemplate")
    decksPanel:SetPoint("LEFT", menuPanel2, "RIGHT", 10, 0)
    decksPanel:SetSize(600, 489)
    decksPanel:Hide()

    flashcardCountText = decksPanel:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
    flashcardCountText:SetPoint("TOPLEFT", decksPanel, "TOPLEFT", 10, -10)
    flashcardCountText:SetText("Flashcards loaded: 0")

    deckPanelTitle = decksPanel:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
    deckPanelTitle:SetPoint("TOPLEFT", flashcardCountText, "TOPLEFT", 0, -30)
    deckPanelTitle:SetText("Decks")

    -- Create a scroll frame for deck checkboxes
    deckScrollFrame = CreateFrame("ScrollFrame", "WoWStudyDeckScrollFrame", decksPanel, "UIPanelScrollFrameTemplate")
    deckScrollFrame:SetPoint("TOPLEFT", deckPanelTitle, "TOPLEFT", 10, -40)
    deckScrollFrame:SetPoint("BOTTOMRIGHT", decksPanel, "BOTTOMRIGHT", -50, 90)

    deckScrollChild = CreateFrame("Frame", "WoWStudyDeckScrollChild", deckScrollFrame)
    deckScrollChild:SetSize(deckScrollFrame:GetWidth(), 1)
    deckScrollFrame:SetScrollChild(deckScrollChild)

    decksPanel:SetScript("OnShow", function()
        -- Create the checkboxes for the decks
        WoWStudyFunction.UpdateLoadedFlashcardsDeckPanelDisplay()
        WoWStudyFunction.CreateDeckCheckboxes()
    end)

    return decksPanel
end

function WoWStudyFunction.UpdateLoadedFlashcardsDeckPanelDisplay()
    flashcardCountText:SetText("Flashcards loaded: " .. #WoWStudyGlobal.selectedFlashcards)
end