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

if not MDungeonTeleports then
    MDungeonTeleports = {}
end

if not MDungeonTeleports.PortalLibraryPos then
    MDungeonTeleports.PortalLibraryPos = {x = 970, y = -100}
end

if MDungeonTeleports.showTooltips == nil then
    MDungeonTeleports.showTooltips = false
end

if MDungeonTeleports.ShiftDragToggle == nil then
    MDungeonTeleports.ShiftDragToggle = false
end

-- Save Function
local function SavePortalLibraryPosition()
    local DungeonFrame = PVEFrame
    if DungeonFrame then
        local frameScale = PortalLibrary:GetEffectiveScale()
        local parentScale = DungeonFrame:GetEffectiveScale()

        local PortalLibraryRight = PortalLibrary:GetRight() * frameScale
        local PortalLibraryBottom = PortalLibrary:GetBottom() * frameScale
        local dungeonRight = DungeonFrame:GetRight() * parentScale
        local dungeonBottom = DungeonFrame:GetBottom() * parentScale

        local offsetX = (PortalLibraryRight - dungeonRight) / parentScale
        local offsetY = (PortalLibraryBottom - dungeonBottom) / parentScale

        MDungeonTeleports.PortalLibraryPos = {x = offsetX, y = offsetY}
    end
end

-- Load Function
local function LoadPortalLibraryPosition()
    local VarVersionPL = 1
    if MDungeonTeleports.VarVersionPL == nil or MDungeonTeleports.VarVersionPL < VarVersionPL then
        MDungeonTeleports.VarVersionPL = VarVersionPL
        MDungeonTeleports.PortalLibraryPos = {x = 970, y = -100}
    end
    local DungeonFrame = PVEFrame
    if DungeonFrame and MDungeonTeleports.PortalLibraryPos then
        local pos = MDungeonTeleports.PortalLibraryPos

        local frameScale = PortalLibrary:GetEffectiveScale()
        local parentScale = DungeonFrame:GetEffectiveScale()

        local adjustedX = pos.x * parentScale / frameScale
        local adjustedY = pos.y * parentScale / frameScale

        PortalLibrary:ClearAllPoints()
        PortalLibrary:SetPoint("BOTTOMRIGHT", DungeonFrame, "BOTTOMRIGHT", adjustedX, adjustedY)
    end
end

-- Reattatch Function
local function ReattatchPortalLibrary()
    local DungeonFrame = PVEFrame
    if DungeonFrame and MDungeonTeleports.PortalLibraryPos then
        local pos = MDungeonTeleports.PortalLibraryPos

        local frameScale = PortalLibrary:GetEffectiveScale()
        local parentScale = DungeonFrame:GetEffectiveScale()

        local adjustedX = pos.x * parentScale / frameScale
        local adjustedY = pos.y * parentScale / frameScale

        PortalLibrary:ClearAllPoints()
        PortalLibrary:SetPoint("BOTTOMRIGHT", DungeonFrame, "BOTTOMRIGHT", adjustedX, adjustedY)
    end
end

-- MARK: Save Function on Close
local function OnPVEFrameClose()
    SavePortalLibraryPosition()
end

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

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

-- MARK: Login Start Up
local function OnPlayerLogin()
    LocaleFontCheck()
    UpdatePortalLibraryBackground()
    LoadPortalLibraryPosition()
    UpdatePortalLibraryUI()
    CompactModePortalLibrary()
    ShiftDragToggleDL()
end

local loginFrame = CreateFrame("Frame")
loginFrame:RegisterEvent("PLAYER_LOGIN")
loginFrame:SetScript(
    "OnEvent",
    function(self, event)
        if event == "PLAYER_LOGIN" then
            OnPlayerLogin()
        end
    end
)

-- MARK: Tooltip Toggle
function ToggleTooltips()
    MDungeonTeleports.showTooltips = not MDungeonTeleports.showTooltips
end

-- MARK: Visibility Toggle
function PortalLibrary_UpdateVisibility(show)
    local portalLibraryFrame = _G["PortalLibrary"]
    if portalLibraryFrame then
        portalLibraryFrame:SetShown(show)
    end
end

