-- Frame Pack Table
local FramePackCategories = {
    {
        category = "Default Frames",
        items = {
            {
                text = "Default Frames",
                value = "Interface/Addons/MDungeonTeleports/media/frames/default_frames.blp",
                value2 = "Interface/AddOns/MDungeonTeleports/media/ui/default_ui/"
            }
        }
    },
    {
        category = "Coloured Frames",
        items = {
            {
                text = "Red Shift",
                value = "Interface/Addons/MDungeonTeleports/media/frames/red_shift.blp",
                value2 = "Interface/AddOns/MDungeonTeleports/media/ui/redshift_ui/"
            },
            {
                text = "Blue Shift",
                value = "Interface/AddOns/MDungeonTeleports/media/frames/blue_shift.blp",
                value2 = "Interface/AddOns/MDungeonTeleports/media/ui/blueshift_ui/"
            },
            {
                text = "Toxic Claws",
                value = "Interface/Addons/MDungeonTeleports/media/frames/toxic_claws.blp",
                value2 = "Interface/AddOns/MDungeonTeleports/media/ui/toxic_ui/"
            },
            {
                text = "Purple Glow",
                value = "Interface/Addons/MDungeonTeleports/media/frames/purple_glow.blp",
                value2 = "Interface/AddOns/MDungeonTeleports/media/ui/purpleglow_ui/"
            }
        }
    },
    {
        category = "Events",
        items = {
            {
                text = "Hallow's End",
                value = "Interface/Addons/MDungeonTeleports/media/frames/hallows_end.blp",
                value2 = "Interface/AddOns/MDungeonTeleports/media/ui/hallowsend_ui/"
            },
            {
                text = "Winter's Veil",
                value = "Interface/Addons/MDungeonTeleports/media/frames/winters_veil.blp",
                value2 = "Interface/AddOns/MDungeonTeleports/media/ui/wintersveil_ui/"
            }
        }
    },
    {
        category = "Custom",
        items = {
            {
                text = "Custom 1",
                value = "Interface/Addons/MDungeonTeleports/media/frames/custom.blp",
                value2 = "Interface/AddOns/MDungeonTeleports/media/ui/custom_ui/"
            },
            {
                text = "Custom 2",
                value = "Interface/Addons/MDungeonTeleports/media/frames/custom2.blp",
                value2 = "Interface/AddOns/MDungeonTeleports/media/ui/custom2_ui/"
            },
            {
                text = "Custom 3",
                value = "Interface/Addons/MDungeonTeleports/media/frames/custom3.blp",
                value2 = "Interface/AddOns/MDungeonTeleports/media/ui/custom3_ui/"
            }
        }
    }
}
    
-- Patch Notes
local patchNotes =
    [[to be removed]]

-- Default settings
local defaultSettings = {
    isSeason1PortalsShown = true,
    isPortalLibraryShown = true,
    isPortalLibraryRaidShown = true,
    selectedFramePack = FramePackCategories[1].items[1].value
}


-- load variables
if not MDungeonTeleports then
    MDungeonTeleports = {}
end

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

local function GetCurrentDate()
    return date("%Y%m%d") -- Returns the current date as a string, e.g., "20240926"
end

-- Set variables
MDungeonTeleports.isSeason1PortalsShown =
    MDungeonTeleports.isSeason1PortalsShown ~= nil and MDungeonTeleports.isSeason1PortalsShown or
    defaultSettings.isSeason1PortalsShown
MDungeonTeleports.isPortalLibraryShown =
    MDungeonTeleports.isPortalLibraryShown ~= nil and MDungeonTeleports.isPortalLibraryShown or
    defaultSettings.isPortalLibraryShown
MDungeonTeleports.isPortalLibraryRaidShown =
    MDungeonTeleports.isPortalLibraryRaidShown ~= nil and MDungeonTeleports.isPortalLibraryRaidShown or
    defaultSettings.isPortalLibraryRaidShown
