-- ini
function TogglePortalHelper()
    if PortalHelper:IsShown() then
        PortalHelper:Hide()
    else
        PortalHelper:Show()
    end
end

-- frame creation and initialization
local PortalHelper = CreateFrame("Frame", "PortalHelper", UIParent)
PortalHelper:SetSize(600, 230)
PortalHelper:SetPoint("CENTER")
PortalHelper:SetMovable(true)
PortalHelper:EnableMouse(true)
PortalHelper:RegisterForDrag("LeftButton")
PortalHelper:SetScript("OnDragStart", PortalHelper.StartMoving)
PortalHelper:SetScript("OnDragStop", PortalHelper.StopMovingOrSizing)
PortalHelper:Hide()
PortalHelper:SetFrameStrata("DIALOG")
PortalHelper:EnableKeyboard(true)
PortalHelper:SetScript(
    "OnKeyDown",
    function(self, key)
        if key == "ESCAPE" then
            self:Hide()
        else
            self:SetPropagateKeyboardInput(true)
        end
    end
)

-- PortalHelperBackground
local PortalHelperBackground = PortalHelper:CreateTexture(nil, "BACKGROUND")
PortalHelperBackground:SetAllPoints(PortalHelper)
PortalHelperBackground:SetTexture(MDungeonTeleports.selectedFramePack)
PortalHelperBackground:SetTexCoord(0, 0.585937, 0.775390, 1)

function UpdatePortalHelperBackground()
    local selectedTexture =
        MDungeonTeleports.selectedFramePack or "Interface/Addons/MDungeonTeleports/media/frames/default_frames.blp"
    PortalHelperBackground:SetTexture(selectedTexture)
end

-- title
local PortalHelperTitle = PortalHelper:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
PortalHelperTitle:SetPoint("TOP", PortalHelper, "TOP", 0, -10)
PortalHelperTitle:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 12, flags)
PortalHelperTitle:SetText("Portal Helper")

-- scroll frame
local scrollFrame = CreateFrame("ScrollFrame", nil, PortalHelper, "UIPanelScrollFrameTemplate")
scrollFrame:SetPoint("TOPLEFT", PortalHelper, "TOPLEFT", 10, -40)
scrollFrame:SetPoint("BOTTOMRIGHT", PortalHelper, "BOTTOMRIGHT", -30, 10)

-- scroll
local scrollChild = CreateFrame("Frame", nil, scrollFrame)
scrollChild:SetSize(600, 1000) -- Make sure this is tall enough to allow scrolling
scrollFrame:SetScrollChild(scrollChild)

-- content
local contentBackground = scrollChild:CreateTexture(nil, "BACKGROUND")
contentBackground:SetAllPoints(scrollChild)
contentBackground:SetColorTexture(0.1, 0.1, 0.1, 0.5)