-- MARK: Portal Library Frame
local PortalLibrary = CreateFrame("Frame", "PortalLibrary", PVEFrame)
PortalLibrary:SetSize(384, 510)
PortalLibrary:SetPoint("CENTER", PVEFrame, "CENTER", 900, 0)
PortalLibrary:SetMovable(true)
PortalLibrary:EnableMouse(true)
PortalLibrary:RegisterForDrag("LeftButton")
PortalLibrary:SetClampedToScreen(true)

PortalLibrary:SetScript(
    "OnDragStart",
    function(self)
        self.isBeingDragged = true
        self:StartMoving()
    end
)

PortalLibrary:SetScript(
    "OnDragStop",
    function(self)
        if self.isBeingDragged then
            self:StopMovingOrSizing()
            SavePortalLibraryPosition()
            self.isBeingDragged = false
            ReattatchPortalLibrary()
        end
    end
)

-- Portal Library Background
local backgroundPortalLib = PortalLibrary:CreateTexture(nil, "BACKGROUND")
backgroundPortalLib:SetAllPoints(PortalLibrary)
backgroundPortalLib:SetTexture("Interface/AddOns/MDungeonTeleports/media/framepacks/frames/default_frames.blp")
backgroundPortalLib:SetTexCoord(0.160156, 0.5, 0, 0.493164)
backgroundPortalLib:SetVertexColor(1, 1, 1, 1)

-- MARK: Update Background Function
function UpdatePortalLibraryBackground()
    local selectedTexture

    if MDungeonTeleports.CompactMode then
        selectedTexture = "Interface/Addons/MDungeonTeleports/media/framepacks/frames/borderless_frames.blp"
    else
        selectedTexture =
            MDungeonTeleports.selectedFramePack or
            "Interface/Addons/MDungeonTeleports/media/framepacks/frames/default_frames.blp"
    end

    backgroundPortalLib:SetTexture(selectedTexture)
end

-- Portal Library Title
local PortalLibraryTitle = PortalLibrary:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
PortalLibraryTitle:SetPoint("TOP", PortalLibrary, "TOP", 0, -10)
PortalLibraryTitle:SetFont(UserFont, 13, "OUTLINE")
PortalLibraryTitle:SetText(L["DUNGEON_LIBRARY"])

-- MARK: Expansion Titles Setup
local titleTWW = PortalLibrary:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
titleTWW:SetPoint("TOP", PortalLibrary, "TOP", 0, -26)
local font, _, flags = titleTWW:GetFont()
titleTWW:SetFont(UserFont, 11, "OUTLINE")
titleTWW:SetTextColor(0.2, 0.8, 0.9)
titleTWW:SetText(L["EXP_TWW"])

local titleDF = PortalLibrary:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
titleDF:SetPoint("TOP", PortalLibrary, "TOP", 0, -86)
local font, _, flags = titleDF:GetFont()
titleDF:SetFont(UserFont, 11, "OUTLINE")
titleDF:SetTextColor(0.2, 0.8, 0.9)
titleDF:SetText(L["EXP_DF"])

local titleSL = PortalLibrary:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
titleSL:SetPoint("TOP", PortalLibrary, "TOP", 0, -146)
local font, _, flags = titleSL:GetFont()
titleSL:SetFont(UserFont, 11, "OUTLINE")
titleSL:SetTextColor(0.2, 0.8, 0.9)
titleSL:SetText(L["EXP_SL"])

local titleBFA = PortalLibrary:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
titleBFA:SetPoint("TOP", PortalLibrary, "TOP", 0, -206)
local font, _, flags = titleBFA:GetFont()
titleBFA:SetFont(UserFont, 11, "OUTLINE")
titleBFA:SetTextColor(0.2, 0.8, 0.9)
titleBFA:SetText(L["EXP_BFA"])

local titleLeg = PortalLibrary:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
titleLeg:SetPoint("TOP", PortalLibrary, "TOP", 0, -266)
local font, _, flags = titleLeg:GetFont()
titleLeg:SetFont(UserFont, 11, "OUTLINE")
titleLeg:SetTextColor(0.2, 0.8, 0.9)
titleLeg:SetText(L["EXP_LEG"])

local titleWOD = PortalLibrary:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
titleWOD:SetPoint("TOP", PortalLibrary, "TOP", 0, -326)
local font, _, flags = titleWOD:GetFont()
titleWOD:SetFont(UserFont, 11, "OUTLINE")
titleWOD:SetTextColor(0.2, 0.8, 0.9)
titleWOD:SetText(L["EXP_WOD"])