MDungeonTeleports.selectedFramePack = MDungeonTeleports.selectedFramePack or FramePackCategories[1].items[1].value

-- Function to check if it's the first launch
local function IsFirstLaunch()
    -- Ensure MDungeonTeleports exists and firstLaunch is properly initialized
    if MDungeonTeleports == nil then
        -- Initialize the variable and mark it as first launch
        MDungeonTeleports = {}
        MDungeonTeleports.firstLaunch = true
        return true
    elseif MDungeonTeleports.firstLaunch == nil then
        -- If firstLaunch has not been set, treat this as the first launch
        MDungeonTeleports.firstLaunch = true
        return true
    elseif MDungeonTeleports.firstLaunch == true then
        -- If firstLaunch is still true, it's the first launch
        MDungeonTeleports.firstLaunch = false -- Set to false for future launches
        return true
    else
        -- If firstLaunch is false, it's not the first launch
        return false
    end
end

-- Function to show or hide the firstLaunch frame
local function ShowFirstLaunchFrame()
    if IsFirstLaunch() then
        -- Show the frame on first launch
        if firstLaunch then
            firstLaunch:Show()
        end
    else
        -- Hide the frame on subsequent launches
        if firstLaunch then
            firstLaunch:Hide()
        end
    end
end

local function SaveSettings()
    if Season1CheckBox and PortalLibraryCheckBox and PortalLibraryRaidCheckBox then
        MDungeonTeleports.isSeason1PortalsShown = Season1CheckBox:GetChecked()
        MDungeonTeleports.isPortalLibraryShown = PortalLibraryCheckBox:GetChecked()
        MDungeonTeleports.isPortalLibraryRaidShown = PortalLibraryRaidCheckBox:GetChecked()
    end

    if selectedFramePackValue then
        MDungeonTeleports.selectedFramePack = selectedFramePackValue
    end
    MDungeonTeleports.frameScale = uiScaleSlider:GetValue()
end

-- toggle season1 portals
local function ToggleSeason1Portals(show)
    if Season_UpdateVisibility then
        Season_UpdateVisibility(show)
    end
    SaveSettings()
end

-- toggle portal library
local function TogglePortalLibrary(show)
    if PortalLibrary_UpdateVisibility then
        PortalLibrary_UpdateVisibility(show)
    end
    SaveSettings()
end

-- toggle raid portal library
local function TogglePortalLibraryRaid(show)
    if PortalLibraryRaid_UpdateVisibility then
        PortalLibraryRaid_UpdateVisibility(show)
    end
    SaveSettings()
end

-- Global function to update frame scales
function UpdateFrameScale(scale)
    -- Update each frame's scale
    if Season1 then
        Season1:SetScale(scale)
    end
    if PortalLibrary then
        PortalLibrary:SetScale(scale)
    end
    if PortalLibraryRaid then
        PortalLibraryRaid:SetScale(scale)
    end
    -- Save the scale value
    MDungeonTeleports.frameScale = scale
end

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

-- backgroundSettings
local backgroundSettings = Settings:CreateTexture(nil, "BACKGROUND")
backgroundSettings:SetAllPoints(Settings)
backgroundSettings:SetTexture(MDungeonTeleports.selectedFramePack)
backgroundSettings:SetTexCoord(0.5, 0.851562, 0, 0.185546)

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

