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.General = Page
Page.density = Helpers.GetDefaultDensity and Helpers.GetDefaultDensity() or "compact"

local function SetToolkitValue(key, value)
    if SinsToolkit.EnsureDB then SinsToolkit.EnsureDB() end
    SinsToolkitDB[key] = value
    if SinsToolkit.TriggerUpdate then SinsToolkit.TriggerUpdate(key) end
end

local function GetMainMinimapVisible()
    return not (SinsToolkitDB and SinsToolkitDB.minimap and SinsToolkitDB.minimap.hide)
end

local function SetMainMinimapVisible(value)
    if SinsToolkit.EnsureDB then SinsToolkit.EnsureDB() end
    SinsToolkitDB.minimap = SinsToolkitDB.minimap or {}
    SinsToolkitDB.minimap.hide = not value
    local DBIcon = LibStub and LibStub("LibDBIcon-1.0", true)
    if DBIcon then
        if value then
            DBIcon:Show(addonName)
        else
            DBIcon:Hide(addonName)
        end
        DBIcon:Refresh(addonName, SinsToolkitDB.minimap)
    end
end

local function GetMainWindowOpacity()
    return SinsToolkit.GetWindowOpacity and SinsToolkit.GetWindowOpacity() or 0.98
end

local function SetMainWindowOpacity(value)
    if SinsToolkit.SetWindowOpacity then
        SinsToolkit.SetWindowOpacity(value)
    else
        SetToolkitValue("mainWindowOpacity", math.min(1, math.max(0.45, value or 0.98)))
    end
end

local function FormatOpacity(value)
    return ("%d%%"):format(math.floor(((value or 0) * 100) + 0.5))
end

function Page:Create(CreateStaticPage, Window)
    local Theme = Helpers.GetTheme and Helpers.GetTheme()
    local UI = SinsToolkit.UIComponents
    local generalPage = CreateStaticPage("General")
    local header = Window:CreateSectionHeader(generalPage, "General Settings")

    local subHeader = generalPage:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
    subHeader:SetPoint("TOPLEFT", header, "BOTTOMLEFT", 0, -8)
    subHeader:SetText("Basic Toolkit preferences.")
    if Theme and Theme.ApplyBodyFont then
        Theme.ApplyBodyFont(subHeader)
    end

    local launcherCard = UI and UI:CreateCard(generalPage, "Launcher", nil, 330, 104) or CreateFrame("Frame", nil, generalPage, "BackdropTemplate")
    launcherCard:SetPoint("TOPLEFT", subHeader, "BOTTOMLEFT", 0, -22)
    if launcherCard.body then
        launcherCard.body:SetText("Main Toolkit minimap entry point.")
    end

    local mainMinimapToggle = Window:CreateCheckbox(launcherCard, "Show Main Minimap Icon", GetMainMinimapVisible, SetMainMinimapVisible, "Shows or hides the main Sin's Toolkit minimap launcher. The Teleport Hub and Bug Catcher have their own minimap controls on their own tabs.")
    mainMinimapToggle:SetPoint("TOPLEFT", launcherCard, "TOPLEFT", 14, -62)

    local windowCard = UI and UI:CreateCard(generalPage, "Window", nil, 420, 132) or CreateFrame("Frame", nil, generalPage, "BackdropTemplate")
    windowCard:SetPoint("TOPLEFT", launcherCard, "BOTTOMLEFT", 0, -14)
    if windowCard.body then
        windowCard.body:SetText("Adjust Toolkit-owned dark window backgrounds. Text and controls remain fully readable.")
    end

    local opacitySlider = Window:CreateSlider(windowCard, "Window Opacity", GetMainWindowOpacity, SetMainWindowOpacity, 0.45, 1, 0.01, "Adjusts Toolkit-owned dark window backgrounds. Text and controls remain fully readable.", FormatOpacity)
    opacitySlider:SetPoint("TOPLEFT", windowCard, "TOPLEFT", 18, -78)

    self.frame = generalPage
    return generalPage
end

Page:Register("General")

return Page
