local addonName = "MDungeonTeleports"
local version = C_AddOns.GetAddOnMetadata(addonName, "Version") or "Unknown"

-- Version Check
function VersionCheck()
   local currentVersion = 40000
    if not MDungeonTeleports then
        MDungeonTeleports = {}
    end

    if MDungeonTeleports.Version == nil or MDungeonTeleports.Version > currentVersion then
        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)

-- 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 3 // ".. version)
local firstPara1 = createText(FeatureWindow, "TOPLEFT", 56, -38, 10, "So what is new in ".. version .. "?")

local firstSubt2 = createSubtitle(FeatureWindow, "TOPLEFT", 10, -50, 12, "New Season and New QoL Features")
local firstPara2 = createText(FeatureWindow, "TOPLEFT", 14, -62, 10, "The new Dungeon and Raid have been added to\nthe new seasonal window as well as their respective\nlibraries and the Key Tracker is also ready to go\nfor Season 3! With the new update comes the\nability to lock your frames and move them\nwith the Shift Drag feature, and you can now\nalso remove that annoying login message!")

local firstSubt3 = createSubtitle(FeatureWindow, "TOPLEFT", 10, -134, 12, "Mists of Pandaria Support")
local firstPara3 = createText(FeatureWindow, "TOPLEFT", 14, -146, 10, "With the introduction of Mists of Pandaria Classic,\nthe addon now supports the teleports\nyou can obtain from the Challenge Mode dungeons!")

local firstSubt4 = createSubtitle(FeatureWindow, "TOPLEFT", 10, -178, 12, "More on the way!")
local firstPara4 = createText(FeatureWindow, "TOPLEFT", 14, -190, 10, "We're working on adding more features and support for\nother content, so stay tuned!")

-- 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
--- MARK: Settings Links Buttons
local discordURL = "https://discord.gg/5AtcdZ9t7Q"
local curseforgeURL = "https://www.curseforge.com/wow/addons/dungeonports"
local wagoURL = "https://addons.wago.io/addons/dungeonports"

local CopyFrame

local function ShowPopup(linkURL, linkName)
    if CopyFrame then
        CopyFrame:Hide()
    end

    if FeatureWindow and FeatureWindow:IsShown() then
      FeatureWindow:Hide()
    end

    -- Create new frame
    CopyFrame = CreateFrame("Frame", "CopyFrame", UIParent, "BackdropTemplate")
    CopyFrame:SetSize(350, 80)
    CopyFrame:SetBackdrop({bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background"})
    CopyFrame:SetPoint("CENTER")
    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:EnableKeyboard(true)
    CopyFrame:SetClampedToScreen(true)
    CopyFrame:SetScript(
        "OnKeyDown",
        function(self, key)
            if key == "ESCAPE" then
                self:Hide()
            else
                self:SetPropagateKeyboardInput(true)
            end
        end
    )

    local poptitle = CopyFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
    poptitle:SetPoint("TOP", CopyFrame, "TOP", 0, -10)
    poptitle:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 12, flags)
    poptitle:SetTextColor(0.2, 0.8, 0.9)
    poptitle: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
    )

    -- MARK: CopyFrame Exit Button
    local copyframeexitbutton = CreateFrame("Button", "SettingsOpenButton", CopyFrame)
    copyframeexitbutton:SetSize(14, 14)
    copyframeexitbutton:SetPoint("TOPRIGHT", CopyFrame, "TOPRIGHT", -5, -5)
    copyframeexitbutton:SetFrameStrata("DIALOG")

    local copyframeexit = copyframeexitbutton:CreateTexture(nil, "BACKGROUND")
    copyframeexit:SetAllPoints(copyframeexitbutton)
    copyframeexit:SetTexture("Interface\\AddOns\\MDungeonTeleports\\media\\ui\\ui_default\\button_close.blp")

    local copyframeexithighlight = copyframeexitbutton:CreateTexture(nil, "HIGHLIGHT")
    copyframeexithighlight:SetAllPoints(copyframeexitbutton)
    copyframeexithighlight:SetTexture("Interface\\AddOns\\MDungeonTeleports\\media\\ui\\ui_default\\button_close_highlight.blp")
    copyframeexithighlight:Hide()

    copyframeexitbutton.copyframeexithighlight = copyframeexithighlight
    copyframeexitbutton.copyframeexit = copyframeexit

    copyframeexitbutton:SetScript(
        "OnEnter",
        function(self)
            self.copyframeexithighlight:Show()
            self.copyframeexit:Hide()
        end
    )

    copyframeexitbutton:SetScript(
        "OnLeave",
        function(self)
            self.copyframeexithighlight:Hide()
            self.copyframeexit:Show()
        end
    )

    copyframeexitbutton:SetScript(
        "OnClick",
        function()
            PlaySound(SOUNDKIT.IG_CHARACTER_INFO_CLOSE)
            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

-- 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
            C_Timer.After(2, function() 
               VersionCheck() 
            end)
         end
      elseif event == "PLAYER_LOGIN" then
         C_Timer.After(2, function() 
            VersionCheck() 
         end)
      end
   end
)