-- checkboxes
local function CreateCheckboxes()
    Season1CheckBox = CreateFrame("CheckButton", nil, Settings, "ChatConfigCheckButtonTemplate")
    Season1CheckBox:SetPoint("CENTER", Settings, "TOPLEFT", 20, -40)
    local font, _, flags = Season1CheckBox.Text:GetFont()
    Season1CheckBox.Text:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 10, flags)
    Season1CheckBox.Text:SetPoint("LEFT", Season1CheckBox, "RIGHT", 0, 0)
    Season1CheckBox.Text:SetText("Show/Hide Current Season")
    Season1CheckBox:SetChecked(MDungeonTeleports.isSeason1PortalsShown)
    Season1CheckBox:SetScript(
        "OnClick",
        function(self)
            ToggleSeason1Portals(self:GetChecked())
        end
    )

    Season1CheckBox:SetNormalTexture("Interface/AddOns/MDungeonTeleports/media/ui/ui_elements/checkbox.blp")
    Season1CheckBox:SetPushedTexture("Interface/AddOns/MDungeonTeleports/media/ui/ui_elements/checkbox_pushed.blp")
    Season1CheckBox:SetHighlightTexture(
        "Interface/AddOns/MDungeonTeleports/media/ui/ui_elements/checkbox_highlight.blp"
    )
    Season1CheckBox:SetCheckedTexture("Interface/AddOns/MDungeonTeleports/media/ui/ui_elements/checkbox_checked.blp")

    PortalLibraryCheckBox = CreateFrame("CheckButton", nil, Settings, "ChatConfigCheckButtonTemplate")
    PortalLibraryCheckBox:SetPoint("CENTER", Settings, "TOPLEFT", 20, -60)
    local font, _, flags = PortalLibraryCheckBox.Text:GetFont()
    PortalLibraryCheckBox.Text:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 10, flags)
    PortalLibraryCheckBox.Text:SetPoint("LEFT", PortalLibraryCheckBox, "RIGHT", 0, 0)
    PortalLibraryCheckBox.Text:SetText("Show/Hide Portal Library")
    PortalLibraryCheckBox:SetChecked(MDungeonTeleports.isPortalLibraryShown)
    PortalLibraryCheckBox:SetScript(
        "OnClick",
        function(self)
            TogglePortalLibrary(self:GetChecked())
        end
    )

    PortalLibraryCheckBox:SetNormalTexture("Interface/AddOns/MDungeonTeleports/media/ui/ui_elements/checkbox.blp")
    PortalLibraryCheckBox:SetPushedTexture(
        "Interface/AddOns/MDungeonTeleports/media/ui/ui_elements/checkbox_pushed.blp"
    )
    PortalLibraryCheckBox:SetHighlightTexture(
        "Interface/AddOns/MDungeonTeleports/media/ui/ui_elements/checkbox_highlight.blp"
    )
    PortalLibraryCheckBox:SetCheckedTexture(
        "Interface/AddOns/MDungeonTeleports/media/ui/ui_elements/checkbox_checked.blp"
    )

    PortalLibraryRaidCheckBox = CreateFrame("CheckButton", nil, Settings, "ChatConfigCheckButtonTemplate")
    PortalLibraryRaidCheckBox:SetPoint("CENTER", Settings, "TOPLEFT", 20, -80)
    local font, _, flags = PortalLibraryRaidCheckBox.Text:GetFont()
    PortalLibraryRaidCheckBox.Text:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 10, flags)
    PortalLibraryRaidCheckBox.Text:SetPoint("LEFT", PortalLibraryRaidCheckBox, "RIGHT", 0, 0)
    PortalLibraryRaidCheckBox.Text:SetText("Show/Hide Portal Library [Raid]")
    PortalLibraryRaidCheckBox:SetChecked(MDungeonTeleports.isPortalLibraryRaidShown)
    PortalLibraryRaidCheckBox:SetScript(
        "OnClick",
        function(self)
            TogglePortalLibraryRaid(self:GetChecked())
        end
    )

    PortalLibraryRaidCheckBox:SetNormalTexture("Interface/AddOns/MDungeonTeleports/media/ui/ui_elements/checkbox.blp")
    PortalLibraryRaidCheckBox:SetPushedTexture(
        "Interface/AddOns/MDungeonTeleports/media/ui/ui_elements/checkbox_pushed.blp"
    )
    PortalLibraryRaidCheckBox:SetHighlightTexture(
        "Interface/AddOns/MDungeonTeleports/media/ui/ui_elements/checkbox_highlight.blp"
    )
    PortalLibraryRaidCheckBox:SetCheckedTexture(
        "Interface/AddOns/MDungeonTeleports/media/ui/ui_elements/checkbox_checked.blp"
    )

    -- tooltips
    ToggleTooltips = CreateFrame("CheckButton", nil, Settings, "ChatConfigCheckButtonTemplate")
    ToggleTooltips:SetPoint("CENTER", Settings, "TOPLEFT", 20, -120)
    local font, _, flags = ToggleTooltips.Text:GetFont()
    ToggleTooltips.Text:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 10, flags)
    ToggleTooltips.Text:SetPoint("LEFT", ToggleTooltips, "RIGHT", 0, 0)
    ToggleTooltips.Text:SetText("Show/Hide Tooltips")
    ToggleTooltips:SetChecked(MDungeonTeleports.showTooltips)
    ToggleTooltips:SetScript(
        "OnClick",
        function(self)
            MDungeonTeleports.showTooltips = self:GetChecked()
        end
    )

    ToggleTooltips:SetNormalTexture("Interface/AddOns/MDungeonTeleports/media/ui/ui_elements/checkbox.blp")
    ToggleTooltips:SetPushedTexture("Interface/AddOns/MDungeonTeleports/media/ui/ui_elements/checkbox_pushed.blp")
    ToggleTooltips:SetHighlightTexture("Interface/AddOns/MDungeonTeleports/media/ui/ui_elements/checkbox_highlight.blp")
    ToggleTooltips:SetCheckedTexture("Interface/AddOns/MDungeonTeleports/media/ui/ui_elements/checkbox_checked.blp")
