local addonName, SinsToolkit = ...

SinsToolkit.UI = SinsToolkit.UI or {}
SinsToolkit.UI.Pages = SinsToolkit.UI.Pages or {}

local PageBase = SinsToolkit.UI.Pages.Base or {}
local Helpers = SinsToolkit.UI.Pages.Helpers or {}
local Page = {}
setmetatable(Page, { __index = PageBase })
SinsToolkit.UI.Pages.BugCatcher = Page
Page.density = Helpers.GetDefaultDensity and Helpers.GetDefaultDensity() or "compact"

function Page:Create(CreateScrollablePage, Window)
    local getGV = Helpers.GetGV
    local setGV = Helpers.SetGV
    local Theme = Helpers.GetTheme and Helpers.GetTheme()

    local bugPage = Helpers.CreateContentPage(Window, "Bug Catcher")
    local bgHeader = Window:CreateSectionHeader(bugPage, "Bug Catcher")

    local bgIntro = bugPage:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
    bgIntro:SetPoint("TOPLEFT", bgHeader, "BOTTOMLEFT", 0, -8)
    bgIntro:SetJustifyH("LEFT")
    bgIntro:SetWidth(720)
    bgIntro:SetJustifyH("LEFT")
    bgIntro:SetText("Capture Lua errors, protected-action errors, and Lua warnings into a compact Toolkit window. The minimap bag can stay visible or only appear when errors exist.")
    if Theme and Theme.ApplyPageDescription then Theme.ApplyPageDescription(bgIntro) end

    local cbBugEn = Window:CreateCheckbox(bugPage, "Enable Bug Catcher", getGV("bugCatcherEnabled"), setGV("bugCatcherEnabled"), "Captures Lua errors, protected-action errors, and Lua warnings inside Sin's Toolkit.")
    cbBugEn:SetPoint("TOPLEFT", bgIntro, "BOTTOMLEFT", 0, -16)

    local cbBugMM = Window:CreateCheckbox(bugPage, "Minimap Icon", getGV("bugCatcherMinimap"), setGV("bugCatcherMinimap"), "Shows the Bug Catcher minimap button. Click to show the last error, Shift-click to reload UI, Alt-click to clear errors, Right-click to hide the button.")
    cbBugMM:SetPoint("TOPLEFT", cbBugEn, "BOTTOMLEFT", 0, -8)

    local cbBugMMO = Window:CreateCheckbox(bugPage, "Only Show With Errors", getGV("bugCatcherMinimapOnlyErrors"), setGV("bugCatcherMinimapOnlyErrors"), "Keeps the Bug Catcher minimap button hidden while there are no stored errors. It appears automatically after an error is captured.")
    cbBugMMO:SetPoint("TOPLEFT", cbBugMM, "BOTTOMLEFT", 0, -8)

    local btnBugSound = Window:CreateDropdownMenu(bugPage, "Error Sound", {
        { value = "NONE", text = "None" },
        { value = "RAID_WARNING", text = "Raid Warning" },
        { value = "ALARM", text = "Alarm" },
        { value = "WHISPER", text = "Whisper" },
        { value = "READY_CHECK", text = "Ready Check" },
        { value = "BUGSACK_FATALITY", text = "BugSack: Fatality" },
    }, getGV("bugCatcherSound"), setGV("bugCatcherSound"), 190, "Choose the sound Sin's Toolkit plays when Bug Catcher captures a Lua error.")
    btnBugSound:SetPoint("TOPLEFT", cbBugMMO, "BOTTOMLEFT", 4, -20)

    local bBugOpen = Window:CreateButton(bugPage, "Open Catcher Window", 180, 30, function() if SinsToolkit.BugCatcher then SinsToolkit.BugCatcher:ShowLast() end end, "Opens the Bug Catcher window and displays the most recent captured error.")
    bBugOpen:SetPoint("TOPLEFT", btnBugSound, "BOTTOMLEFT", -4, -20)

    local bBugTest = Window:CreateButton(bugPage, "Test Error", 180, 30, function() if SinsToolkit.BugCatcher and SinsToolkit.BugCatcher.RunSelfTest then SinsToolkit.BugCatcher:RunSelfTest() end end, "Captures a safe test entry, opens Bug Catcher, and plays the configured alert sound without generating a real Lua error.")
    bBugTest:SetPoint("LEFT", bBugOpen, "RIGHT", 10, 0)

    self.frame = bugPage
    return bugPage
end

Page:Register("Bug Catcher")

return Page
