-- Version Check
function VersionCheck()
    local currentVersion = 30000

    if not MDungeonTeleports then
        MDungeonTeleports = {}
    end

    if MDungeonTeleports.Version == nil or MDungeonTeleports.Version > currentVersion then
        WipeSavedVariables()
        MDungeonTeleports.Version = currentVersion
        LaunchFeatureWindow()
        return
    end

    if MDungeonTeleports.Version < currentVersion then
        MDungeonTeleports.Version = currentVersion
        LaunchFeatureWindow()
        return
    end
end

-- MARK: First Launch Frame
local FeatureWindow = CreateFrame("Frame", "FeatureWindow", UIParent)
FeatureWindow:SetSize(480, 216)
FeatureWindow:SetPoint("CENTER")
FeatureWindow:SetMovable(true)
FeatureWindow:EnableMouse(true)
FeatureWindow:RegisterForDrag("LeftButton")
FeatureWindow:SetScript("OnDragStart", FeatureWindow.StartMoving)
FeatureWindow:SetScript("OnDragStop", FeatureWindow.StopMovingOrSizing)
FeatureWindow:SetFrameStrata("DIALOG")
FeatureWindow:Hide()
FeatureWindow:EnableKeyboard(true)
FeatureWindow:SetClampedToScreen(true)
FeatureWindow:SetScale(1.1)
FeatureWindow:SetScript(
   "OnKeyDown",
   function(self, key)
      if key == "ESCAPE" then
         self:Hide()
      else
         self:SetPropagateKeyboardInput(true)
      end
   end
)

-- Background
local FeatureBackground = FeatureWindow:CreateTexture(nil, "BACKGROUND")
FeatureBackground:SetAllPoints(FeatureWindow)
FeatureBackground:SetTexture("Interface/AddOns/MDungeonTeleports/media/ui/ui_frames/ui_frames.blp")
FeatureBackground:SetTexCoord(0, 0.585937, 0, 0.527343)
FeatureBackground:SetVertexColor(1, 1, 1, 1)

local function createText(parent, anchor, x, y, fontSize, text, justify, font)
   local titleText = parent:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
   titleText:SetPoint("TOPLEFT", parent, anchor, x, y)
   titleText:SetFont(font or "Interface/AddOns/MDungeonTeleports/media/fonts/expressway_light.ttf", fontSize, flags)
   titleText:SetText(text)
   titleText:SetJustifyH("LEFT")
   return titleText
end

local function createSubtitle(parent, anchor, x, y, fontSize, text)
   local subText = parent:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
   subText:SetPoint("TOPLEFT", parent, anchor, x, y)
   subText:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", fontSize, flags)
   subText:SetText(text)
   subText:SetJustifyH("LEFT")
   return subText
end

-- first launch window text
local firstPara1 = createSubtitle(FeatureWindow, "TOPLEFT", 10, -10, 14, "Welcome to M+ Dungeon Teleports")
local firstSubt1 = createSubtitle(FeatureWindow, "TOPLEFT", 60, -26, 12, "Season 2 // 3.0.0")
local firstPara1 = createText(FeatureWindow, "TOPLEFT", 36, -38, 10, "So what is new in 3.0.0 this Season?")

local firstSubt2 = createSubtitle(FeatureWindow, "TOPLEFT", 10, -50, 12, "Key Tracker!")
local firstPara2 = createText(FeatureWindow, "TOPLEFT", 14, -62, 10, "Want to be keeping things simple? Just turn on\nthe Key Tracker, see your current key and\neasily teleport there, simple as. The Key\nTracker does include a !keys funtion and the\nability to respond to it.")

local firstSubt3 = createSubtitle(FeatureWindow, "TOPLEFT", 10, -114, 12, "Season 2 Portals!")
local firstPara3 = createText(FeatureWindow, "TOPLEFT", 14, -126, 10, "With a brand new season comes new portals!\nAll of Season 2's portals are now in their own\nwindow you can toggle in the settings!")

