-- Create placeholders for the required variables
local killsCheck
local questsCheck
local killsEditBox
local timerEditBox
local deathCheck

---
-- Update Setting Checkbox States in the Trigger panel
---
function WoWStudyFunction.UpdateSettingCheckboxes()
    -- Set each checkbox to reflect the saved state
    killsCheck:SetChecked(WoWStudySaved.settings.enableKills)
    questsCheck:SetChecked(WoWStudySaved.settings.enableQuests)
    deathCheck:SetChecked(WoWStudySaved.settings.enableOnDeath)
    killsEditBox:SetText(WoWStudySaved.settings.killInterval)
    timerEditBox:SetText(WoWStudySaved.settings.timerValue)
end

function WoWStudyFunction.SetupFlashcardTriggersPanel(menuFrame, menuPanel2)
    local flashcardTriggersPanel = CreateFrame("Frame", flashcardTriggersPanel, menuFrame, "ShadowOverlayTemplate")
    flashcardTriggersPanel:SetPoint("LEFT", menuPanel2, "RIGHT", 10, 0)
    flashcardTriggersPanel:SetSize(600, 489)
    flashcardTriggersPanel:Hide()

    local title = flashcardTriggersPanel:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
    title:SetPoint("TOP", flashcardTriggersPanel, "TOP", 10, -10)
    title:SetText("Flashcard Triggers")

    -- "Quests" checkbox to toggle flashcards on quest events
    questsCheck = CreateFrame("CheckButton", "WoWStudyQuestsCheck", flashcardTriggersPanel, "UICheckButtonTemplate")
    questsCheck:SetPoint("TOPLEFT", flashcardTriggersPanel, "TOPLEFT", 20, -40)
    questsCheck.text = _G[questsCheck:GetName().."Text"]
    questsCheck.text:SetText("Quests")
    questsCheck:SetChecked(WoWStudySaved.settings.enableQuests or true)
    -- Tooltip for the checkbox
    questsCheck:SetScript("OnEnter", function(self)
        GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
        GameTooltip:SetText("Show flashcards on quest events such as accepting or completing a quest.")
        GameTooltip:Show()
    end)
    questsCheck:SetScript("OnLeave", function(self)
        GameTooltip:Hide()
    end)

    questsCheck:SetScript("OnClick", function(self)
        WoWStudySaved.settings.enableQuests = self:GetChecked()
    end)

    gatheringCheck = CreateFrame("CheckButton", "WoWStudyGatheringCheck", flashcardTriggersPanel, "UICheckButtonTemplate")
    gatheringCheck:SetPoint("TOPLEFT", questsCheck, "TOPLEFT", 0, -40)
    gatheringCheck.text = _G[gatheringCheck:GetName().."Text"]
    gatheringCheck.text:SetText("Gathering")
    gatheringCheck:SetChecked(WoWStudySaved.settings.enableGathering or true)
    -- Tooltip for the checkbox
    gatheringCheck:SetScript("OnEnter", function(self)
        GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
        GameTooltip:SetText("Show flashcards on gathering events.")
        GameTooltip:Show()
    end)
    gatheringCheck:SetScript("OnLeave", function(self)
        GameTooltip:Hide()
    end)
    gatheringCheck:SetScript("OnClick", function(self)
        WoWStudySaved.settings.enableGathering = self:GetChecked()
    end)
    
    deathCheck = CreateFrame("CheckButton", "WoWStudyDeathCheck", flashcardTriggersPanel, "UICheckButtonTemplate")
    deathCheck:SetPoint("TOPLEFT", gatheringCheck, "TOPLEFT", 0, -40)
    deathCheck.text = _G[deathCheck:GetName().."Text"]
    deathCheck.text:SetText("Deaths")
    deathCheck:SetChecked(WoWStudySaved.settings.enableOnDeath or true)
    -- Tooltip for the checkbox
    deathCheck:SetScript("OnEnter", function(self)
        GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
        GameTooltip:SetText("Show flashcards on death events.")
        GameTooltip:Show()
    end)
    deathCheck:SetScript("OnLeave", function(self)
        GameTooltip:Hide()
    end)
    deathCheck:SetScript("OnClick", function(self)
        WoWStudySaved.settings.enableOnDeath = self:GetChecked()
    end)

    -- "Kills" checkbox to toggle flashcards on kills
    killsCheck = CreateFrame("CheckButton", "WoWStudyKillsCheck", flashcardTriggersPanel, "UICheckButtonTemplate")
    killsCheck:SetPoint("TOPLEFT", deathCheck, "TOPLEFT", 0, -40)
    killsCheck.text = _G[killsCheck:GetName().."Text"]
    killsCheck.text:SetText("Kills")
    killsCheck:SetChecked(WoWStudySaved.settings.enableKills or true)

    killsCheck:SetScript("OnClick", function(self)
        WoWStudySaved.settings.enableKills = self:GetChecked()
    end)
    -- Tooltip for the checkbox
    killsCheck:SetScript("OnEnter", function(self)
        GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
        GameTooltip:SetText("Show flashcards on every X kills, X set by below setting.")
        GameTooltip:Show()
    end)
    killsCheck:SetScript("OnLeave", function(self)
        GameTooltip:Hide()
    end)

    local killsLabel = flashcardTriggersPanel:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    killsLabel:SetPoint("TOPLEFT", killsCheck, "TOPLEFT", 0, -40)
    killsLabel:SetText("Every X Kills:")

    killsEditBox = CreateFrame("EditBox", "WoWStudyKillsEditBox", flashcardTriggersPanel, "InputBoxTemplate")
    killsEditBox:SetSize(60, 20)
    killsEditBox:SetPoint("LEFT", killsLabel, "RIGHT", 10, 0)
    killsEditBox:SetAutoFocus(false)
    killsEditBox:SetNumeric(true)
    killsEditBox:SetMaxLetters(3)
    killsEditBox:SetText(WoWStudySaved.settings.killInterval or "1")

    -- Add tooltip
    killsEditBox:SetScript("OnEnter", function(self)
        GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
        GameTooltip:SetText("Set the number of kills between each flashcard.  Press Enter to save.")
        GameTooltip:Show()
    end)

    killsEditBox:SetScript("OnLeave", function(self)
        GameTooltip:Hide()
    end)

    killsEditBox:SetScript("OnEnterPressed", function(self)
        local val = tonumber(self:GetText()) or 1
        WoWStudySaved.settings.killInterval = val
        WoWStudyGlobal.kills = 0
        self:ClearFocus()
    end)

    killsEditBox:SetScript("OnEscapePressed", function(self)
        self:SetText(WoWStudySaved.settings.killInterval or "1")
        self:ClearFocus()
    end)

    -- Timer and Kills Edit Boxes
    local timerLabel = flashcardTriggersPanel:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    timerLabel:SetPoint("TOPLEFT", killsLabel, "TOPLEFT", 0, -40)
    timerLabel:SetText("Timer (seconds):")

    timerEditBox = CreateFrame("EditBox", "WoWStudyTimerEditBox", flashcardTriggersPanel, "InputBoxTemplate")
    timerEditBox:SetSize(60, 20)
    timerEditBox:SetPoint("LEFT", timerLabel, "RIGHT", 10, 0)
    timerEditBox:SetAutoFocus(false)
    timerEditBox:SetNumeric(true)  -- If you only want numeric input
    timerEditBox:SetMaxLetters(4)  -- e.g., 4 digits max: 9999 seconds
    timerEditBox:SetText(WoWStudySaved.settings.timerValue or "60") -- default if nil

    -- Save changes when user presses Enter
    timerEditBox:SetScript("OnEnterPressed", function(self)
        local val = tonumber(self:GetText()) or 60
        WoWStudySaved.settings.timerValue = val
        self:ClearFocus()
    end)

    -- Optionally discard changes if Escape
    timerEditBox:SetScript("OnEscapePressed", function(self)
        self:SetText(WoWStudySaved.settings.timerValue or "60")
        self:ClearFocus()
    end)

    -- Add tooltip explaining timer
    timerEditBox:SetScript("OnEnter", function(self)
        GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
        GameTooltip:SetText("Set the timer for how long a flashcard is displayed.  Press Enter to save.")
        GameTooltip:Show()
    end)

    timerEditBox:SetScript("OnLeave", function(self)
        GameTooltip:Hide()
    end)

    -- Create placeholders
    local placeholder = flashcardTriggersPanel:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    placeholder:SetPoint("TOPLEFT", timerLabel, "TOPLEFT", 0, -60)
    placeholder:SetText("Coming soon:")

    local placeholder2 = flashcardTriggersPanel:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    placeholder2:SetPoint("TOPLEFT", placeholder, "TOPLEFT", 0, -40)
    placeholder2:SetText("Steps")

    local placeholder3 = flashcardTriggersPanel:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    placeholder3:SetPoint("TOPLEFT", placeholder2, "TOPLEFT", 0, -40)
    placeholder3:SetText("Flights")

    local placeholder5 = flashcardTriggersPanel:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    placeholder5:SetPoint("TOPLEFT", placeholder3, "TOPLEFT", 0, -40)
    placeholder5:SetText("Challenges")


    -- on show
    flashcardTriggersPanel:SetScript("OnShow", function()
        WoWStudyFunction.UpdateSettingCheckboxes()
    end)
    return flashcardTriggersPanel

end





