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

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

    if MDungeonTeleports.Version == nil or MDungeonTeleports.Version > currentVersion then
        --MDungeonTeleports.Version = currentVersion
        MDT_LaunchFeatureWindow()
        return
    end

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

-- font set
local defaultFont = "Interface/AddOns/MDungeonTeleports/media/fonts/accia.ttf"

local function FontSet()
    if GetLocale() == "zhCN" or GetLocale() == "koKR" or GetLocale() == "zhTW" then
        defaultFont = GameFontNormal:GetFont()
    else
        defaultFont = "Interface/AddOns/MDungeonTeleports/media/fonts/accia.ttf"
    end
end

FontSet()

-- MARK: First Launch Frame
local FeatureWindow = CreateFrame("Frame", "FeatureWindow", UIParent)
FeatureWindow:SetSize(578, 342)
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.3)
--------------------------------------------
-- MARK: Backdrop and Border
local FeatureWindowBackdrop = FeatureWindow:CreateTexture(nil, "BACKGROUND")
FeatureWindowBackdrop:SetAllPoints(FeatureWindow)
FeatureWindowBackdrop:SetTexture("Interface\\AddOns\\MDungeonTeleports\\media\\ui\\launch_frame.blp")
FeatureWindowBackdrop:SetTexCoord(0, 0.846679, 0, 1)
--------------------------------------------
-- MARK: Titles
-- Title Sizes and Colours
local MainTitleSize = 22
local SubTitleSize = 16
local DescTitleSize = 12
local MainTitleColour = {223/255, 185/255, 69/255}
local SubTitleColour = {1, 1, 1}
local DescTitleColour = {172/255, 172/255, 172/255}

local FeatureTitle = FeatureWindow:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
FeatureTitle:SetPoint("TOP", FeatureWindow, "TOP", 0, -18)
FeatureTitle:SetFont(defaultFont, MainTitleSize, nil)
FeatureTitle:SetJustifyH("CENTER")
FeatureTitle:SetTextColor(unpack(MainTitleColour))
FeatureTitle:SetText(L["FEATURE_TITLE"])

local FeatureTitle2 = FeatureWindow:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
FeatureTitle2:SetPoint("TOP", FeatureWindow, "TOP", 0, -44)
FeatureTitle2:SetFont(defaultFont, SubTitleSize, nil)
FeatureTitle2:SetJustifyH("CENTER")
FeatureTitle2:SetTextColor(unpack(DescTitleColour))
FeatureTitle2:SetText(L["VER_TITLE"])

local icon1 = FeatureWindow:CreateTexture(nil, "ARTWORK")
icon1:SetSize(48, 48)
icon1:SetPoint("TOPLEFT", FeatureWindow, "TOPLEFT", 40, -82)
icon1:SetTexture("Interface\\Icons\\Icon_treasuremap")
icon1:SetTexCoord(0.15, 0.85, 0.15, 0.85)

local icon1Border = FeatureWindow:CreateTexture(nil, "OVERLAY")
icon1Border:SetSize(58, 58)
icon1Border:SetPoint("CENTER", icon1, "CENTER", 0, 0)
icon1Border:SetTexture("interface\\professions\\professions.blp")
icon1Border:SetTexCoord(0.173828125, 0.22265625, 0.5380859375, 0.5869140625)

local FeatureTitle3 = FeatureWindow:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
FeatureTitle3:SetPoint("TOPLEFT", FeatureWindow, "TOPLEFT", 100, -88)
FeatureTitle3:SetFont(defaultFont, SubTitleSize, nil)
FeatureTitle3:SetJustifyH("LEFT")
FeatureTitle3:SetTextColor(unpack(SubTitleColour))
FeatureTitle3:SetText(L["FEATURE_1"])

local FeatureTitle4 = FeatureWindow:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
FeatureTitle4:SetPoint("TOPLEFT", FeatureWindow, "TOPLEFT", 100, -106)
FeatureTitle4:SetFont(defaultFont, DescTitleSize, nil)
FeatureTitle4:SetJustifyH("LEFT")
FeatureTitle4:SetTextColor(unpack(DescTitleColour))
FeatureTitle4:SetText(L["FEATURE_DESC_1"])

local icon2 = FeatureWindow:CreateTexture(nil, "ARTWORK")
icon2:SetSize(48, 48)
icon2:SetPoint("TOPLEFT", FeatureWindow, "TOPLEFT", 40, -138)
icon2:SetTexture("Interface\\Icons\\Inv_10_specialization_professionbook_enchanting_color2")
icon2:SetTexCoord(0.15, 0.85, 0.15, 0.85)

