-- frame pack table
local FramePackItems = {
    {
        text = "Default",
        value = "Interface/Addons/MDungeonTeleports/media/frames/default_frames.blp",
        value2 = "Interface/AddOns/MDungeonTeleports/media/ui/default_ui/"
    },
    {
        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/"
    },
    {
        text = "Hallow's End",
        value = "Interface/Addons/MDungeonTeleports/media/frames/hallows_end.blp",
        value2 = "Interface/AddOns/MDungeonTeleports/media/ui/hallowsend_ui/"
    },
    {
        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/"
    }
}

local patchNotes =
    [[Use Shift+Drag to move your frames around. Use 'dp' to access the settings. How to add your own customs frame can be found below or on the CurseForge project page.   

[2.1.1]

- Added 'Hallows End' Frame Pack

[2.1.0]

- Added 2 more slots for Custom Frame Packs for a total of 3. Custom 1, Custom 2 and Custom 3.
- Added 'Purple Glow' frame pack.

- Updated Checkbox UI
- Updated UI to follow the frame pack selection.
- Updated Portal Helper Icon
- Updated Info Icon
- Updated Button UI Assets.
- Updated Cooldown Overlay to be more transparent than a near blackout.

- Fixed Portal Button Overlays drawing in the wrong orders.
- Fixed UI Button Overlays drawing and clipping.
- Fixed Info Panel UI and Background not updating
- Fixed Portal Helper UI and Background not updating.

- Removed Original Clean [It's just a border difference]

[2.0.2] 

-- New Features --

- Added First Time Launch Window, explaining how the addon works and how to access the settings. [This will show up for all users regardless if you already had it.]
- Added a toggle for tooltips over portals.
- Added Reset Button to reset the AddOn.

-- Updates --

- Updated Checkboxes in Settings.
- Updated First Launch Window Positions.

-- Bug Fixes --

- Fixed Word wrap in Info Panel.

[2.0.1]

- Added a new frame pack 'Toxic Claws'

-- Bug Fixes --

- Settings is no longer accessible in combat.
- Custom Frame Pack option has default frames set until a custom.blp has been added by a user.
- Default frames have their opacity corrected.
- Logo's and text in the Info Panel have been corrected.
- Removed any left over Debug print.
- Info Panel's tooltip now shows.
- Info Panel and Portal Helper now have background for all preset Frame Packs.
- Portal Helper's Title is now correct.
- Info Panel and Portal Helper background now update accordingly.
- Added usage info to the Info Panel.

2.0.0 [Customization Update]

This update comes with a plethora of new features for customization, plenty of QoL changes as well as bug fixes.

-- New Features -- 

- Added Frame Customization [Options below, more are in the works for future updates.]
-- Default
-- Original (Clean)
-- Red Shift
-- Blue Shift
-- Custom [See information below on how to add your own custom frame.]

- Added Settings button to PVEFrame an alternative to '/dp'.
- Added Portal Helper. Shows where portals were acquired from and if you can still get them. [information to be updated and improved]
- Added Info Panel for patch notes and other relevant information.
- Added the ability to scale frames.

-- Updates -- 

- Updated Lock Icon.
- Updated AddOn .toc Logo.
- Updated AddOn ingame .toc name.
- Updated font to be 'Expressway' instead of using your default UI font, everything should be uniform with this.
- Updated buttons to be more like.. buttons. Borders, Highlights e.t.c.
- Updated login message to be once per day, not every login. Version also updates from .toc instead of manually updating.
- Updated buttons sizes to 32x32 from 28x28. 28x28 caused problems with textures and scaling, 32x32 should resolve this.
- Updated positioning of all buttons and frame sizes.
- Updated default_frames.blp and new frames to match new frame sizing.
- Updated Addon .toc Tooltip.

-- Bug Fixes -- 

- Fixed Siege of Boralus portal sometimes choosing the wrong Spell ID.
- Fixed Icon for Sanctum of Domination raid portal.
- Fixed incorrect dungeon abbreviations.

To add your own custom frame, you need to use the template provided in "MDungeonTeleports\media\frames\template.png". Use this in your photo editing software of choice to create your own frames. To load this into your addon, you must save the edited template as 'custom.blp' you can use a converter such as 'BLPNG Convertor' on WoWInterface to save your file as ".blp". Once you have a file saved as 'custom.blp' you need to place it in the addon folder where you first found the template, so the path of the file should be 'MDungeonTeleports\media\frames\custom.blp'. Make sure to reload your game once you have placed the texture in the folder, then once you have reloaded, select custom in the setting of the addon and there you go, your own frames!]]

-- Default settings
local defaultSettings = {
    isSeason1PortalsShown = true,
    isPortalLibraryShown = true,
    isPortalLibraryRaidShown = true,
    selectedFramePack = FramePackItems[1].value -- Default frame pack
}

-- 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 FramePackItems[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
    -- Save the scale value
    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: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)
local FramePackDropdownText = FramePackDropdown:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
local font, _, flags = FramePackDropdownText:GetFont()
FramePackDropdownText:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 10, flags)
FramePackDropdownText:SetPoint("TOP", FramePackDropdown, "TOP", 0, 10)
FramePackDropdownText:SetText("Frame Pack")

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

