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

function Page:Create(CreateScrollablePage, Window, CreateWrappedText)
    local getGV = Helpers.GetGV
    local setGV = Helpers.SetGV
    local UI = SinsToolkit.UIComponents

    local teleportsPage = Helpers.CreateContentPage(Window, "Teleports")

    local teleportHeader = Helpers.CreatePageHeader(teleportsPage, "Teleport Hub")

    CreateWrappedText(teleportsPage, {"TOPLEFT", teleportHeader, "BOTTOMLEFT", 0, -6}, 640, "Open a compact launcher for known teleports, hearthstones, portals, and teleport toys. The floating hub keeps search, optional Favorites and Recently Used sections, secure Use buttons, and the optional Teleport minimap icon.", "GameFontHighlightSmall")

    local function setTeleportOption(key)
        return function(value)
            if SinsToolkitDB then
                SinsToolkitDB[key] = value and true or false
            end
            if SinsToolkit.TeleportHub and SinsToolkit.TeleportHub.Render then
                SinsToolkit.TeleportHub:Render()
            end
        end
    end

    local settingsCard = UI and UI:CreateCard(teleportsPage, "Settings", nil, 704, 190) or CreateFrame("Frame", nil, teleportsPage, "BackdropTemplate")
    settingsCard:SetPoint("TOPLEFT", teleportHeader, "BOTTOMLEFT", 0, -48)
    if settingsCard.body then
        settingsCard.body:SetText("Launcher availability and minimap access.")
    end

    local teleportEnabled = Window:CreateCheckbox(settingsCard, "Enable Teleport Hub", getGV("teleportHubEnabled"), setGV("teleportHubEnabled"), "Enables Sin's Toolkit QOL Teleport Hub and the /stktp commands.")
    teleportEnabled:SetPoint("TOPLEFT", settingsCard, "TOPLEFT", 14, -58)

    local teleportMinimap = Window:CreateCheckbox(settingsCard, "Show Teleport Minimap Icon", getGV("teleportHubMinimap"), setGV("teleportHubMinimap"), "Shows a small minimap button for opening Teleport Hub. You can also use /stktp if you prefer no minimap button.")
    teleportMinimap:SetPoint("TOPLEFT", teleportEnabled, "BOTTOMLEFT", 0, -8)

    local teleportFavorites = Window:CreateCheckbox(settingsCard, "Show Favorites Section", getGV("teleportHubShowFavorites"), setTeleportOption("teleportHubShowFavorites"), "Shows saved favorite teleports in the floating hub.")
    teleportFavorites:SetPoint("TOPLEFT", teleportMinimap, "BOTTOMLEFT", 0, -8)

    local teleportRecent = Window:CreateCheckbox(settingsCard, "Show Recently Used Section", getGV("teleportHubShowRecent"), setTeleportOption("teleportHubShowRecent"), "Shows recently used teleports in the floating hub and records new recent teleports while enabled.")
    teleportRecent:SetPoint("TOPLEFT", teleportFavorites, "BOTTOMLEFT", 0, -8)

    local actionsCard = UI and UI:CreateCard(teleportsPage, "Actions", nil, 704, 116) or CreateFrame("Frame", nil, teleportsPage, "BackdropTemplate")
    actionsCard:SetPoint("TOPLEFT", settingsCard, "BOTTOMLEFT", 0, -14)
    if actionsCard.body then
        actionsCard.body:SetText("Open, rescan, or reset the floating launcher.")
    end

    local teleportOpen = Window:CreateButton(actionsCard, "Open Floating Hub", 150, 24, function()
        if SinsToolkit.TeleportHub and SinsToolkit.TeleportHub.Open then
            SinsToolkit.TeleportHub:Open()
        end
    end, "Opens the floating Teleport Hub panel. Same as /stktp.")
    teleportOpen:SetPoint("TOPLEFT", actionsCard, "TOPLEFT", 18, -58)

    local teleportScan = Window:CreateButton(actionsCard, "Rescan Teleports", 150, 24, function()
        if SinsToolkit.TeleportHub and SinsToolkit.TeleportHub.Scan then
            SinsToolkit.TeleportHub:Scan(true)
        end
    end, "Refreshes known teleport, hearthstone, toy, and class teleport availability for the current character.")
    teleportScan:SetPoint("LEFT", teleportOpen, "RIGHT", 12, 0)

    local teleportReset = Window:CreateButton(actionsCard, "Reset Hub Window", 150, 24, function()
        if SinsToolkit.TeleportHub and SinsToolkit.TeleportHub.ResetWindow then
            SinsToolkit.TeleportHub:ResetWindow()
        end
    end, "Resets the floating hub to the center of the screen.")
    teleportReset:SetPoint("TOPLEFT", teleportOpen, "BOTTOMLEFT", 0, -10)

    local hearthCard = UI and UI:CreateCard(teleportsPage, "Preferred Hearthstone", nil, 704, 116) or CreateFrame("Frame", nil, teleportsPage, "BackdropTemplate")
    hearthCard:SetPoint("TOPLEFT", actionsCard, "BOTTOMLEFT", 0, -14)
    if hearthCard.body then
        hearthCard.body:SetText("Choose which owned Hearthstone the launcher should use, or let it pick a random available one.")
    end

    local hearthControl
    local function RefreshPreferredHearthstoneSelector()
        local hub = SinsToolkit.TeleportHub
        local current = hub and hub.GetPreferredHearthstone and hub:GetPreferredHearthstone() or "random"
        local options = hub and hub.GetPreferredHearthstoneOptions and hub:GetPreferredHearthstoneOptions() or {}
        local label = "Random Available Hearthstone"
        for _, option in ipairs(options) do
            if option.value == current then
                label = option.text
                break
            end
        end
        if hearthControl and UIDropDownMenu_SetText then
            UIDropDownMenu_SetText(hearthControl, label)
        elseif hearthControl and hearthControl.SetText then
            hearthControl:SetText(label)
        end
    end

    if UIDropDownMenu_CreateInfo then
        hearthControl = CreateFrame("Frame", addonName .. "PreferredHearthstoneDropdown", hearthCard, "UIDropDownMenuTemplate")
        hearthControl:SetPoint("TOPLEFT", hearthCard, "TOPLEFT", 0, -58)
        UIDropDownMenu_SetWidth(hearthControl, 250)
        UIDropDownMenu_Initialize(hearthControl, function(_, level)
            local hub = SinsToolkit.TeleportHub
            local options = hub and hub.GetPreferredHearthstoneOptions and hub:GetPreferredHearthstoneOptions() or {
                { value = "random", text = "Random Available Hearthstone" },
            }
            local current = hub and hub.GetPreferredHearthstone and hub:GetPreferredHearthstone() or "random"
            for _, option in ipairs(options) do
                local info = UIDropDownMenu_CreateInfo()
                info.text = option.text
                info.checked = option.value == current
                info.func = function()
                    if hub and hub.SetPreferredHearthstone then
                        hub:SetPreferredHearthstone(option.value)
                    end
                    RefreshPreferredHearthstoneSelector()
                end
                UIDropDownMenu_AddButton(info, level)
            end
        end)
        Window:SetTooltip(hearthControl, "Preferred Hearthstone", "Choose Random Available Hearthstone or one of your owned Hearthstones for the launcher Hearthstone card.")
    else
        hearthControl = Window:CreateButton(hearthCard, "Random Available Hearthstone", 240, 24, nil, "Cycles through Random Available Hearthstone and your owned Hearthstones.")
        hearthControl:SetPoint("TOPLEFT", hearthCard, "TOPLEFT", 18, -64)
        hearthControl:SetScript("OnClick", function()
            local hub = SinsToolkit.TeleportHub
            if not hub or not hub.GetPreferredHearthstoneOptions or not hub.SetPreferredHearthstone then return end
            local options = hub:GetPreferredHearthstoneOptions()
            if #options == 0 then return end
            local current = hub:GetPreferredHearthstone()
            local nextIndex = 1
            for index, option in ipairs(options) do
                if option.value == current then
                    nextIndex = index + 1
                    break
                end
            end
            if nextIndex > #options then nextIndex = 1 end
            hub:SetPreferredHearthstone(options[nextIndex].value)
            RefreshPreferredHearthstoneSelector()
        end)
    end
    hearthControl:SetScript("OnShow", RefreshPreferredHearthstoneSelector)

    local teleportLocked = Window:CreateCheckbox(teleportsPage, "Show Unavailable Teleports", getGV("teleportHubShowLocked"), setGV("teleportHubShowLocked"), "Shows teleports, toys, or class options that this character has not learned or does not own yet.")
    teleportLocked:SetPoint("TOPLEFT", hearthCard, "BOTTOMLEFT", 0, -18)

    CreateWrappedText(teleportsPage, {"TOPLEFT", teleportLocked, "BOTTOMLEFT", 4, -18}, 720, "Use /stktp, /stkteleport, the Teleport Hub minimap button, or the main Toolkit minimap right-click shortcut to open the launcher. Old grid/list/menu commands are kept as harmless compatibility aliases.", "GameFontHighlightSmall")

    teleportsPage.OnShowCallback = RefreshPreferredHearthstoneSelector

    self.frame = teleportsPage
    return teleportsPage
end

Page:Register("Teleports")

return Page
