local addonName, ns = ...
local L = ns.L

local cache = {}
local W = ns.Widgets
local C = ns.COLORS

function ns:InitStealthReminder()
    local p = ns.MainFrame.Content
    local db = NaowhQOL.stealthReminder
    local stealthDisplay = ns.StealthReminderDisplay
    local stanceDisplay = ns.StanceReminderDisplay

    if db.stanceSoundID and not db.stanceSound then
        db.stanceSound = { id = db.stanceSoundID }
    end

    W:CachedPanel(cache, "srFrame", p, function(f)
        local sf, sc = W:CreateScrollFrame(f, 800)

        W:CreatePageHeader(sc,
            {{"STEALTH", C.BLUE}, {"/ STANCE", C.ORANGE}},
            L["STEALTH_SUBTITLE"])

        local function refreshStealth() if stealthDisplay then stealthDisplay:UpdateDisplay() end end
        local function refreshStance() if stanceDisplay then stanceDisplay:UpdateDisplay() end end
        local function refreshAll() refreshStealth(); refreshStance() end

        local killArea = CreateFrame("Frame", nil, sc, "BackdropTemplate")
        killArea:SetSize(460, 112)
        killArea:SetPoint("TOPLEFT", 10, -75)
        killArea:SetBackdrop({ bgFile = [[Interface\Buttons\WHITE8x8]] })
        killArea:SetBackdropColor(0.01, 0.56, 0.91, 0.08)

        local masterCB = W:CreateCheckbox(killArea, {
            label = L["STEALTH_ENABLE"],
            db = db, key = "enabled",
            x = 15, y = -8,
            isMaster = true,
        })

        local unlockCB = W:CreateCheckbox(killArea, {
            label = L["COMMON_UNLOCK"],
            db = db, key = "unlock",
            x = 15, y = -38,
            template = "ChatConfigCheckButtonTemplate",
            onChange = refreshStealth
        })
        unlockCB:SetShown(db.enabled)

        local restedCB = W:CreateCheckbox(killArea, {
            label = L["STEALTH_DISABLE_RESTED"],
            db = db, key = "disableWhenRested",
            x = 15, y = -62,
            template = "ChatConfigCheckButtonTemplate",
            onChange = refreshStealth
        })
        restedCB:SetShown(db.enabled)

        local groupOnlyCB = W:CreateCheckbox(killArea, {
            label = L["STEALTH_GROUP_ONLY"],
            db = db, key = "groupOnly",
            x = 15, y = -86,
            template = "ChatConfigCheckButtonTemplate",
            onChange = refreshStealth
        })
        groupOnlyCB:SetShown(db.enabled)

        local stealthSections = CreateFrame("Frame", nil, sc)
        stealthSections:SetPoint("TOPLEFT", killArea, "BOTTOMLEFT", 0, -10)
        stealthSections:SetPoint("RIGHT", sc, "RIGHT", -10, 0)
        stealthSections:SetHeight(300)

        local RelayoutAll

        local colWrap, colContent = W:CreateCollapsibleSection(stealthSections, {
            text = L["STEALTH_SECTION_STEALTH"],
            startOpen = false,
            onCollapse = function() if RelayoutAll then RelayoutAll() end end,
        })

        W:CreateFontPicker(colContent, 10, -5, db.font, function(name)
            db.font = name
            refreshStealth()
        end)

        W:CreateCheckbox(colContent, {
            label = L["STEALTH_SHOW_STEALTHED"],
            db = db, key = "showStealthed",
            x = 10, y = -55,
            template = "ChatConfigCheckButtonTemplate",
            onChange = refreshStealth
        })

        W:CreateCheckbox(colContent, {
            label = L["STEALTH_SHOW_NOT"],
            db = db, key = "showNotStealthed",
            x = 10, y = -79,
            template = "ChatConfigCheckButtonTemplate",
            onChange = refreshStealth
        })

        local G = ns.Layout:New(2)

        W:CreateColorPicker(colContent, {
            label = L["STEALTH_COLOR_STEALTHED"], db = db,
            rKey = "stealthR", gKey = "stealthG", bKey = "stealthB",
            x = G:Col(1), y = -105,
            onChange = refreshStealth
        })

        W:CreateColorPicker(colContent, {
            label = L["STEALTH_COLOR_NOT"], db = db,
            rKey = "warningR", gKey = "warningG", bKey = "warningB",
            x = G:Col(2), y = -105,
            onChange = refreshStealth
        })

        local stealthTextLbl = colContent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
        stealthTextLbl:SetPoint("TOPLEFT", G:Col(1), -145)
        stealthTextLbl:SetText(L["STEALTH_TEXT"])

        local stealthTextBox = CreateFrame("EditBox", nil, colContent, "BackdropTemplate")
        stealthTextBox:SetSize(180, 24)
        stealthTextBox:SetPoint("LEFT", stealthTextLbl, "RIGHT", 8, 0)
        stealthTextBox:SetBackdrop({ bgFile = [[Interface\Buttons\WHITE8x8]],
            edgeFile = [[Interface\Buttons\WHITE8x8]], edgeSize = 1 })
        stealthTextBox:SetBackdropColor(0, 0, 0, 1)
        stealthTextBox:SetBackdropBorderColor(0, 0, 0, 1)
        stealthTextBox:SetFontObject("GameFontHighlightSmall")
        stealthTextBox:SetAutoFocus(false)
        stealthTextBox:SetTextInsets(6, 6, 0, 0)
        stealthTextBox:SetMaxLetters(30)
        stealthTextBox:SetText(db.stealthText or L["STEALTH_DEFAULT"])
        stealthTextBox:SetScript("OnEnterPressed", function(self) self:ClearFocus() end)
        stealthTextBox:SetScript("OnEscapePressed", function(self) self:ClearFocus() end)
        stealthTextBox:SetScript("OnEditFocusLost", function(self)
            local val = strtrim(self:GetText())
            if val == "" then val = L["STEALTH_DEFAULT"]; self:SetText(val) end
            db.stealthText = val
            refreshStealth()
        end)

        local warningTextLbl = colContent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
        warningTextLbl:SetPoint("TOPLEFT", G:Col(1), -175)
        warningTextLbl:SetText(L["STEALTH_WARNING_TEXT"])

        local warningTextBox = CreateFrame("EditBox", nil, colContent, "BackdropTemplate")
        warningTextBox:SetSize(180, 24)
        warningTextBox:SetPoint("LEFT", warningTextLbl, "RIGHT", 8, 0)
        warningTextBox:SetBackdrop({ bgFile = [[Interface\Buttons\WHITE8x8]],
            edgeFile = [[Interface\Buttons\WHITE8x8]], edgeSize = 1 })
        warningTextBox:SetBackdropColor(0, 0, 0, 1)
        warningTextBox:SetBackdropBorderColor(0, 0, 0, 1)
        warningTextBox:SetFontObject("GameFontHighlightSmall")
        warningTextBox:SetAutoFocus(false)
        warningTextBox:SetTextInsets(6, 6, 0, 0)
        warningTextBox:SetMaxLetters(30)
        warningTextBox:SetText(db.warningText or L["STEALTH_WARNING_DEFAULT"])
        warningTextBox:SetScript("OnEnterPressed", function(self) self:ClearFocus() end)
        warningTextBox:SetScript("OnEscapePressed", function(self) self:ClearFocus() end)
        warningTextBox:SetScript("OnEditFocusLost", function(self)
            local val = strtrim(self:GetText())
            if val == "" then val = L["STEALTH_WARNING_DEFAULT"]; self:SetText(val) end
            db.warningText = val
            refreshStealth()
        end)

        local druidNote = colContent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
        druidNote:SetPoint("TOPLEFT", G:Col(1), -205)
        druidNote:SetText(L["STEALTH_DRUID_NOTE"])

        W:CreateCheckbox(colContent, {
            label = L["STEALTH_BALANCE"],
            db = db, key = "enableBalance",
            x = G:Col(1), y = -225,
            template = "ChatConfigCheckButtonTemplate",
            onChange = refreshStealth
        })

        W:CreateCheckbox(colContent, {
            label = L["STEALTH_GUARDIAN"],
            db = db, key = "enableGuardian",
            x = G:Col(1), y = -249,
            template = "ChatConfigCheckButtonTemplate",
            onChange = refreshStealth
        })

        W:CreateCheckbox(colContent, {
            label = L["STEALTH_RESTORATION"],
            db = db, key = "enableResto",
            x = G:Col(1), y = -273,
            template = "ChatConfigCheckButtonTemplate",
            onChange = refreshStealth
        })

        colContent:SetHeight(305)
        colWrap:RecalcHeight()

        local stealthPosWrap = W:CreatePositionSection(stealthSections, {
            db = db,
            moduleName = "stealthReminder",
            title = L["STEALTH_POSITION"],
            pointKey = "point",
            display = ns.StealthReminderDisplay,
            onCollapse = function() if RelayoutAll then RelayoutAll() end end,
            onChange = refreshStealth,
        })

        local stanceKillArea = CreateFrame("Frame", nil, sc, "BackdropTemplate")
        stanceKillArea:SetSize(460, 88)
        stanceKillArea:SetBackdrop({ bgFile = [[Interface\Buttons\WHITE8x8]] })
        stanceKillArea:SetBackdropColor(0.91, 0.56, 0.01, 0.08)

        local stanceMasterCB = W:CreateCheckbox(stanceKillArea, {
            label = L["STEALTH_ENABLE_STANCE"],
            db = db, key = "stanceEnabled",
            x = 15, y = -8,
            isMaster = true,
        })

        local stanceUnlockCB = W:CreateCheckbox(stanceKillArea, {
            label = L["COMMON_UNLOCK"],
            db = db, key = "stanceUnlock",
            x = 15, y = -38,
            template = "ChatConfigCheckButtonTemplate",
            onChange = refreshStance
        })
        stanceUnlockCB:SetShown(db.stanceEnabled)

        local combatOnlyCB = W:CreateCheckbox(stanceKillArea, {
            label = L["GCD_COMBAT_ONLY"],
            db = db, key = "stanceCombatOnly",
            x = 15, y = -62,
            template = "ChatConfigCheckButtonTemplate",
            onChange = refreshStance
        })
        combatOnlyCB:SetShown(db.stanceEnabled)

        local stanceRestedCB = W:CreateCheckbox(stanceKillArea, {
            label = L["STEALTH_DISABLE_RESTED"],
            db = db, key = "stanceDisableWhenRested",
            x = 200, y = -62,
            template = "ChatConfigCheckButtonTemplate",
            onChange = refreshStance
        })
        stanceRestedCB:SetShown(db.stanceEnabled)

        local stanceInstanceCB = W:CreateCheckbox(stanceKillArea, {
            label = L["STEALTH_STANCE_INSTANCE_ONLY"],
            db = db, key = "stanceInstanceOnly",
            x = 200, y = -38,
            template = "ChatConfigCheckButtonTemplate",
            onChange = refreshStance
        })
        stanceInstanceCB:SetShown(db.stanceEnabled)

        local stanceSections = CreateFrame("Frame", nil, sc)
        stanceSections:SetPoint("RIGHT", sc, "RIGHT", -10, 0)
        stanceSections:SetHeight(200)

        local stColWrap, stColContent = W:CreateCollapsibleSection(stanceSections, {
            text = L["STEALTH_SECTION_STANCE"],
            startOpen = false,
            onCollapse = function() if RelayoutAll then RelayoutAll() end end,
        })

        W:CreateFontPicker(stColContent, 10, -5, db.stanceFont or db.font, function(name)
            db.stanceFont = name
            refreshStance()
        end)

        W:CreateColorPicker(stColContent, {
            label = L["COMMON_LABEL_TEXT_COLOR"], db = db,
            rKey = "stanceWarnR", gKey = "stanceWarnG", bKey = "stanceWarnB",
            x = 10, y = -55,
            onChange = refreshStance
        })

        local stanceTextLbl = stColContent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
        stanceTextLbl:SetPoint("TOPLEFT", 10, -95)
        stanceTextLbl:SetText(L["STEALTH_WARNING_TEXT"])

        local stanceTextBox = CreateFrame("EditBox", nil, stColContent, "BackdropTemplate")
        stanceTextBox:SetSize(180, 24)
        stanceTextBox:SetPoint("LEFT", stanceTextLbl, "RIGHT", 8, 0)
        stanceTextBox:SetBackdrop({ bgFile = [[Interface\Buttons\WHITE8x8]],
            edgeFile = [[Interface\Buttons\WHITE8x8]], edgeSize = 1 })
        stanceTextBox:SetBackdropColor(0, 0, 0, 1)
        stanceTextBox:SetBackdropBorderColor(0, 0, 0, 1)
        stanceTextBox:SetFontObject("GameFontHighlightSmall")
        stanceTextBox:SetAutoFocus(false)
        stanceTextBox:SetTextInsets(6, 6, 0, 0)
        stanceTextBox:SetMaxLetters(30)
        stanceTextBox:SetText(db.stanceWarnText or ns.GetDefaultStanceLabel())
        stanceTextBox:SetScript("OnEnterPressed", function(self) self:ClearFocus() end)
        stanceTextBox:SetScript("OnEscapePressed", function(self) self:ClearFocus() end)
        stanceTextBox:SetScript("OnEditFocusLost", function(self)
            local val = strtrim(self:GetText())
            if val == "" then val = ns.GetDefaultStanceLabel(); self:SetText(val) end
            db.stanceWarnText = val
            refreshStance()
        end)

        W:CreateCheckbox(stColContent, {
            label = L["STEALTH_ENABLE_SOUND"],
            db = db, key = "stanceSoundEnabled",
            x = 10, y = -130,
            template = "ChatConfigCheckButtonTemplate",
            onChange = function()
                W:ApplyDisableStates(stColContent)
                refreshStance()
            end
        })

        local function stanceSoundDisabled() return not db.stanceSoundEnabled end

        W:CreateSoundPicker(stColContent, 10, -160, db.stanceSound or ns.Media.DEFAULT_SOUND,
            function(sound) db.stanceSound = sound end,
            { disableif = stanceSoundDisabled })

        local intervalSlider = W:CreateSlider(stColContent, {
            label = L["STEALTH_REPEAT"],
            min = 0, max = 15, step = 1,
            x = 10, y = -205,
            disableif = stanceSoundDisabled,
            db = db, key = "stanceSoundInterval",
            onChange = function(val) db.stanceSoundInterval = val end
        })

        stColContent:SetHeight(250)
        stColWrap:RecalcHeight()

        local stancePosWrap = W:CreatePositionSection(stanceSections, {
            db = db,
            moduleName = "stealthReminder",
            title = L["STANCE_POSITION"],
            pointKey = "stancePoint",
            xKey = "stanceX",
            yKey = "stanceY",
            display = ns.StanceReminderDisplay,
            onCollapse = function() if RelayoutAll then RelayoutAll() end end,
            onChange = refreshStance,
        })

        local stealthSectionList = { colWrap, stealthPosWrap }
        local stanceSectionList = { stColWrap, stancePosWrap }

        RelayoutAll = function()
            for i, section in ipairs(stealthSectionList) do
                section:ClearAllPoints()
                if i == 1 then
                    section:SetPoint("TOPLEFT", stealthSections, "TOPLEFT", 0, 0)
                else
                    section:SetPoint("TOPLEFT", stealthSectionList[i - 1], "BOTTOMLEFT", 0, -12)
                end
                section:SetPoint("RIGHT", stealthSections, "RIGHT", 0, 0)
            end

            local stealthH = 0
            if db.enabled then
                for _, s in ipairs(stealthSectionList) do
                    stealthH = stealthH + s:GetHeight() + 12
                end
            end
            stealthSections:SetHeight(math.max(stealthH, 1))

            stanceKillArea:ClearAllPoints()
            stanceKillArea:SetPoint("TOPLEFT", stealthSections, "BOTTOMLEFT", 0, -20)

            stanceSections:ClearAllPoints()
            stanceSections:SetPoint("TOPLEFT", stanceKillArea, "BOTTOMLEFT", 0, -10)
            stanceSections:SetPoint("RIGHT", sc, "RIGHT", -10, 0)

            for i, section in ipairs(stanceSectionList) do
                section:ClearAllPoints()
                if i == 1 then
                    section:SetPoint("TOPLEFT", stanceSections, "TOPLEFT", 0, 0)
                else
                    section:SetPoint("TOPLEFT", stanceSectionList[i - 1], "BOTTOMLEFT", 0, -12)
                end
                section:SetPoint("RIGHT", stanceSections, "RIGHT", 0, 0)
            end

            local stanceH = 0
            if db.stanceEnabled then
                for _, s in ipairs(stanceSectionList) do
                    stanceH = stanceH + s:GetHeight() + 12
                end
            end
            stanceSections:SetHeight(math.max(stanceH, 1))

            local totalH = 75 + 88 + 10 + stealthH + 20 + 88 + 10 + stanceH + 40
            sc:SetHeight(math.max(totalH, 800))
        end

        masterCB:HookScript("OnClick", function(self)
            db.enabled = self:GetChecked() and true or false
            refreshStealth()
            unlockCB:SetShown(db.enabled)
            restedCB:SetShown(db.enabled)
            groupOnlyCB:SetShown(db.enabled)
            stealthSections:SetShown(db.enabled)
            RelayoutAll()
        end)
        stealthSections:SetShown(db.enabled)

        stanceMasterCB:HookScript("OnClick", function(self)
            db.stanceEnabled = self:GetChecked() and true or false
            refreshStance()
            stanceUnlockCB:SetShown(db.stanceEnabled)
            combatOnlyCB:SetShown(db.stanceEnabled)
            stanceRestedCB:SetShown(db.stanceEnabled)
            stanceInstanceCB:SetShown(db.stanceEnabled)
            stanceSections:SetShown(db.stanceEnabled)
            RelayoutAll()
        end)
        stanceSections:SetShown(db.stanceEnabled)

        local restoreBtn = W:CreateRestoreDefaultsButton({
            moduleName = "stealthReminder",
            parent = sc,
            initFunc = function() ns:InitStealthReminder() end,
            onRestore = function()
                NaowhQOL.stealthReminder.stanceWarnText = nil
                if cache.srFrame then
                    cache.srFrame:Hide()
                    cache.srFrame:SetParent(nil)
                    cache.srFrame = nil
                end
                if stealthDisplay then stealthDisplay:UpdateDisplay() end
                if stanceDisplay then stanceDisplay:UpdateDisplay() end
            end
        })
        restoreBtn:SetPoint("BOTTOMLEFT", sc, "BOTTOMLEFT", 10, 20)

        RelayoutAll()
    end)
end