end

CreateCheckboxes()

-- Dropdown
local FramePackDropdown = CreateFrame("Frame", "FramePackDropdown", Settings, "UIDropDownMenuTemplate")
FramePackDropdown:SetPoint("CENTER", Settings, "TOPRIGHT", -85, -60)

-- Create a font string for the label
local FramePackDropdownLabel = FramePackDropdown:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
local font, _, flags = FramePackDropdownLabel:GetFont()
FramePackDropdownLabel:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 10, flags)
FramePackDropdownLabel:SetPoint("TOP", FramePackDropdown, "TOP", 0, 10)
FramePackDropdownLabel:SetText("Select Frame Pack") -- Fixed label text

local selectedFramePackValue = MDungeonTeleports.selectedFramePack
local selectedUIPack = MDungeonTeleports.selectedUIPack

-- Function to update the dropdown text based on the selected frame pack
local function UpdateDropdownText(value)
    -- Find the corresponding text for the selected value
    for _, category in pairs(FramePackCategories) do
        for _, option in pairs(category.items) do
            if option.value == value then
                -- Update the text shown inside the dropdown
                UIDropDownMenu_SetText(FramePackDropdown, option.text) -- Update dropdown display text
                return
            end
        end
    end
    -- Fallback text if no match is found
    UIDropDownMenu_SetText(FramePackDropdown, "Select Frame Pack") -- Keep the dropdown text consistent
end

