local ADDON_NAME, NS = ...
local L = LibStub("AceLocale-3.0"):GetLocale("MDungeonTeleports")

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

-- Font Check and Change
local UserFont = "Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf"
local UserFontSub = "Interface/AddOns/MDungeonTeleports/media/fonts/expressway_light.ttf"

local function LocaleFontCheck()
    if GetLocale() == "zhCN" then 
        UserFont = "Interface/AddOns/MDungeonTeleports/media/fonts/sarasa.ttf"
        UserFontSub = "Interface/AddOns/MDungeonTeleports/media/fonts/sarasa.ttf"
    elseif GetLocale() == "koKR" then 
        UserFont = "Interface/AddOns/MDungeonTeleports/media/fonts/sarasa.ttf"
        UserFontSub = "Interface/AddOns/MDungeonTeleports/media/fonts/sarasa.ttf"
    else 
        UserFont = "Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf"
        UserFontSub = "Interface/AddOns/MDungeonTeleports/media/fonts/expressway_light.ttf"
    end
end

LocaleFontCheck()

-- 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 UserFontSub, 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(UserFont, fontSize, flags)
   subText:SetText(text)
   subText:SetJustifyH("LEFT")
   return subText
end

-- first launch window text
local firstPara1 = createSubtitle(FeatureWindow, "TOPLEFT", 10, -10, 14, L["FW_TITLE_1"])
local firstSubt1 = createSubtitle(FeatureWindow, "TOPLEFT", 60, -26, 12, L["FW_SUB_TITLE_1"].. version)
local firstPara1 = createText(FeatureWindow, "TOPLEFT", 56, -38, 10, L["FW_SUB_TITLE_2"].. version .. "?")

local firstSubt2 = createSubtitle(FeatureWindow, "TOPLEFT", 10, -50, 12, L["FW_TITLE_2"])
local firstPara2 = createText(FeatureWindow, "TOPLEFT", 14, -62, 10, L["FW_TEXT_1"])

local firstSubt3 = createSubtitle(FeatureWindow, "TOPLEFT", 10, -134, 12, L["FW_TITLE_3"])
local firstPara3 = createText(FeatureWindow, "TOPLEFT", 14, -146, 10, L["FW_TEXT_2"])

local firstSubt4 = createSubtitle(FeatureWindow, "TOPLEFT", 10, -178, 12, L["FW_TITLE_4"])
local firstPara4 = createText(FeatureWindow, "TOPLEFT", 14, -190, 10, L["FW_TEXT_3"])

-- addon links
local firstSubt5 = createSubtitle(FeatureWindow, "TOPLEFT", 325, -167, 12, L["ADDON_LINK"])

-- 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(UserFont, 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(UserFont, 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
)