local function InitializeDropdown(self, level, menulist)
    local info = UIDropDownMenu_CreateInfo()
    for _, option in pairs(FramePackItems) do
        info.text = option.text
        info.value = option.value
        -- No direct assignment for value2 since it now represents a UI pack
        info.func = function(self)
            -- Update the local and saved variables for both value and value2 (now UIPack)
            selectedFramePackValue = option.value
            selectedUIPack = option.value2

            MDungeonTeleports.selectedFramePack = selectedFramePackValue
            MDungeonTeleports.selectedUIPack = selectedUIPack

            -- Set the dropdown selection to reflect the chosen frame pack
            UIDropDownMenu_SetSelectedValue(FramePackDropdown, selectedFramePackValue)

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

            -- Call functions to update relevant UI backgrounds and elements
            if UpdateSeason1Background then
                UpdateSeason1Background()
            end
            if UpdatePortalLibraryBackground then
                UpdatePortalLibraryBackground()
            end
            if UpdatePortalLibRaidBackground then
                UpdatePortalLibRaidBackground()
            end
            if UpdateInfoPanelBackground then
                UpdateInfoPanelBackground()
            end
            if UpdatePortalHelperBackground then
                UpdatePortalHelperBackground()
            end
            if UpdateSeason1UI then
                UpdateSeason1UI()
            end
            if UpdatePortalLibraryUI then
                UpdatePortalLibraryUI()
            end
            if UpdatePortalLibraryRaidUI then
                UpdatePortalLibraryRaidUI()
            end
            if UpdateSettingsUI then
                UpdateSettingsUI()
            end
            if UpdatePortalHelperUI then
                UpdatePortalHelperUI()
            end

            -- Save settings to persist these selections
            SaveSettings()
        end
        -- Check which option is selected based on value
        info.checked = (option.value == selectedFramePackValue)
        UIDropDownMenu_AddButton(info)
    end
end

UIDropDownMenu_Initialize(FramePackDropdown, InitializeDropdown)
UIDropDownMenu_SetWidth(FramePackDropdown, 120)
UIDropDownMenu_SetButtonWidth(FramePackDropdown, 124)
UIDropDownMenu_SetSelectedValue(FramePackDropdown, selectedFramePackValue)
UIDropDownMenu_JustifyText(FramePackDropdown, "LEFT")

-- 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 = 800, y = 100}
        MDungeonTeleports.frameScale = 1
        MDungeonTeleports.isPortalLibraryRaidShown = true
        MDungeonTeleports.isSeason1PortalsShown = true
        MDungeonTeleports.isPortalLibraryShown = true
        MDungeonTeleports.showTooltips = false
        MDungeonTeleports.firstLaunch = false
        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
)

-- portal helper button
local portalHelperButton = CreateFrame("Button", "SettingsOpenButton", Settings)
portalHelperButton:SetSize(14, 14)
portalHelperButton:SetPoint("BOTTOMRIGHT", Settings, "BOTTOMRIGHT", -7, 5)
portalHelperButton:SetFrameStrata("DIALOG")

local portalHelperButtonTexture = portalHelperButton:CreateTexture(nil, "BACKGROUND")
portalHelperButtonTexture:SetAllPoints(portalHelperButton)
portalHelperButtonTexture:SetTexture("Interface\\AddOns\\MDungeonTeleports\\media\\ui\\button_portalhelper.blp")