local function InitializeDropdown(self, level, menulist)
    local info = UIDropDownMenu_CreateInfo()

    -- Level 1: Show main categories
    if level == 1 then
        for _, category in pairs(FramePackCategories) do
            info.text = category.category
            info.hasArrow = true -- Indicates a submenu
            info.notCheckable = true
            info.menuList = category.items -- Pass the items to level 2
            UIDropDownMenu_AddButton(info, level)
        end
    -- Level 2: Show the frame packs within each category
    elseif level == 2 and menulist then
        for _, option in pairs(menulist) do
            info.text = option.text
            info.func = function(self)
                -- Set the selected frame pack and UI pack
                selectedFramePackValue = option.value
                selectedUIPack = option.value2

                -- Update the saved variables
                MDungeonTeleports.selectedFramePack = selectedFramePackValue
                MDungeonTeleports.selectedUIPack = selectedUIPack

                -- Set the dropdown to show the selected value
                UIDropDownMenu_SetSelectedValue(FramePackDropdown, selectedFramePackValue)

                -- Update the dropdown text to reflect the selected option
                UpdateDropdownText(selectedFramePackValue)

                -- Update the frame pack background texture
                backgroundSettings:SetTexture(selectedFramePackValue)

                -- Call update functions for UI
                if UpdateSeason1Background then UpdateSeason1Background() end
                if UpdatePortalLibraryBackground then UpdatePortalLibraryBackground() end
                if UpdatePortalLibRaidBackground then UpdatePortalLibRaidBackground() end

                -- Update UI packs
                if UpdateSeason1UI then UpdateSeason1UI() end
                if UpdatePortalLibraryUI then UpdatePortalLibraryUI() end
                if UpdatePortalLibraryRaidUI then UpdatePortalLibraryRaidUI() end
                if UpdateSettingsUI then UpdateSettingsUI() end

                -- Save settings
                SaveSettings()
            end
            info.notCheckable = true
            UIDropDownMenu_AddButton(info, level)
        end
    end
end

-- Initialize the dropdown
UIDropDownMenu_Initialize(FramePackDropdown, InitializeDropdown)
UIDropDownMenu_SetWidth(FramePackDropdown, 120)
UIDropDownMenu_SetButtonWidth(FramePackDropdown, 164)
UIDropDownMenu_SetSelectedValue(FramePackDropdown, selectedFramePackValue)
UIDropDownMenu_JustifyText(FramePackDropdown, "LEFT")

-- Set the initial dropdown text based on the selected value
UpdateDropdownText(selectedFramePackValue) -- Update to reflect the initial selection

-- ui scale slider
local uiScaleSlider = CreateFrame("Slider", "uiScaleSlider", Settings, "OptionsSliderTemplate")
uiScaleSlider:SetPoint("center", Settings, "CENTER", 0, -70)
uiScaleSlider:SetMinMaxValues(0.8, 1.2)
uiScaleSlider:SetValue(MDungeonTeleports.scaleSliderValue or 50)
uiScaleSlider:SetValueStep(0.1)
uiScaleSlider:SetWidth(200)

-- minimum label
local minLabel = _G[uiScaleSlider:GetName() .. "Low"]
minLabel:SetText("0.8")
minLabel:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 10, flags)
-- maximum label
local maxLabel = _G[uiScaleSlider:GetName() .. "High"]
maxLabel:SetText("1.2")
maxLabel:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 10, flags)
-- text label for the slider
local textLabel = _G[uiScaleSlider:GetName() .. "Text"]
textLabel:SetText("UI Scale")
textLabel:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 10, flags)
-- OnValueChanged handler for the slider
uiScaleSlider:SetScript(
    "OnValueChanged",
    function(self, value)
        -- Round to one decimal place
        local scale = math.floor(value * 10 + 0.5) / 10
        -- Update the scale globally
        UpdateFrameScale(scale)
    end
)

uiScaleSlider:SetValue(MDungeonTeleports.frameScale or 1.0)

-- Update the UI with the current settings
local function UpdateUI()
    if Season1CheckBox and PortalLibraryCheckBox then
        Season1CheckBox:SetChecked(MDungeonTeleports.isSeason1PortalsShown)
        PortalLibraryCheckBox:SetChecked(MDungeonTeleports.isPortalLibraryShown)
        PortalLibraryRaidCheckBox:SetChecked(MDungeonTeleports.isPortalLibraryRaidShown)
        UIDropDownMenu_SetSelectedValue(FramePackDropdown, MDungeonTeleports.selectedFramePack)
        if MDungeonTeleports.selectedFramePack then
            backgroundSettings:SetTexture(MDungeonTeleports.selectedFramePack)
        end
        ToggleSeason1Portals(MDungeonTeleports.isSeason1PortalsShown)
        TogglePortalLibrary(MDungeonTeleports.isPortalLibraryShown)
        TogglePortalLibraryRaid(MDungeonTeleports.isPortalLibraryRaidShown)
    end
