local _, addonTable = ...
local WipedQoL = addonTable.WipedQoL
local UI = addonTable.UI
local TRANSLATE = addonTable.TRANSLATE or function(msg) return msg end

WipedQoL:RegisterTabBuilder("general", function(addon, parent)
    local frame = CreateFrame("Frame", nil, parent)
    local raidToolsSection = UI.CreateSection(
        frame,
        TRANSLATE("Hide raid tools in group"),
        TRANSLATE("Hides Blizzard's raid tools while you are in a party.\nEnabling or disabling takes effect after reloading the UI.")
    )
    raidToolsSection:SetPoint("TOPLEFT", frame, "TOPLEFT", 14, -14)
    raidToolsSection:SetPoint("TOPRIGHT", frame, "TOPRIGHT", -14, -14)
    raidToolsSection:SetHeight(100)

    local raidToolsCheckbox = UI.CreateSectionToggle(raidToolsSection, function()
        return addon:IsRaidFrameManagerHidden()
    end, function(value)
        addon:SetRaidFrameManagerHidden(value)
    end)

    local reloadButton = CreateFrame("Button", nil, raidToolsSection, "UIPanelButtonTemplate")
    reloadButton:SetSize(72, 24)
    reloadButton:SetPoint("TOPLEFT", raidToolsSection.description, "BOTTOMLEFT", 0, -8)
    reloadButton:SetText("/reload")
    reloadButton:SetScript("OnClick", ReloadUI)
    UI.SkinButton(reloadButton)

    local attention = reloadButton:CreateTexture(nil, "OVERLAY")
    attention:SetPoint("TOPLEFT", reloadButton, "TOPLEFT", -2, 2)
    attention:SetPoint("BOTTOMRIGHT", reloadButton, "BOTTOMRIGHT", 2, -2)
    UI.SetAddonColor(attention, "SetColorTexture")
    attention:SetAlpha(0)
    local attentionAnimation = attention:CreateAnimationGroup()
    local attentionIn = attentionAnimation:CreateAnimation("Alpha")
    attentionIn:SetFromAlpha(0)
    attentionIn:SetToAlpha(0.35)
    attentionIn:SetDuration(0.08)
    attentionIn:SetOrder(1)
    local attentionOut = attentionAnimation:CreateAnimation("Alpha")
    attentionOut:SetFromAlpha(0.35)
    attentionOut:SetToAlpha(0)
    attentionOut:SetDuration(0.42)
    attentionOut:SetOrder(2)

    local reloadWasShown = false
    local function RefreshRaidFrameManagerState()
        if raidToolsCheckbox and raidToolsCheckbox.Refresh then
            raidToolsCheckbox.Refresh(true)
        end
        local shouldShow = addon.raidFrameManagerReloadSuggested == true
        reloadButton:SetShown(shouldShow)
        if shouldShow and not reloadWasShown then
            attentionAnimation:Stop()
            attentionAnimation:Play()
        end
        reloadWasShown = shouldShow
    end

    function addon:RefreshRaidFrameManagerState()
        RefreshRaidFrameManagerState()
    end

    local achievementNavigationSection = UI.CreateSection(
        frame,
        TRANSLATE("Achievement navigation"),
        TRANSLATE("Adds back, forward and history buttons to Blizzard's achievement window and remembers selected achievements for the current game session.")
    )
    achievementNavigationSection:SetPoint("TOPLEFT", raidToolsSection, "BOTTOMLEFT", 0, -12)
    achievementNavigationSection:SetPoint("TOPRIGHT", raidToolsSection, "BOTTOMRIGHT", 0, -12)
    achievementNavigationSection:SetHeight(78)

    UI.CreateSectionToggle(achievementNavigationSection, function()
        return addon:IsAchievementNavigationEnabled()
    end, function(value)
        addon:SetAchievementNavigationEnabled(value)
    end)

    RefreshRaidFrameManagerState()
    return frame
end)