local portalHelperButtonHoverOverlay = portalHelperButton:CreateTexture(nil, "HIGHLIGHT")
portalHelperButtonHoverOverlay:SetAllPoints(portalHelperButton)
portalHelperButtonHoverOverlay:SetTexture(
    "Interface\\AddOns\\MDungeonTeleports\\media\\ui\\button_portalhelper_highlight.blp"
)
portalHelperButtonHoverOverlay:Hide()

portalHelperButton.portalHelperButtonHoverOverlay = portalHelperButtonHoverOverlay
portalHelperButton.portalHelperButtonTexture = portalHelperButtonTexture

portalHelperButton:SetScript(
    "OnEnter",
    function(self)
        self.portalHelperButtonHoverOverlay:Show()
        self.portalHelperButtonTexture:Hide()
        GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
        GameTooltip:SetText("Portal Helper", 1, 1, 1)
        GameTooltip:AddLine("Find information on portals and where they were aquired from.", nil, nil, nil, true)
        GameTooltip:Show()
    end
)

portalHelperButton:SetScript(
    "OnLeave",
    function(self)
        self.portalHelperButtonHoverOverlay:Hide()
        self.portalHelperButtonTexture:Show()
        GameTooltip:Hide()
    end
)

portalHelperButton:SetScript(
    "OnClick",
    function()
        if PortalHelper:IsShown() then
            PortalHelper:Hide()
            Settings:Show()
        else
            PortalHelper:Show()
            Settings:Hide()
        end
    end
)

-- info button
local infoButton = CreateFrame("Button", "SettingsOpenButton", Settings)
infoButton:SetSize(14, 14)
infoButton:SetPoint("BOTTOMRIGHT", Settings, "BOTTOMRIGHT", -26, 5)
infoButton:SetFrameStrata("DIALOG")

local infoButtonTexture = infoButton:CreateTexture(nil, "BACKGROUND")
infoButtonTexture:SetAllPoints(infoButton)
infoButtonTexture:SetTexture("Interface\\AddOns\\MDungeonTeleports\\media\\ui\\button_info.blp")

local infoButtonHighlight = infoButton:CreateTexture(nil, "HIGHLIGHT")
infoButtonHighlight:SetAllPoints(infoButton)
infoButtonHighlight:SetTexture("Interface\\AddOns\\MDungeonTeleports\\media\\ui\\button_info_highlight.blp")
infoButtonHighlight:Hide()

infoButton.infoButtonHighlight = infoButtonHighlight
infoButton.infoButtonTexture = infoButtonTexture

infoButton:SetScript(
    "OnEnter",
    function(self)
        self.infoButtonHighlight:Show()
        self.infoButtonTexture:Hide()
        GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
        GameTooltip:SetText("Info", 1, 1, 1)
        GameTooltip:AddLine("Check addon information.", nil, nil, nil, true)
        GameTooltip:Show()
    end
)