end

-- reset button
local resetButton = CreateFrame("Button", "ResetButton", Settings)
resetButton:SetSize(14, 14)
resetButton:SetPoint("BOTTOMLEFT", Settings, "BOTTOMLEFT", 7, 5)
resetButton:SetFrameStrata("DIALOG")

local resetButtonTexture = resetButton:CreateTexture(nil, "BACKGROUND")
resetButtonTexture:SetAllPoints(resetButton)
resetButtonTexture:SetTexture("Interface\\AddOns\\MDungeonTeleports\\media\\ui\\default_ui\\button_reset.blp")

local resetButtonHighlight = resetButton:CreateTexture(nil, "HIGHLIGHT")
resetButtonHighlight:SetAllPoints(resetButton)
resetButtonHighlight:SetTexture("Interface\\AddOns\\MDungeonTeleports\\media\\ui\\default_ui\\button_reset_highlight.blp")
resetButtonHighlight:Hide()

resetButton.resetButtonHighlight = resetButtonHighlight
resetButton.resetButtonTexture = resetButtonTexture

resetButton:SetScript(
    "OnEnter",
    function(self)
        self.resetButtonHighlight:Show()
        self.resetButtonTexture:Hide()
        GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
        GameTooltip:SetText("Reset Addon UI", 1, 1, 1)
        GameTooltip:AddLine("Reset your addon. This will reload your UI", nil, nil, nil, true)
        GameTooltip:Show()
    end
)

resetButton:SetScript(
    "OnLeave",
    function(self)
        self.resetButtonHighlight:Hide()
        self.resetButtonTexture:Show()
        GameTooltip:Hide()
    end
)

resetButton:SetScript(
    "OnClick",
    function(self)
        MDungeonTeleports.PortalLibraryRaidPos = {x = 970, y = -195}
        MDungeonTeleports.PortalLibraryPos = {x = 970, y = -100}
        MDungeonTeleports.Season1Pos = {x = 620, y = 220}
        MDungeonTeleports.frameScale = 1
        MDungeonTeleports.isPortalLibraryRaidShown = true
        MDungeonTeleports.isSeason1PortalsShown = true
        MDungeonTeleports.isPortalLibraryShown = true
        MDungeonTeleports.showTooltips = false
        MDungeonTeleports.firstLaunch = false
        MDungeonTeleports.selectedFramePack = FramePackCategories[1].items[1].value
        MDungeonTeleports.selectedUIPack = "Interface/AddOns/MDungeonTeleports/media/ui/default_ui/"
        ReloadUI()
    end
)

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

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

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

settingsExitButton.settingsExitButtonHighlight = settingsExitButtonHighlight
settingsExitButton.settingExitButtonTexture = settingExitButtonTexture

settingsExitButton:SetScript(
    "OnEnter",
    function(self)
        self.settingsExitButtonHighlight:Show()
        self.settingExitButtonTexture:Hide()
    end
)

settingsExitButton:SetScript(
    "OnLeave",
    function(self)
        self.settingsExitButtonHighlight:Hide()
        self.settingExitButtonTexture:Show()
    end
)

settingsExitButton:SetScript(
    "OnClick",
    function()
        Settings:Hide()
    end
)

-- settings button
local settingsOpenButton = CreateFrame("Button", "SettingsOpenButton", PVEFrame)
settingsOpenButton:SetSize(14, 14)
settingsOpenButton:SetPoint("TOPRIGHT", PVEFrame, "TOPRIGHT", -22, -5)
settingsOpenButton:SetFrameStrata("DIALOG")

local settingsOpenButtonTexture = settingsOpenButton:CreateTexture(nil, "BACKGROUND")
settingsOpenButtonTexture:SetAllPoints(settingsOpenButton)
settingsOpenButtonTexture:SetTexture("Interface\\AddOns\\MDungeonTeleports\\media\\ui\\default_ui\\button_settings.blp")