-- Portals Information
local portalsInfo =
    [[
Want to know where the portals come from? Check here to see when they were available and if they still are!

--Cataclysm--

- [GB] - Grim Batol  
Available During Season 1 of The War Within - |cFFecdc22[Keystone Hero: Grim Batol]|r

- [TOTT] - Throne of the Tides
Available During Season 3 of Dragonflight - |cFFecdc22[Keystone Hero: Throne of the Tides]|r

- [VP] - Vortex Pinnacle
Available During Season 2 of Dragonflight - |cFFecdc22[Keystone Hero: Vortex Pinnacle]|r

--Mists of Pandaria--

- [GOTSS] - Gate of the Setting Sun
Available during Mists of Pandaria Challenge Mode - |cFFecdc22[Gate of the Setting Sun: Gold]|r

- [MSP] - Mogu'shan Palace
Available during Mists of Pandaria Challenge Mode - |cFFecdc22[Mogu'shan Palace: Gold]|r

- [SCHOLO] - Scholomance
Available during Mists of Pandaria Challenge Mode - |cFFecdc22[Scholomance: Gold]|r

- [SH] - Scarlet Halls
Available during Mists of Pandaria Challenge Mode - |cFFecdc22[Scarlet Halls: Gold]|r

- [SM] - Scarlet Monastery
Available during Mists of Pandaria Challenge Mode - |cFFecdc22[Scarlet Monestary: Gold]|r

- [SONT] - Siege of Niuzao Temple
Available during Mists of Pandaria Challenge Mode - |cFFecdc22[Siege of Niuzao Temple: Gold]|r

- [SPM] - Shado-Pan Monastery
Available during Mists of Pandaria Challenge Mode - |cFFecdc22[Shado-Pan Monastery: Gold]|r

- [SSB] - Stormstount Brewery
Available during Mists of Pandaria Challenge Mode - |cFFecdc22[Stormstout Brewery: Gold]|r

- [TJS] - Temple of the Jade Serpent
Available during Mists of Pandaria Challenge Mode and Season 1 of Dragonflight - |cFFecdc22[Temple of the Jade Serpent: Gold]|r + |cFFecdc22[Keystone Hero: Temple of the Jade Serpent]|r


--Warlords of Dreanor--

- [AUCH] - Auchindoun
Available during the Warlords of Draenor Challenge Mode - |cFFecdc22[Auchindoun: Gold]|r

- [BSM] - Bloodmaul Slag Mines
Available during the Warlords of Draenor Challenge Mode - |cFFecdc22[Bloodmaul Slag Mines: Gold]|r

- [EB] - Everbloom
Available during the Warlords of Draenor Challenge Mode and Season 3 of Dragonflight - |cFFecdc22[Everbloom: Gold]|r + |cFFecdc22[Keystone Hero: Everbloom]|r

- [GD] - Grimrail Depot
Available during the Warlords of Draenor Challenge Mode and Season 4 of Shadowlands - |cFFecdc22[Grimrail Depot: Gold]|r + |cFFecdc22[Keystone Hero: Grimrail Depot]|r

- [ID] - Iron Docks
Available during the Warlords of Draenor Challenge Mode and Season 4 of Shadowlands - |cFFecdc22[Iron Docks: Gold]|r + |cFFecdc22[Keystone Hero: Iron Docks]|r

- [SBG] - Shadowmoon Burial Ground
Available during the Warlords of Draenor Challenge Mode and Season 1 of Dragonflight - |cFFecdc22[Shadowmoon Burial Grounds: Gold]|r + |cFFecdc22[Keystone Hero: Shadowmoon Burial Grounds]|r

- [SKY] - Skyreach
Available during the Warlords of Draenor Challenge Mode - |cFFecdc22[Skyreach: Gold]|r

- [UBS] - Upper Blackrock Spire
Available during the Warlords of Draenor Challenge Mode - |cFFecdc22[Upper Blackrock Spire: Gold]|r


--Legion--

- [BRH] - Black Rook Hold
Available during Season 3 of Dragonflight - |cFFecdc22[Keystone Hero: Black Rook Hold]|r

- [COS] - Court of Stars
Available during Season 1 of Dragonflight - |cFFecdc22[Keystone Hero: Court of Stars]|r

- [DHT] - Darkheart Thicket
Available during Season 3 of Dragonflight - |cFFecdc22[Keystone Hero: Darkheart Thicket]|r

- [HOV] - Halls of Valor
Available during Season 1 of Dragonflight - |cFFecdc22[Keystone Hero: Halls of Valor]|r

- [NL] - Neltharion's Lair
Available during Season 2 of Dragonflight - |cFFecdc22[Keystone Hero: Neltharion's Lair]|r

- [KARA] - Return to Karazhan
Available during Season 4 of Shadowlands - |cFFecdc22[Keystone Hero: Return to Karazhan]|r


--Battle for Azeroth--

- [AD] - Atal'Dazar
Available during Season 3 of Dragonflight - |cFFecdc22[Keystone Hero: Atal'Dazar]|r

- [FH] - Freehold
Available during Season 2 of Dragonflight - |cFFecdc22[Keystone Hero: Freehold]|r

- [SIEGE] - Siege of Boralus
Available during Season 1 of The War Within - |cFFecdc22[Keystone Hero: Siege of Boralus]|r

- [UNDR] - The Underrot
Available during Season 2 of Dragonflight - |cFFecdc22[Keystone Hero: The Underrot]|r

- [WM] - Waycrest Manor
Available during Season 3 of Dragonflight - |cFFecdc22[Keystone Hero: Waycrest Manor]|r

- [MECHA] - Operation: Mechagon
Available during Season 4 of Shadowlands - |cFFecdc22[Keystone Hero: Operation: Mechagon]|r


--Shadowlands--

- [DOS] - De Other Side
Available during Seasons 1-3 of Shadowlands - |cFFecdc22[Keystone Hero: De Other Side]|r

- [HOA] - Halls of Attonement
Available during Seasons 1-3 of Shadowlands - |cFFecdc22[Keystone Hero: Halls of Attonement]|r

- [MISTS] - Mists of Tirna Scithe
Available during Seasons 1-3 of Shadowlands and Season 1 of The War Within - |cFFecdc22[Keystone Hero: Mists of Tirne Scithe]|r

- [NW] - Necrotic Wake
Available during Seasons 1-3 of Shadowlands and Season 1 of The War Within - |cFFecdc22[Keystone Hero: Necrotic Wake]|r

- [PF] - Plaguefall
Available during Seasons 1-3 of Shadowlands - |cFFecdc22[Keystone Hero: Plaguefall]|r

- [SD] - Sanguine Depths
Available during Seasons 1-3 of Shadowlands - |cFFecdc22[Keystone Hero: Sanguine Depths]|r

- [SOA] - Spires of Ascension
Available during Seasons 1-3 of Shadowlands - |cFFecdc22[Keystone Hero: Spires of Ascension]|r

- [TOP] - Theatre of Pain
Available during Seasons 1-3 of Shadowlands - |cFFecdc22[Keystone Hero: Theatre of Pain]|r

- [TAZ] - Tazavesh, The Veiled Market
Available during Seasons 4 of Shadowlands - |cFFecdc22[Keystone Hero: Tazavesh, The Veiled Market]|r


--Dragonflight--

- [AA] - Algeth'ar Academy
Available during Season 1 and 4 of Dragonflight - |cFFecdc22[Keystone Hero: Algeth'ar Academy]|r

- [AV] - Azure Vault
Available during Season 1 and 4 of Dragonflight - |cFFecdc22[Keystone Hero: Azure Vault]|r

- [BH] - Brackenhide Hollow
Available during Season 2 and 4 of Dragonflight - |cFFecdc22[Keystone Hero: Brackenhide Hollow]|r

- [HOI] - Halls of Infusion
Available during Season 2 and 4 of Dragonflight - |cFFecdc22[Keystone Hero: Halls of Infusion]|r

- [NELTH] - Neltharus
Available during Season 2 and 4 of Dragonflight - |cFFecdc22[Keystone Hero: Neltharus]|r

- [NO] - Nokhud Offensive
Available during Season 1 and 4 of Dragonflight - |cFFecdc22[Keystone Hero: Nokhud Offensive]|r

- [RLP] - Ruby Life Pools
Available during Season 1 and 4 of Dragonflight - |cFFecdc22[Keystone Hero: Ruby Life Pools]|r

- [ULD] - Uldaman, Legacy of Tyr
Available during Season 2 and 4 of Dragonflight - |cFFecdc22[Keystone Hero: Uldaman, Legacy of Tyr]|r

- [DOTI] - Dawn of the Infinite
Available during Season 2 and 4 of Dragonflight - |cFFecdc22[Keystone Hero: Dawn of the Infinite]|r


--The War Within--

- [ARAK] - Ara'kara, City of Echoes
Available during Season 1 of The War Within - |cFFecdc22[Keystone Hero: Ara'kara, City of Echoes]|r

- [COT] - City of Threads
Available during Season 1 of The War Within - |cFFecdc22[Keystone Hero: City of Threads]|r

- [DAWN] - The Dawnbreaker
Available during Season 1 of The War Within - |cFFecdc22[Keystone Hero: The Dawnbreaker]|r

- [SV] - The Stonevault
Available during Season 1 of The War Within - |cFFecdc22[Keystone Hero: The Stonevault]|r

- [DC] - Darkflame Cleft
Will be available during Season 2 of The War Within

- [PTSF] - Priory of the Sacred Flame
Will be available during Season 2 of The War Within

- [ROOK] - The Rookery
Will be available during Season 2 of The War Within

- [CM] - Cinderbrew Meadery
Will be available during Season 2 of The War Within
]]

-- text
local portalsText = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
portalsText:SetPoint("TOPLEFT", scrollChild, "TOPLEFT", 10, -10)
portalsText:SetWidth(550)
local font, _, flags = portalsText:GetFont()
portalsText:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 10, "OUTLINE")
portalsText:SetJustifyH("LEFT")
portalsText:SetText(portalsInfo)

-- exit button button
local exitButton = CreateFrame("Button", "SettingsOpenButton", PortalHelper)
exitButton:SetSize(14, 14)
exitButton:SetPoint("TOPRIGHT", PortalHelper, "TOPRIGHT", -5, -5)
exitButton:SetFrameStrata("DIALOG")

local exitButtonTexture = exitButton:CreateTexture(nil, "BACKGROUND")
exitButtonTexture:SetAllPoints(exitButton)
exitButtonTexture:SetTexture("Interface\\AddOns\\MDungeonTeleports\\media\\ui\\default_ui\\button_close.blp")

local exitButtonHighlight = exitButton:CreateTexture(nil, "HIGHLIGHT")
exitButtonHighlight:SetAllPoints(exitButton)
exitButtonHighlight:SetTexture(
    "Interface\\AddOns\\MDungeonTeleports\\media\\ui\\default_ui\\button_close_highlight.blp"
)
exitButtonHighlight:Hide()

exitButton.exitButtonHighlight = exitButtonHighlight
exitButton.exitButtonTexture = exitButtonTexture

exitButton:SetScript(
    "OnEnter",
    function(self)
        self.exitButtonHighlight:Show()
        self.exitButtonTexture:Hide()
    end
)

exitButton:SetScript(
    "OnLeave",
    function(self)
        self.exitButtonHighlight:Hide()
        self.exitButtonTexture:Show()
    end
)
exitButton:SetScript(
    "OnClick",
    function()
        PortalHelper:Hide()
    end
)

function UpdatePortalHelperUI()
    local UIPack = MDungeonTeleports.selectedUIPack or "Interface/Addons/MDungeonTeleports/media/ui/default_ui/"
    exitButton.exitButtonTexture:SetTexture(UIPack .. "button_close.blp")
    exitButton.exitButtonHighlight:SetTexture(UIPack .. "button_close_highlight.blp")
end

UpdatePortalHelperUI()

-- Register the ADDON_LOADED event
PortalHelper:RegisterEvent("ADDON_LOADED")
PortalHelper:SetScript("OnEvent", OnEvent)

-- Event handler for ADDON_LOADED
local function OnEvent(self, event, arg1)
    if event == "ADDON_LOADED" and arg1 == "MDungeonTeleports" then
        UpdatePortalHelperUI()
    end
end