local icon2Border = FeatureWindow:CreateTexture(nil, "OVERLAY")
icon2Border:SetSize(58, 58)
icon2Border:SetPoint("CENTER", icon2, "CENTER", 0, 0)
icon2Border:SetTexture("interface\\professions\\professions.blp")
icon2Border:SetTexCoord(0.173828125, 0.22265625, 0.5380859375, 0.5869140625)

local FeatureTitle5 = FeatureWindow:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
FeatureTitle5:SetPoint("TOPLEFT", FeatureWindow, "TOPLEFT", 100, -140)
FeatureTitle5:SetFont(defaultFont, SubTitleSize, nil)
FeatureTitle5:SetJustifyH("LEFT")
FeatureTitle5:SetTextColor(unpack(SubTitleColour))
FeatureTitle5:SetText(L["FEATURE_2"])

local FeatureTitle6 = FeatureWindow:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
FeatureTitle6:SetPoint("TOPLEFT", FeatureWindow, "TOPLEFT", 100, -158)
FeatureTitle6:SetFont(defaultFont, DescTitleSize, nil)
FeatureTitle6:SetJustifyH("LEFT")
FeatureTitle6:SetTextColor(unpack(DescTitleColour))
FeatureTitle6:SetText(L["FEATURE_DESC_2"])

local icon3 = FeatureWindow:CreateTexture(nil, "ARTWORK")
icon3:SetSize(48, 48)
icon3:SetPoint("TOPLEFT", FeatureWindow, "TOPLEFT", 40, -196)
icon3:SetTexture("Interface\\Icons\\Inv_engineering_90_toolbox_yellow")
icon3:SetTexCoord(0.15, 0.85, 0.15, 0.85)

local icon3Border = FeatureWindow:CreateTexture(nil, "OVERLAY")
icon3Border:SetSize(58, 58)
icon3Border:SetPoint("CENTER", icon3, "CENTER", 0, 0)
icon3Border:SetTexture("interface\\professions\\professions.blp")
icon3Border:SetTexCoord(0.173828125, 0.22265625, 0.5380859375, 0.5869140625)

local FeatureTitle7 = FeatureWindow:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
FeatureTitle7:SetPoint("TOPLEFT", FeatureWindow, "TOPLEFT", 100, -202)
FeatureTitle7:SetFont(defaultFont, SubTitleSize, nil)
FeatureTitle7:SetJustifyH("LEFT")
FeatureTitle7:SetTextColor(unpack(SubTitleColour))
FeatureTitle7:SetText(L["FEATURE_3"])

local FeatureTitle8 = FeatureWindow:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
FeatureTitle8:SetPoint("TOPLEFT", FeatureWindow, "TOPLEFT", 100, -220)
FeatureTitle8:SetFont(defaultFont, DescTitleSize, nil)
FeatureTitle8:SetJustifyH("LEFT")
FeatureTitle8:SetTextColor(unpack(DescTitleColour))
FeatureTitle8:SetText(L["FEATURE_DESC_3"])

local FeatureTitle9 = FeatureWindow:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
FeatureTitle9:SetPoint("TOP", FeatureWindow, "TOP", 0, -275)
FeatureTitle9:SetFont(defaultFont, DescTitleSize, nil)
FeatureTitle9:SetJustifyH("LEFT")
FeatureTitle9:SetTextColor(unpack(DescTitleColour))
FeatureTitle9:SetText(L["MORE_UPDATES"])
--------------------------------------------
-- MARK: FeatureWindow Close Button
local closeButtonContainer = AceGUI:Create("SimpleGroup")
closeButtonContainer:SetLayout("Flow")
closeButtonContainer:SetWidth(60)
closeButtonContainer:SetHeight(32)
closeButtonContainer.frame:SetScale(1.7)
closeButtonContainer.frame:SetParent(FeatureWindow)
closeButtonContainer.frame:SetPoint("TOP", FeatureWindow, "TOP", 0, -175)
closeButtonContainer.frame:Show()

local aceCloseButton = AceGUI:Create("Button")
aceCloseButton:SetWidth(90)
aceCloseButton.frame:SetScale(0.6)
local buttonText = aceCloseButton.frame:GetFontString()
if buttonText then
    buttonText:SetFont(defaultFont, 14, nil)