local settingsButtonHoverOverlay = settingsOpenButton:CreateTexture(nil, "HIGHLIGHT")
settingsButtonHoverOverlay:SetAllPoints(settingsOpenButton)
settingsButtonHoverOverlay:SetTexture("Interface\\AddOns\\MDungeonTeleports\\media\\ui\\default_ui\\button_settings_highlight.blp")
settingsButtonHoverOverlay:Hide()

settingsOpenButton.settingsOpenButtonTexture = settingsOpenButtonTexture
settingsOpenButton.settingsButtonHoverOverlay = settingsButtonHoverOverlay

settingsOpenButton:SetScript(
    "OnEnter",
    function(self)
        self.settingsButtonHoverOverlay:Show()
        self.settingsOpenButtonTexture:Hide()
        GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
        GameTooltip:SetText("M+ Dungeon Teleports Settings", 1, 1, 1)
        GameTooltip:AddLine("Click to open the settings panel.", nil, nil, nil, true)
        GameTooltip:AddLine("Use Shift+Drag to move your frames.", nil, nil, nil, true)
        if InCombatLockdown() then
            GameTooltip:AddLine("|cFFFF0000Unavailable during combat!|r", nil, nil, nil, true)
        end
        GameTooltip:Show()
    end
)

settingsOpenButton:SetScript(
    "OnLeave",
    function(self)
        self.settingsButtonHoverOverlay:Hide()
        self.settingsOpenButtonTexture:Show()
        GameTooltip:Hide()
    end
)

settingsOpenButton:SetScript(
    "OnClick",
    function(self)
        if InCombatLockdown() then
            print("|cFFFF0000Settings cannot be opened during combat!|r")
            return
        end

        if Settings:IsShown() then
            Settings:Hide()
        else
            Settings:Show()
        end
    end
)

local function UpdateButtonStatus()
    if InCombatLockdown() then
        settingsOpenButton:Disable()
    else
        settingsOpenButton:Enable()
    end
end

settingsOpenButton:RegisterEvent("PLAYER_REGEN_ENABLED")
settingsOpenButton:RegisterEvent("PLAYER_REGEN_DISABLED")

settingsOpenButton:SetScript("OnEvent", UpdateButtonStatus)

UpdateButtonStatus()

local function ToggleSettingsPanel()
    if InCombatLockdown() then
        print("|cFFFF0000Settings cannot be opened during combat!|r")
        return
    end

    if Settings:IsShown() then
        Settings:Hide()
    else
        Settings:Show()
    end
end

-- slash command for /dp
SLASH_DPTOGGLE1 = "/dp"
SlashCmdList["DPTOGGLE"] = function()
    ToggleSettingsPanel()
end

local function OnEvent(self, event, arg1)
    if event == "ADDON_LOADED" and arg1 == "MDungeonTeleports" then
        selectedFramePackValue = MDungeonTeleports.selectedFramePack or FramePackCategories[1].items[1].value
        local savedScale = MDungeonTeleports.frameScale or 1.0
        UIDropDownMenu_SetSelectedValue(FramePackDropdown, selectedFramePackValue)
        uiScaleSlider:SetValue(savedScale)
        UpdateFrameScale(savedScale)
        UpdateUI()
        UpdateSettingsUI()
        UpdateDropdownText(selectedFramePackValue)
    end
end

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

-- firstBackground
local firstBackground = firstLaunch:CreateTexture(nil, "BACKGROUND")
firstBackground:SetAllPoints(firstLaunch)
firstBackground:SetTexture("Interface/Tooltips/UI-Tooltip-Background")
firstBackground:SetVertexColor(0, 0, 0, 0.7)

-- title
local firstTitle = firstLaunch:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
firstTitle:SetPoint("TOP", firstLaunch, "TOP", 0, -10)
firstTitle:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 14, flags)
firstTitle:SetText("M+ Dungeon Teleports")

