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

local function GetToolkitValue(key)
    if SinsToolkit.EnsureDB then SinsToolkit.EnsureDB() end
    return SinsToolkitDB and SinsToolkitDB[key]
end

local function BuildFeatureStatusRows()
    local enabledRows = {}
    local disabledRows = {}

    local function AddStatus(label, enabled, detail, disabledDetail)
        local row = {
            label = label,
            detail = enabled and detail or disabledDetail,
        }
        if enabled then
            enabledRows[#enabledRows + 1] = row
        else
            disabledRows[#disabledRows + 1] = row
        end
    end

    local bugBlocked = SinsToolkit.BugCatcher and SinsToolkit.BugCatcher.IsBlockedByExternalAddon and SinsToolkit.BugCatcher:IsBlockedByExternalAddon()
    AddStatus("Gear Scanner", GetToolkitValue("gearScannerEnabled"))
    AddStatus("Bug Catcher", GetToolkitValue("bugCatcherEnabled") and not bugBlocked, nil, bugBlocked and "External BugSack active" or nil)
    AddStatus("Teleport Hub", GetToolkitValue("teleportHubEnabled"))
    AddStatus("Auto Gossip", GetToolkitValue("automateGossip"), GetToolkitValue("automateGossipModifier") or "SHIFT")
    AddStatus("UI Error Filtering", GetToolkitValue("hideOutOfRangeErrors"))
    AddStatus("Quick Links", GetToolkitValue("wowheadSearchEnabled"))
    AddStatus("No Auto Close Panels", GetToolkitValue("keepPanelsOpen"))
    AddStatus("Auto Unshift for Druids", GetToolkitValue("gseAutoUnshift"))
    AddStatus("Profession Hotkey", GetToolkitValue("professionItemHotkeyEnabled"), GetToolkitValue("professionItemHotkeyModifier") or "ALT")
    AddStatus("Frame Tools", GetToolkitValue("frameMoverEnabled"))
    AddStatus("Auto Flight Form", GetToolkitValue("druidAutoFlightForm"))
    AddStatus("Hover Identifier", GetToolkitValue("identifierEnabled"))

    return enabledRows, disabledRows
end

function Page:Create(CreateScrollablePage, Window)
    local Theme = Helpers.GetTheme and Helpers.GetTheme()
    local infoPage, infoChild = CreateScrollablePage("Information")
    local infoHeader = Window:CreateSectionHeader(infoChild, "Home")
    local UI = SinsToolkit.UIComponents

    local banner = infoChild:CreateTexture(nil, "ARTWORK")
    banner:SetTexture("Interface\\AddOns\\SinsToolkit\\Media\\Banner.tga")
    banner:SetTexCoord(0, 1, 0, 1)
    banner:SetSize(560, 186)

    local statusCard = UI and UI:CreateCard(infoChild, "Toolkit Status", "Interface\\AddOns\\SinsToolkit\\Media\\Logo.tga", 360, 132) or CreateFrame("Frame", nil, infoChild, "BackdropTemplate")
    local providerLine = statusCard:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
    providerLine:SetPoint("TOPLEFT", 18, -48)
    providerLine:SetJustifyH("LEFT")
    if Theme and Theme.ApplyBodyFont then Theme.ApplyBodyFont(providerLine) end
    local statusBadge = UI and UI:CreateStatusBadge(statusCard, "Ready", "enabled")

    local featureCard = UI and UI:CreateCard(infoChild, "Feature Status", nil, 360, 260) or CreateFrame("Frame", nil, infoChild, "BackdropTemplate")
    local enabledTitle = featureCard:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    enabledTitle:SetText("|cff00ff00Enabled|r")
    local disabledTitle = featureCard:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    disabledTitle:SetText("|cffff4040Disabled|r")
    if Theme and Theme.ApplySectionHeader then
        Theme.ApplySectionHeader(enabledTitle)
        Theme.ApplySectionHeader(disabledTitle)
    end
    local featureLabels = {}
    for i = 1, 24 do
        local fs = featureCard:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
        fs:SetJustifyH("LEFT")
        if Theme and Theme.ApplyStatusText then Theme.ApplyStatusText(fs) end
        featureLabels[i] = fs
    end

    local environmentCard = UI and UI:CreateCard(infoChild, "Environment", nil, 260, 120) or CreateFrame("Frame", nil, infoChild, "BackdropTemplate")
    local environmentText = environmentCard:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
    environmentText:SetPoint("TOPLEFT", 18, -40)
    environmentText:SetPoint("RIGHT", -18, 0)
    environmentText:SetJustifyH("LEFT")
    environmentText:SetText("Sin's Toolkit adapts to the active UI framework.\n\nCurrent Mode: |cffb868dc" .. ((SinsToolkit.GetProviderDisplayName and SinsToolkit.GetProviderDisplayName()) or "Standalone") .. "|r")
    if Theme and Theme.ApplySmallBodyFont then Theme.ApplySmallBodyFont(environmentText) end

    local actionsCard = UI and UI:CreateCard(infoChild, "Quick Actions", nil, 260, 150) or CreateFrame("Frame", nil, infoChild, "BackdropTemplate")
    local btnRunScan = UI and UI:CreateActionButton(actionsCard, "Run Gear Scan", function()
        if SinsToolkit.ForceGearScan then SinsToolkit.ForceGearScan() end
    end, "Runs /stk scan with your current Gear Scanner settings.", 150) or CreateFrame("Button", nil, actionsCard, "UIPanelButtonTemplate")
    local btnOpenTeleports = UI and UI:CreateActionButton(actionsCard, "Open Teleport Hub", function()
        if SinsToolkit.TeleportHub and SinsToolkit.TeleportHub.Open then SinsToolkit.TeleportHub:Open() end
    end, "Opens the Teleport Hub. Slash command: /stktp.", 150) or CreateFrame("Button", nil, actionsCard, "UIPanelButtonTemplate")
    local btnOpenBug = UI and UI:CreateActionButton(actionsCard, "Open Bug Catcher", function()
        if SinsToolkit.BugCatcher and SinsToolkit.BugCatcher.OpenSack then SinsToolkit.BugCatcher:OpenSack() end
    end, "Opens the Bug Catcher. Slash command: /stkbug.", 150) or CreateFrame("Button", nil, actionsCard, "UIPanelButtonTemplate")
    local btnOpenProfiles = UI and UI:CreateActionButton(actionsCard, "Open Profile Vault", function()
        Window:SelectPage("Profiles")
    end, "Opens Profile Vault for backups and restores.", 150) or CreateFrame("Button", nil, actionsCard, "UIPanelButtonTemplate")

    infoChild:SetHeight(640)
    local function LayoutInformationPage()
        local availableWidth = math.max(420, (infoPage:GetWidth() or 700) - 44)
        local bannerAspect = 2
        local bannerWidth = math.min(availableWidth - 16, 900)
        local bannerHeight = math.floor(bannerWidth / bannerAspect)
        bannerHeight = math.max(120, math.min(180, bannerHeight))
        bannerWidth = math.floor(bannerHeight * bannerAspect)

        infoChild:SetWidth(math.max(availableWidth, bannerWidth + 40))
        banner:ClearAllPoints()
        banner:SetSize(bannerWidth, bannerHeight)
        banner:SetPoint("TOP", infoChild, "TOP", 0, -46)

        local gap = 12
        local leftWidth = math.max(300, math.floor((availableWidth - gap - 40) * 0.42))
        local rightWidth = math.max(300, availableWidth - leftWidth - gap - 40)
        local topY = -(bannerHeight + 64)

        statusCard:ClearAllPoints()
        statusCard:SetPoint("TOPLEFT", infoChild, "TOPLEFT", 20, topY)
        statusCard:SetSize(leftWidth, 96)
        providerLine:SetText("|cffbca7d6Provider:|r " .. ((SinsToolkit.GetProviderDisplayName and SinsToolkit.GetProviderDisplayName()) or "Standalone"))
        if statusBadge then
            statusBadge:ClearAllPoints()
            statusBadge:SetPoint("TOPRIGHT", statusCard, "TOPRIGHT", -18, -44)
        end

        environmentCard:ClearAllPoints()
        environmentCard:SetPoint("TOPLEFT", statusCard, "BOTTOMLEFT", 0, -gap)
        environmentCard:SetSize(leftWidth, 96)
        environmentText:SetText("Sin's Toolkit is running as a standalone addon.\nCurrent Mode: |cffb868dc" .. ((SinsToolkit.GetProviderDisplayName and SinsToolkit.GetProviderDisplayName()) or "Standalone") .. "|r")

        actionsCard:ClearAllPoints()
        actionsCard:SetPoint("TOPLEFT", environmentCard, "BOTTOMLEFT", 0, -gap)
        actionsCard:SetSize(leftWidth, leftWidth >= 340 and 116 or 184)
        btnRunScan:ClearAllPoints()
        btnRunScan:SetPoint("TOPLEFT", actionsCard, "TOPLEFT", 18, -44)
        btnOpenTeleports:ClearAllPoints()
        if leftWidth >= 340 then
            btnOpenTeleports:SetPoint("LEFT", btnRunScan, "RIGHT", 12, 0)
        else
            btnOpenTeleports:SetPoint("TOPLEFT", btnRunScan, "BOTTOMLEFT", 0, -10)
        end
        btnOpenBug:ClearAllPoints()
        btnOpenBug:SetPoint("TOPLEFT", btnOpenTeleports, "BOTTOMLEFT", 0, -10)
        btnOpenProfiles:ClearAllPoints()
        if leftWidth >= 340 then
            btnOpenProfiles:SetPoint("LEFT", btnOpenBug, "RIGHT", 12, 0)
        else
            btnOpenProfiles:SetPoint("TOPLEFT", btnOpenBug, "BOTTOMLEFT", 0, -10)
        end

        featureCard:ClearAllPoints()
        featureCard:SetPoint("TOPLEFT", statusCard, "TOPRIGHT", gap, 0)
        featureCard:SetSize(rightWidth, 390)
        enabledTitle:ClearAllPoints()
        enabledTitle:SetPoint("TOPLEFT", featureCard, "TOPLEFT", 18, -42)
        disabledTitle:ClearAllPoints()
        disabledTitle:SetPoint("TOPLEFT", featureCard, "TOPLEFT", math.max(180, math.floor(rightWidth * 0.52)), -42)

        for _, fs in ipairs(featureLabels) do
            fs:Hide()
        end

        local enabledRows, disabledRows = BuildFeatureStatusRows()
        local function DrawRows(rows, startIndex, anchor, enabled)
            for i, row in ipairs(rows) do
                local fs = featureLabels[startIndex + i - 1]
                if fs then
                    fs:ClearAllPoints()
                    fs:SetPoint("TOPLEFT", anchor, "BOTTOMLEFT", 0, -7 - ((i - 1) * 18))
                    fs:SetWidth(math.max(130, math.floor(rightWidth * 0.42)))
                    fs:SetText((enabled and "|cff00ff00+ |r" or "|cffff4040- |r") .. row.label .. (row.detail and (" |cffbca7d6(" .. row.detail .. ")|r") or ""))
                    fs:Show()
                end
            end
        end
        DrawRows(enabledRows, 1, enabledTitle, true)
        DrawRows(disabledRows, 13, disabledTitle, false)

        infoChild:SetHeight(math.max(610, bannerHeight + 500))
    end
    infoPage.OnLayoutChanged = LayoutInformationPage
    infoPage.OnShowCallback = LayoutInformationPage

    self.frame = infoPage
    return infoPage
end

Page:Register("Information")

return Page