local firstSubt4 = createSubtitle(FeatureWindow, "TOPLEFT", 10, -158, 12, "Expanded Portal Library")
local firstPara4 = createText(FeatureWindow, "TOPLEFT", 14, -170, 10, "Now Season 2 is here you can find all The War\nWithin portals in the Portal Library along with the\nrest of the expansion teleports.")

-- addon links
local firstSubt5 = createSubtitle(FeatureWindow, "TOPLEFT", 325, -167, 12, "Add-On Links")

-- info exit button button
local FeatureExit = CreateFrame("Button", "SettingsOpenButton", FeatureWindow)
FeatureExit:SetSize(18, 18)
FeatureExit:SetPoint("TOPRIGHT", FeatureWindow, "TOPRIGHT", -7, -7)
FeatureExit:SetFrameStrata("DIALOG")

local FeatureExitTexure = FeatureExit:CreateTexture(nil, "BACKGROUND")
FeatureExitTexure:SetAllPoints(FeatureExit)
FeatureExitTexure:SetTexture("Interface/Tooltips/UI-Tooltip-Background")
FeatureExitTexure:SetVertexColor(0, 0, 0, 0.9)

local FeatureExitText = FeatureExit:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
FeatureExitText:SetPoint("CENTER", FeatureExit, "CENTER", 0, 0)
FeatureExitText:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 10, flags)
FeatureExitText:SetText("X")

local FeatureExitHighlight = FeatureExit:CreateTexture(nil, "HIGHLIGHT")
FeatureExitHighlight:SetAllPoints(FeatureExit)
FeatureExitHighlight:SetTexture("Interface\\AddOns\\MDungeonTeleports\\media\\ui\\default_ui\\button_highlight.blp")
FeatureExitHighlight:Hide()

FeatureExit.FeatureExitHighlight = FeatureExitHighlight

FeatureExit:SetScript(
   "OnEnter",
   function(self)
      self.FeatureExitHighlight:Show()
   end
)

FeatureExit:SetScript(
   "OnLeave",
   function(self)
      self.FeatureExitHighlight:Hide()
   end
)
FeatureExit:SetScript(
   "OnClick",
   function()
      if FeatureWindow:IsShown() then
         FeatureWindow:Hide()
      else
         FeatureWindow:Show()
      end
   end
)

--- MARK: FirstLaunch Links Buttons
local discordURL = "https://discord.gg/Ht9WYtfZ"
local curseforgeURL = "https://www.curseforge.com/wow/addons/dungeonports"
local wagoURL = "https://addons.wago.io/addons/dungeonports"

local function ShowPopup(linkURL, linkName)
   local CopyFrame = CreateFrame("Frame", "CopyFrame", UIParent, "UIPanelDialogTemplate")
   CopyFrame:SetSize(350, 80)
   CopyFrame:SetPoint("CENTER")
   CopyFrame:SetFrameStrata("TOOLTIP")
   CopyFrame:EnableMouse(true)
   CopyFrame:SetMovable(true)
   CopyFrame:RegisterForDrag("LeftButton")
   CopyFrame:SetScript(
       "OnDragStart",
       function(self)
           self:StartMoving()
       end
   )
   CopyFrame:SetScript(
       "OnDragStop",
       function(self)
           self:StopMovingOrSizing()
       end
   )

   CopyFrame.Title:SetText(linkName)

   local editBox = CreateFrame("EditBox", nil, CopyFrame, "InputBoxTemplate")
   editBox:SetSize(310, 20)
   editBox:SetPoint("TOP", CopyFrame, "TOP", 0, -40)
   editBox:SetAutoFocus(false)
   editBox:SetText(linkURL)
   editBox:HighlightText()

   editBox:SetScript(
       "OnEscapePressed",
       function()
           CopyFrame:Hide()
       end
   )
   editBox:SetScript(
       "OnEnterPressed",
       function()
           editBox:ClearFocus()
       end
   )
   editBox:SetScript(
       "OnEditFocusGained",
       function(self)
           self:HighlightText()
       end
   )

   CopyFrame.CloseButton:SetScript(
       "OnClick",
       function()
           CopyFrame:Hide()
       end
   )
   
   CopyFrame:Show()
end