end
aceCloseButton:SetText("Got it!")
aceCloseButton:SetCallback("OnClick", function()
    PlaySound(SOUNDKIT.IG_MAINMENU_OPTION_CHECKBOX_ON)
        if MDungeonTeleports.Version == nil or MDungeonTeleports.Version < 60000 then
            MDungeonTeleports = {
                Season1Pos = {x = 392, y = -98, anchor = "TOPRIGHT"},
                PortalLibraryPos = {x = 781, y = -98, anchor = "TOPRIGHT"},
                PortalLibraryRaidPos = {x = 909, y = -98, anchor = "TOPRIGHT"},
                KeyTrackerPos = {x = 794, y = -206, anchor = "TOPLEFT"},
                showTooltips = false,
                ShiftDragToggle = false,
                isRespondChecked = true,
                isSeason1PortalsShown = true,
                isPortalLibraryShown = true,
                isPortalLibraryRaidShown = true,
                isKeyFrameShown = true,
                loginMessage = true,
                frameScale = 1,
                KeyTrackerFrameScale = 1,
                borderColor = { r = 0.05, g = 0.05, b = 0.05, a = 0.8 },
                backgroundColor = { r = 0, g = 0, b = 0, a = 0.6 },
                expansionColor = { r = 0, g = 1, b = 0.6 },
                titleColor = { r = 1, g = 1, b = 1 },
                buttonTextColor = { r = 1, g = 1, b = 1 },
                borderThickness = 2,
                customFontName = "Expressway",
                customFont = "Interface\\AddOns\\MDungeonTeleports\\media\\fonts\\expressway.ttf",
                settingsScale = 1,
                announcePort = false,
                LockIcons = false,
                DarkenText = false,
                rioScore = true,
                realmName = false,
                dragHandle = true,
                selectedPreset = 1,
                buttonBorderColor = { r = 0.05, g = 0.05, b = 0.05, a = 0.8 },
                KeyTrackerWidth = 160,
                lastVersionCheck = nil,
                expansionVisibility = {
                    MID = true,
                    TWW = true,
                    DF = true,
                    SL = true,
                    BFA = true,
                    Legion = true,
                    WOD = true,
                    MOP = true,
                    CATA = true,
                    WRATH = true,
                },
                raidExpansionVisibility = {
                    TWW = true,
                    DF = true,
                    SL = true,
                },
                lastLogin = "2026-02-01",
                teleportsTab = true,
                mythicTabButtons = true,
                mythicTabVertical = true,
                worldMapTab = true,
                selectedHearthstoneId = 6948,
                TextSizeButton = 11,
                TextSizeExpansion = 12,
                TextSizeTitle = 12,
                HideSettingsButton = false,
                minimapButton = { hide = false },
                MapSeason1 = true,
                ClassTeleports = true,
                RaceTeleports = true,
                randomHearthstone = false,
                ShowDalaranHS = true,
                ShowGarrisonHS = true,
                ShowArcantina = true,
                centerDungeonPortals = false,
                centerRaidPortals = false,
                hideMissingPortals = false,
                ShowPlotTeleport = true,
                MapMidnight = true,
                Version = 60100
            }
        -- Set locale-specific fonts
        local locale = GetLocale and GetLocale() or "enUS"
            if locale == "zhCN" then
                MDungeonTeleports.customFont = "Interface\\AddOns\\MDungeonTeleports\\media\\fonts\\sarasa_sc.ttf"
                MDungeonTeleports.customFontName = "SarasaSC"
            elseif locale == "koKR" then
                MDungeonTeleports.customFont = "Interface\\AddOns\\MDungeonTeleports\\media\\fonts\\wanted.ttf"
                MDungeonTeleports.customFontName = "Wanted"
            elseif locale == "zhTW" then
                MDungeonTeleports.customFont = "Interface\\AddOns\\MDungeonTeleports\\media\\fonts\\sarasa_tc.ttf"
                MDungeonTeleports.customFontName = "SarasaTC"            
            else
                MDungeonTeleports.customFont = "Interface\\AddOns\\MDungeonTeleports\\media\\fonts\\expressway.ttf"
                MDungeonTeleports.customFontName = "Expressway"
        end
        ReloadUI()
    end
    if MDungeonTeleports.Version == 60000 then
        MDungeonTeleports.Version = 60100
        FeatureWindow:Hide()
    end
end)

aceCloseButton:SetCallback("OnEnter", function(widget)
    GameTooltip:SetOwner(widget.frame, "ANCHOR_TOP")
    GameTooltip:SetText(L["RESET_ADDON_TIP"], 1, 1, 1)
    GameTooltip:Show()
end)
aceCloseButton:SetCallback("OnLeave", function()
    GameTooltip:Hide()
end)

closeButtonContainer:AddChild(aceCloseButton)
--------------------------------------------
-- MARK: Launch Feature Window
function MDT_LaunchFeatureWindow()
   FeatureWindow:Show()
end

local launchFrame = CreateFrame("Frame")
launchFrame:RegisterEvent("ADDON_LOADED")
launchFrame:RegisterEvent("PLAYER_LOGIN")
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
)

--[[ update logic so i don't brain forget, if first launch ver is nil or 0 so launch opens and ver is set to current ver and then not touched again
if ver is less than current ver then launch opens and ver is set to current ver
if ver is equal to current ver then do nothing
if ver is greater than current ver then launch opens and ver is set to current ver but depending on if resets are needed hmm, i will forget
]]--