infoButton:SetScript(
    "OnLeave",
    function(self)
        self.infoButtonHighlight:Hide()
        self.infoButtonTexture:Show()
        GameTooltip:Hide()
    end
)
infoButton:SetScript(
    "OnClick",
    function()
        if infoPanel:IsShown() then
            infoPanel:Hide()
            Settings:Show()
        else
            infoPanel:Show()
            Settings:Hide()
        end
    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\\button_settings.blp")

local settingsButtonHoverOverlay = settingsOpenButton:CreateTexture(nil, "HIGHLIGHT")
settingsButtonHoverOverlay:SetAllPoints(settingsOpenButton)
settingsButtonHoverOverlay:SetTexture("Interface\\AddOns\\MDungeonTeleports\\media\\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
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 FramePackItems[1].value
        local savedScale = MDungeonTeleports.frameScale or 1.0
        UIDropDownMenu_SetSelectedValue(FramePackDropdown, selectedFramePackValue)
        uiScaleSlider:SetValue(savedScale)
        UpdateFrameScale(savedScale)
        UpdateUI()
        UpdateSettingsUI()
        UpdateInfoPanelBackground()
        UpdatePortalHelperBackground()
        UpdatePortalHelperUI()
    end
end

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

-- infoPanelBackground
local infoPanelBackground = infoPanel:CreateTexture(nil, "BACKGROUND")
infoPanelBackground:SetAllPoints(infoPanel)
infoPanelBackground:SetTexture(MDungeonTeleports.selectedFramePack)
infoPanelBackground:SetTexCoord(0.5, 0.802734, 0.185556, 0.371093)

function UpdateInfoPanelBackground()
    local selectedTexture =
        MDungeonTeleports.selectedFramePack or "Interface/Addons/MDungeonTeleports/media/frames/default_frames.blp"
    infoPanelBackground:SetTexture(selectedTexture)
end

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

-- Scroll Frame
local scrollFrame = CreateFrame("ScrollFrame", nil, infoPanel, "UIPanelScrollFrameTemplate")
scrollFrame:SetPoint("TOPLEFT", infoPanel, "TOPLEFT", 10, -25)
scrollFrame:SetPoint("BOTTOMRIGHT", infoPanel, "BOTTOMRIGHT", -30, 40)

-- Scroll Child Frame
local scrollChild = CreateFrame("Frame", nil, scrollFrame)
scrollChild:SetSize(300, 1000)
scrollFrame:SetScrollChild(scrollChild)

-- content
local contentBackground = scrollChild:CreateTexture(nil, "BACKGROUND")
contentBackground:SetAllPoints(scrollChild)
contentBackground:SetColorTexture(0.1, 0.1, 0.1, 0.5)

-- text
local portalsText = scrollChild:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
portalsText:SetPoint("TOPLEFT", scrollChild, "TOPLEFT", 10, -10)
portalsText:SetWidth(260)
local font, _, flags = portalsText:GetFont()
portalsText:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 10, flags)
portalsText:SetJustifyH("LEFT")
portalsText:SetText(patchNotes)

-- logo and info
local studyLogo = infoPanel:CreateTexture(nil, "ARTWORK")
studyLogo:SetSize(32, 32)
studyLogo:SetPoint("BOTTOMLEFT", infoPanel, "BOTTOMLEFT", 5, 5)
studyLogo:SetTexture("Interface\\AddOns\\MDungeonTeleports\\media\\peepostudy.blp")

local studyText = infoPanel:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
studyText:SetPoint("CENTER", infoPanel, "CENTER", 0, -75)
local font, _, flags = studyText:GetFont()
studyText:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 8, flags)
studyText:SetTextColor(0.5, 0.5, 0.5)
studyText:SetText("peepoStudy's M+ Dungeon Teleports")

local addonLogo = infoPanel:CreateTexture(nil, "ARTWORK")
addonLogo:SetSize(32, 32)
addonLogo:SetPoint("BOTTOMRIGHT", infoPanel, "BOTTOMRIGHT", -5, 5)
addonLogo:SetTexture("Interface\\AddOns\\MDungeonTeleports\\media\\icon.blp")

-- title
local versionNo = infoPanel:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
versionNo:SetPoint("BOTTOM", infoPanel, "BOTTOM", 0, 3)
local font, _, flags = versionNo:GetFont()
versionNo:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 8, flags)
versionNo:SetTextColor(0.5, 0.5, 0.5)
versionNo:SetText(version)

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

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

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

infoExitButton.exitButtonHighlight = exitButtonHighlight
infoExitButton.infoExitButtonTexture = infoExitButtonTexture

infoExitButton:SetScript(
    "OnEnter",
    function(self)
        self.exitButtonHighlight:Show()
        self.infoExitButtonTexture:Hide()
    end
)

infoExitButton:SetScript(
    "OnLeave",
    function(self)
        self.exitButtonHighlight:Hide()
        self.infoExitButtonTexture:Show()
    end
)
infoExitButton:SetScript(
    "OnClick",
    function()
        if infoPanel:IsShown() then
            infoPanel:Hide()
        else
            infoPanel:Show()
        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: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
    portalHelperButtonTexture:SetTexture(UIPack .. "button_portalhelper.blp")
    portalHelperButtonHoverOverlay:SetTexture(UIPack .. "button_portalhelper_highlight.blp")
    -- info helper button
    infoButtonTexture:SetTexture(UIPack .. "button_info.blp")
    infoButtonHighlight:SetTexture(UIPack .. "button_info_highlight.blp")
    -- open settings pve button
    settingsOpenButtonTexture:SetTexture(UIPack .. "button_settings.blp")
    settingsButtonHoverOverlay:SetTexture(UIPack .. "button_settings_highlight.blp")
    -- exit button for info panel
    infoExitButtonTexture:SetTexture(UIPack .. "button_close.blp")
    exitButtonHighlight:SetTexture(UIPack .. "button_close_highlight.blp")
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()