local titleMOP = PortalLibrary:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
titleMOP:SetPoint("TOP", PortalLibrary, "TOP", 0, -386)
local font, _, flags = titleMOP:GetFont()
titleMOP:SetFont(UserFont, 11, "OUTLINE")
titleMOP:SetTextColor(0.2, 0.8, 0.9)
titleMOP:SetText(L["EXP_MOP"] )

local titleCATA = PortalLibrary:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
titleCATA:SetPoint("TOP", PortalLibrary, "TOP", 0, -446)
local font, _, flags = titleSL:GetFont()
titleCATA:SetFont(UserFont, 11, "OUTLINE")
titleCATA:SetTextColor(0.2, 0.8, 0.9)
titleCATA:SetText(L["EXP_CATA"])

-- MARK: Time Formatting
local function FormatCooldownTime(seconds)
    if seconds >= 3600 then
        local hours = math.floor(seconds / 3600)
        return string.format("%dh", hours)
    elseif seconds >= 60 then
        local minutes = math.floor(seconds / 60)
        return string.format("%dm", minutes)
    else
        return string.format("%ds", math.floor(seconds))
    end
end

-- -MARK: Cooldown Text Function
local function UpdateCooldownText(button, spellID)
    local cooldownFrame = button.cooldownText
    local cooldownOverlay = button.cooldownOverlay
    local spellCooldownInfo = C_Spell.GetSpellCooldown(spellID)

    if spellCooldownInfo then
        local startTime = spellCooldownInfo.startTime
        local duration = spellCooldownInfo.duration
        local isEnabled = spellCooldownInfo.isEnabled

        if startTime > 0 and duration > 0 and isEnabled then
            local remaining = max(0, (startTime + duration) - GetTime())

            if remaining > 5 then
                cooldownFrame:SetText(FormatCooldownTime(remaining))
                cooldownOverlay:Show()
            else
                cooldownFrame:SetText("")
                cooldownOverlay:Hide()
            end
        else
            cooldownFrame:SetText("")
            cooldownOverlay:Hide()
        end
    else
        cooldownFrame:SetText("")
        cooldownOverlay:Hide()
    end
end

-- MARK: Button Settings
local buttonSize = 32
local buttonSpacing = 5

local spellButtons = {}