-- Setup Button
local function CreateButton(name, texturePath, highlightTexturePath, linkURL, x, y)
   local button = CreateFrame("Button", name, FeatureWindow)
   button:SetSize(24, 24)
   button:SetPoint("BOTTOMRIGHT", FeatureWindow, "BOTTOMRIGHT", x, y)
   button:SetFrameStrata("DIALOG")
   
   local buttonIcon = button:CreateTexture(nil, "BACKGROUND")
   buttonIcon:SetAllPoints(button)
   buttonIcon:SetTexture(texturePath)
   
   local buttonIconHighlight = button:CreateTexture(nil, "HIGHLIGHT")
   buttonIconHighlight:SetAllPoints(button)
   buttonIconHighlight:SetTexture(highlightTexturePath)
   buttonIconHighlight:Hide()
   
   button:SetScript(
      "OnEnter",
      function(self)
         buttonIconHighlight:Show()
         buttonIcon:Hide()
         GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
         GameTooltip:SetText(name, 1, 1, 1)
         GameTooltip:Show()
      end
   )
   
   button:SetScript(
      "OnLeave",
      function(self)
         buttonIconHighlight:Hide()
         buttonIcon:Show()
         GameTooltip:Hide()
      end
   )
   
   button:SetScript(
      "OnClick",
      function()
         ShowPopup(linkURL, name)
      end
   )
   
   return button
end

-- Create Buttons
local DiscordButton = CreateButton("Discord", "Interface\\AddOns\\MDungeonTeleports\\media\\ui\\ui_external\\button_discord.blp", "Interface\\AddOns\\MDungeonTeleports\\media\\ui\\ui_external\\button_discord_highlight.blp", discordURL, -81, 10)
local CurseForgeButton = CreateButton("CurseForge", "Interface\\AddOns\\MDungeonTeleports\\media\\ui\\ui_external\\button_curseforge.blp", "Interface\\AddOns\\MDungeonTeleports\\media\\ui\\ui_external\\button_curseforge_highlight.blp", curseforgeURL, -107, 10)
local WagoButton = CreateButton("Wago", "Interface\\AddOns\\MDungeonTeleports\\media\\ui\\ui_external\\button_wago.blp", "Interface\\AddOns\\MDungeonTeleports\\media\\ui\\ui_external\\button_wago_highlight.blp", wagoURL, -133, 10)

function LaunchFeatureWindow()
   FeatureWindow:Show()
end

function WipeSavedVariables()
   MDungeonTeleports.Season1Pos = {x = 615, y = 280}
   MDungeonTeleports.Season2Pos = {x = 615, y = 145}
   MDungeonTeleports.PortalLibraryPos = {x = 970, y = -100}
   MDungeonTeleports.PortalLibraryRaidPos = {x = 970, y = -195}
   MDungeonTeleports.showTooltips = false
   MDungeonTeleports.frameScale = 1
   MDungeonTeleports.isPortalLibraryRaidShown = true
   MDungeonTeleports.isSeason1PortalsShown = true
   MDungeonTeleports.isSeason2PortalsShown = true
   MDungeonTeleports.isPortalLibraryShown = true
   MDungeonTeleports.isKeyFrameShown = true
   MDungeonTeleports.isRespondChecked = false
   MDungeonTeleports.firstLaunch = false
   MDungeonTeleports.selectedFramePack = FramePackCategories[1].items[1].value
   MDungeonTeleports.selectedUIPack = "Interface/AddOns/MDungeonTeleports/media/ui/ui_default/"
end

-- Create a frame to register events
local launchFrame = CreateFrame("Frame")

-- Register both ADDON_LOADED and PLAYER_LOGIN events
launchFrame:RegisterEvent("ADDON_LOADED")
launchFrame:RegisterEvent("PLAYER_LOGIN")

-- Event handler for both events
launchFrame:SetScript(
   "OnEvent",
   function(self, event, arg1)
      if event == "ADDON_LOADED" then
         if arg1 == "MDungeonTeleports" then
            VersionCheck()
         end
      elseif event == "PLAYER_LOGIN" then
         VersionCheck()
      end
   end
)