-- title
local firstSubTitle = firstLaunch:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
firstSubTitle:SetPoint("TOP", firstLaunch, "TOP", 0, -23)
firstSubTitle:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 8, flags)
firstSubTitle:SetText("by peepoStudy")

-- title
local firstText = firstLaunch:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
firstText:SetPoint("TOP", firstLaunch, "TOP", 0, -35)
firstText:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 12, flags)
firstText:SetText("Getting Started")

-- title
local firstSubText = firstLaunch:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
firstSubText:SetPoint("TOP", firstLaunch, "TOP", 0, -55)
firstSubText:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 10, flags)
firstSubText:SetText(
    "To move your Portal Frames around to the desired positons \nyou would like, you can use the key combo of |cFF2CCBB6'Shift+Drag'|r.\n\nYou can customize your Portal Frames in the settings panel\nof the AddOn, use the slash command in chat |cFF2CCBB6'/dp'|r to access\nthem or you can use the |cFF2CCBB6Settings Button|r when \nyou open the Dungeon Finder window.\n\nThanks for downloading the AddOn, you can report problems and \nissues on the CurseForge Project Page or you can use the \nDiscord linked below.\n\nhttps://discord.gg/5AtcdZ9t7Q"
)

-- info exit button button
local firstExit = CreateFrame("Button", "SettingsOpenButton", firstLaunch)
firstExit:SetSize(48, 18)
firstExit:SetPoint("BOTTOM", firstLaunch, "BOTTOM", 0, 7)
firstExit:SetFrameStrata("DIALOG")

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

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

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

firstExit.firstHighlight = firstHighlight

firstExit:SetScript(
    "OnEnter",
    function(self)
        self.firstHighlight:Show()
    end
)

firstExit:SetScript(
    "OnLeave",
    function(self)
        self.firstHighlight:Hide()
    end
)
firstExit:SetScript(
    "OnClick",
    function()
        if firstLaunch:IsShown() then
            firstLaunch:Hide()
        else
            firstLaunch:Show()
        end
    end
)

function UpdateSettingsUI()
    local UIPack = MDungeonTeleports.selectedUIPack or "Interface/Addons/MDungeonTeleports/media/ui/default_ui/"
    -- reset addon button
    -- exit button for settings
    settingExitButtonTexture:SetTexture(UIPack .. "button_close.blp")
    settingsExitButtonHighlight:SetTexture(UIPack .. "button_close_highlight.blp")
    -- portal helper button
    -- info helper button
    -- open settings pve button
    settingsOpenButtonTexture:SetTexture(UIPack .. "button_settings.blp")
    settingsButtonHoverOverlay:SetTexture(UIPack .. "button_settings_highlight.blp")
    -- exit button for info panel
end

local eventFrame = CreateFrame("Frame")
eventFrame:RegisterEvent("ADDON_LOADED")
eventFrame:SetScript("OnEvent", OnEvent)

-- 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" and arg1 == "MDungeonTeleports" then
            -- Call the function to show or hide the frame when the addon is loaded
            ShowFirstLaunchFrame()
        elseif event == "PLAYER_LOGIN" then
            -- Call the function to show or hide the frame when the player logs in
            ShowFirstLaunchFrame()
        end
    end
)

-- login message
local loginmessage = CreateFrame("Frame")
loginmessage:RegisterEvent("PLAYER_LOGIN")
loginmessage:SetScript(
    "OnEvent",
    function(self, event)
        if event == "PLAYER_LOGIN" then
            if not MDungeonTeleports then
                MDungeonTeleports = {}
            end
            local today = GetCurrentDate()
            if MDungeonTeleports.lastLoginMessageDate ~= today then
                DEFAULT_CHAT_FRAME:AddMessage(
                    "[" ..
                        version ..
                            "] M+ Dungeon Teleports. Use /dp to access settings to customize your frames. Use Shift+Drag to move your frames around.",
                    0.8,
                    0.6,
                    1
                )
                MDungeonTeleports.lastLoginMessageDate = today
            end
        end
    end
)

UpdateSettingsUI()