-- MARK: Button Creation
local function CreateSpellButton(parent, data, rowOffset)
    local button = CreateFrame("Button", nil, parent, "SecureActionButtonTemplate")
    button:SetSize(buttonSize, buttonSize)
    button:SetPoint("TOPLEFT", parent, "TOPLEFT", (data.index - 1) * (buttonSize + buttonSpacing) + 10, rowOffset)

    button.data = data

    -- Texture for the button
    local buttonTexture = button:CreateTexture(nil, "ARTWORK")
    buttonTexture:SetAllPoints(button)
    buttonTexture:SetTexture(data.icon)

    -- Border texture
    local borderTexture = button:CreateTexture(nil, "OVERLAY")
    borderTexture:SetAllPoints(button)
    borderTexture:SetTexture("Interface\\AddOns\\MDungeonTeleports\\media\\ui\\ui_default\\button_border.blp")
    borderTexture:SetDrawLayer("OVERLAY", 2)
    button.borderTexture = borderTexture

    -- Cooldown texture
    local cooldownOverlay = button:CreateTexture(nil, "OVERLAY")
    cooldownOverlay:SetAllPoints(button)
    cooldownOverlay:SetTexture("Interface\\Tooltips\\UI-Tooltip-Background")
    cooldownOverlay:SetVertexColor(0, 0, 0, 0.6)
    cooldownOverlay:Hide()
    button.cooldownOverlay = cooldownOverlay

    -- Button text
    local buttonText = button:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    buttonText:SetPoint("TOP", button, "BOTTOM", 1, -2)
    local font, _, flags = buttonText:GetFont()
    buttonText:SetFont(UserFont, 11, "OUTLINE")
    buttonText:SetTextColor(1, 1, 1)
    buttonText:SetText(data.name)
    button.buttonText = buttonText

    -- Cooldown text
    local cooldownText = button:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
    cooldownText:SetPoint("CENTER", button, "CENTER", 1, 0)
    cooldownText:SetTextColor(1, 1, 1)
    cooldownText:SetFont(UserFont, 14, "OUTLINE")
    button.cooldownText = cooldownText

    -- Spell ID
    button:SetAttribute("type", "spell")
    button:SetAttribute("spell", data.spellID)

    -- Hover overlay
    local hoverOverlay = button:CreateTexture(nil, "HIGHLIGHT")
    hoverOverlay:SetAllPoints(button)
    hoverOverlay:SetTexture("Interface\\AddOns\\MDungeonTeleports\\media\\ui\\ui_default\\button_highlight.blp")
    hoverOverlay:Hide()
    button.hoverOverlay = hoverOverlay

    table.insert(spellButtons, button)

    -- Button texture update function
    local function UpdateButtonTexture()
        local UIPack

        if MDungeonTeleports.CompactMode then
            UIPack = "Interface/Addons/MDungeonTeleports/media/ui/ui_default/"
        else
            UIPack = MDungeonTeleports.selectedUIPack or "Interface/Addons/MDungeonTeleports/media/ui/ui_default/"
        end

        if IsSpellKnown(data.spellID) then
            buttonTexture:SetTexture(data.icon)
        elseif MDungeonTeleports.AltLocks then
            if type(AltLocks) == "table" then
                for _, lock in ipairs(AltLocks) do
                    if lock.spellID == data.spellID then
                        buttonTexture:SetTexture(lock.icon)
                        buttonTexture:SetAlpha(0.6)
                        buttonTexture:SetDesaturated(true)
                        return
                    end
                end
            end
            buttonTexture:SetTexture(UIPack .. "lock.blp")
            buttonTexture:SetAlpha(1)
            buttonTexture:SetDesaturated(false)
        else
            buttonTexture:SetTexture(UIPack .. "lock.blp")
            buttonTexture:SetAlpha(1)
            buttonTexture:SetDesaturated(false)
        end
    end

    button.UpdateButtonTexture = UpdateButtonTexture

    UpdateButtonTexture()

    -- Event handler
    local function OnEvent(self, event)
        if event == "SPELL_UPDATE_COOLDOWN" or event == "SPELLS_CHANGED" then
            UpdateButtonTexture()
            UpdateCooldownText(self, data.spellID)
        end
    end

    -- Tooltips and hover
    button:SetScript(
        "OnEnter",
        function(self)
            if MDungeonTeleports.showTooltips then
                GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
                GameTooltip:SetSpellByID(data.spellID)
                GameTooltip:Show()
            end
            hoverOverlay:Show()
        end
    )

    button:SetScript(
        "OnLeave",
        function(self)
            GameTooltip:Hide()
            hoverOverlay:Hide()
        end
    )

    -- Register events
    button:RegisterEvent("SPELL_UPDATE_COOLDOWN")
    button:RegisterEvent("SPELLS_CHANGED")
    button:RegisterForClicks("AnyUp", "AnyDown")
    button:SetScript("OnEvent", OnEvent)

    return button
end

-- MARK: Update UI Function
function UpdatePortalLibraryUI()
    local UIPack

    if MDungeonTeleports.CompactMode then
        UIPack = "Interface/Addons/MDungeonTeleports/media/ui/ui_default/"
    else
        UIPack = MDungeonTeleports.selectedUIPack or "Interface/Addons/MDungeonTeleports/media/ui/ui_default/"
    end

    for _, btn in ipairs(spellButtons) do
        if btn.borderTexture then
            btn.borderTexture:SetTexture(UIPack .. "button_border.blp")
        end
        if btn.hoverOverlay then
            btn.hoverOverlay:SetTexture(UIPack .. "button_highlight.blp")
        end
        if btn.UpdateButtonTexture then
            btn.UpdateButtonTexture()
        end
    end
end

--MARK: Button Rows Setup
local rowOffset = -40
local row2Offset = -99
local row3Offset = -159
local row4Offset = -219
local row5Offset = -279
local row6Offset = -339
local row7Offset = -399
local row8Offset = -459

-- MARK: Button Rows Setup
for i, data in ipairs(MDT_PL_TWW) do
    data.index = i
    data.row = 1
    CreateSpellButton(PortalLibrary, data, rowOffset)
end

for i, data in ipairs(MDT_PL_DF) do
    data.index = i
    data.row = 2
    CreateSpellButton(PortalLibrary, data, row2Offset)
end

for i, data in ipairs(MDT_PL_SL) do
    data.index = i
    data.row = 3
    CreateSpellButton(PortalLibrary, data, row3Offset)
end

for i, data in ipairs(MDT_PL_BFA) do
    data.index = i
    data.row = 4
    CreateSpellButton(PortalLibrary, data, row4Offset)
end

for i, data in ipairs(MDT_PL_Legion) do
    data.index = i
    data.row = 5
    CreateSpellButton(PortalLibrary, data, row5Offset)
end

for i, data in ipairs(MDT_PL_WOD) do
    data.index = i
    data.row = 6
    CreateSpellButton(PortalLibrary, data, row6Offset)
end

for i, data in ipairs(MDT_PL_MOP) do
    data.index = i
    data.row = 7
    CreateSpellButton(PortalLibrary, data, row7Offset)
end

for i, data in ipairs(MDT_PL_CATA) do
    data.index = i
    data.row = 8
    CreateSpellButton(PortalLibrary, data, row8Offset)
end

-- Compact Mode Start
function CompactModePortalLibrary()
    if MDungeonTeleports.CompactMode == true then
        CompactModePortalLibraryOn()
    else
        CompactModePortalLibraryOff()
    end
end

-- Compact Mode On
function CompactModePortalLibraryOn()
    PortalLibrary:SetSize(348, 386)
    backgroundPortalLib:SetTexCoord(0.160156, 0.5, 0, 0.493164)
    PortalLibraryTitle:SetPoint("TOP", PortalLibrary, "TOP", 0, -4)
    PortalLibraryTitle:SetFont(UserFont, 11, "OUTLINE")

    titleTWW:SetPoint("TOP", PortalLibrary, "TOP", 0, -15)
    titleDF:SetPoint("TOP", PortalLibrary, "TOP", 0, -61)
    titleSL:SetPoint("TOP", PortalLibrary, "TOP", 0, -107)
    titleBFA:SetPoint("TOP", PortalLibrary, "TOP", 0, -153)
    titleLeg:SetPoint("TOP", PortalLibrary, "TOP", 0, -199)
    titleWOD:SetPoint("TOP", PortalLibrary, "TOP", 0, -245)
    titleMOP:SetPoint("TOP", PortalLibrary, "TOP", 0, -291)
    titleCATA:SetPoint("TOP", PortalLibrary, "TOP", 0, -337)

    titleTWW:SetFont(UserFont, 10, "OUTLINE")
    titleDF:SetFont(UserFont, 10, "OUTLINE")
    titleSL:SetFont(UserFont, 10, "OUTLINE")
    titleBFA:SetFont(UserFont, 10, "OUTLINE")
    titleLeg:SetFont(UserFont, 10, "OUTLINE")
    titleWOD:SetFont(UserFont, 10, "OUTLINE")
    titleMOP:SetFont(UserFont, 10, "OUTLINE")
    titleCATA:SetFont(UserFont, 10, "OUTLINE")

    local buttonSpacing = 2
    local rowOffset = -27
    local row2Offset = -73
    local row3Offset = -119
    local row4Offset = -165
    local row5Offset = -211
    local row6Offset = -257
    local row7Offset = -303
    local row8Offset = -349
    local row9Offset = -395
    local row10Offset = -441

    local numColumns = #MDT_PL_TWW

    for _, button in ipairs(spellButtons) do
        local data = button.data
        local rowOffsets = {
            [1] = rowOffset,
            [2] = row2Offset,
            [3] = row3Offset,
            [4] = row4Offset,
            [5] = row5Offset,
            [6] = row6Offset,
            [7] = row7Offset,
            [8] = row8Offset,
            [9] = row9Offset,
            [10] = row10Offset
        }

        local rowOffsetValue = rowOffsets[data.row] or rowOffset
        local colOffset = (data.index - 1) * (buttonSize + buttonSpacing) + 4

        -- Move button
        button:SetPoint("TOPLEFT", button:GetParent(), "TOPLEFT", colOffset, rowOffsetValue)

        local buttonText = button.buttonText
        if buttonText then
            buttonText:ClearAllPoints()
            buttonText:SetPoint("TOP", button, "BOTTOM", 1, 12)
            buttonText:SetFont(UserFont, 10, "OUTLINE")
        end
        local cooldownText = button.cooldownText
        if cooldownText then
            cooldownText:ClearAllPoints()
            cooldownText:SetPoint("CENTER", button, "CENTER", 0, 4)
            cooldownText:SetFont(UserFont, 12, "OUTLINE")
        end
    end
end

-- Compact Mode Off
function CompactModePortalLibraryOff()
    PortalLibrary:SetSize(384, 510)
    backgroundPortalLib:SetTexCoord(0.160156, 0.5, 0, 0.493164)
    PortalLibraryTitle:SetPoint("TOP", PortalLibrary, "TOP", 0, -10)
    PortalLibraryTitle:SetFont(UserFont, 13, "OUTLINE")

    titleTWW:SetPoint("TOP", PortalLibrary, "TOP", 0, -26)
    titleDF:SetPoint("TOP", PortalLibrary, "TOP", 0, -86)
    titleSL:SetPoint("TOP", PortalLibrary, "TOP", 0, -146)
    titleBFA:SetPoint("TOP", PortalLibrary, "TOP", 0, -206)
    titleLeg:SetPoint("TOP", PortalLibrary, "TOP", 0, -266)
    titleWOD:SetPoint("TOP", PortalLibrary, "TOP", 0, -326)
    titleMOP:SetPoint("TOP", PortalLibrary, "TOP", 0, -386)
    titleCATA:SetPoint("TOP", PortalLibrary, "TOP", 0, -446)

    titleTWW:SetFont(UserFont, 11, "OUTLINE")
    titleDF:SetFont(UserFont, 11, "OUTLINE")
    titleSL:SetFont(UserFont, 11, "OUTLINE")
    titleBFA:SetFont(UserFont, 11, "OUTLINE")
    titleLeg:SetFont(UserFont, 11, "OUTLINE")
    titleWOD:SetFont(UserFont, 11, "OUTLINE")
    titleMOP:SetFont(UserFont, 11, "OUTLINE")
    titleCATA:SetFont(UserFont, 11, "OUTLINE")

    local buttonSpacing = 5
    local rowOffset = -40
    local row2Offset = -99
    local row3Offset = -159
    local row4Offset = -219
    local row5Offset = -279
    local row6Offset = -339
    local row7Offset = -399
    local row8Offset = -459
    local row9Offset = -519
    local row10Offset = -579

    local numColumns = #MDT_PL_TWW

    for _, button in ipairs(spellButtons) do
        local data = button.data
        local rowOffsets = {
            [1] = rowOffset,
            [2] = row2Offset,
            [3] = row3Offset,
            [4] = row4Offset,
            [5] = row5Offset,
            [6] = row6Offset,
            [7] = row7Offset,
            [8] = row8Offset,
            [9] = row9Offset,
            [10] = row10Offset
        }

        local rowOffsetValue = rowOffsets[data.row] or rowOffset
        local colOffset = (data.index - 1) * (buttonSize + buttonSpacing) + 10

        button:SetPoint("TOPLEFT", button:GetParent(), "TOPLEFT", colOffset, rowOffsetValue)

        local buttonText = button.buttonText
        if buttonText then
            buttonText:ClearAllPoints()
            buttonText:SetPoint("TOP", button, "BOTTOM", 1, -2)
            buttonText:SetFont(UserFont, 11, "OUTLINE")
        end
        local cooldownText = button.cooldownText
        if cooldownText then
            cooldownText:ClearAllPoints()
            cooldownText:SetPoint("CENTER", button, "CENTER", 1, 0)
            cooldownText:SetFont(UserFont, 14, "OUTLINE")
        end
    end
end

-- MARK: Shift Drag Toggle
function ShiftDragToggleDL()
    if MDungeonTeleports.ShiftDragToggle == true then
        PortalLibrary:SetScript("OnDragStart", function(self)
            if IsShiftKeyDown() then
                self.isBeingDragged = true
                self:StartMoving()
            end
        end)
    else
        PortalLibrary:SetScript("OnDragStart", function(self)
            self.isBeingDragged = true
            self:StartMoving()
        end)
    end
end

UpdatePortalLibraryUI()