-- Start Up
if not MDungeonTeleports then
   MDungeonTeleports = {}
end

if not MDungeonTeleports.PortalLibraryRaidPos then
   MDungeonTeleports.PortalLibraryRaidPos = {x = 970, y = -195}
end

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

-- Save Function
local function SavePortalLibraryRaidPosition()
   local DungeonFrame = PVEFrame
   if DungeonFrame then
      local frameScale = PortalLibraryRaid:GetScale()
      local parentScale = DungeonFrame:GetScale()
      
      local PortalLibraryRaidRight = PortalLibraryRaid:GetRight() * frameScale
      local PortalLibraryRaidBottom = PortalLibraryRaid:GetBottom() * frameScale
      local dungeonRight = DungeonFrame:GetRight() * parentScale
      local dungeonBottom = DungeonFrame:GetBottom() * parentScale
      
      local offsetX = (PortalLibraryRaidRight - dungeonRight) / parentScale
      local offsetY = (PortalLibraryRaidBottom - dungeonBottom) / parentScale
      
      MDungeonTeleports.PortalLibraryRaidPos = {x = offsetX, y = offsetY}
   end
end

-- Load Function
local function LoadPortalLibraryRaidPosition()
   local DungeonFrame = PVEFrame
   if DungeonFrame and MDungeonTeleports.PortalLibraryRaidPos then
      local pos = MDungeonTeleports.PortalLibraryRaidPos
      
      local frameScale = PortalLibraryRaid:GetScale()
      local parentScale = DungeonFrame:GetScale()
      
      local adjustedX = pos.x * parentScale / frameScale
      local adjustedY = pos.y * parentScale / frameScale
      
      PortalLibraryRaid:ClearAllPoints()
      PortalLibraryRaid:SetPoint("BOTTOMRIGHT", DungeonFrame, "BOTTOMRIGHT", adjustedX, adjustedY)
   end
end

-- Reattach Function
local function ReattachPortalLibraryRaid()
   local DungeonFrame = PVEFrame
   if DungeonFrame and MDungeonTeleports.PortalLibraryRaidPos then
      local pos = MDungeonTeleports.PortalLibraryRaidPos
      
      local frameScale = PortalLibraryRaid:GetScale()
      local parentScale = DungeonFrame:GetScale()
      
      local adjustedX = pos.x * parentScale / frameScale
      local adjustedY = pos.y * parentScale / frameScale
      
      PortalLibraryRaid:ClearAllPoints()
      PortalLibraryRaid:SetPoint("BOTTOMRIGHT", DungeonFrame, "BOTTOMRIGHT", adjustedX, adjustedY)
   end
end

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

-- MARK: Login Start Up
local function OnPlayerLogin()
   UpdatePortalLibRaidBackground()
   LoadPortalLibraryRaidPosition()
   UpdatePortalLibraryRaidUI()
   CompactModePortalLibraryRaid()
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 PortalLibraryRaid_UpdateVisibility(show)
   local portalLibraryRaidFrame = _G["PortalLibraryRaid"]
   if portalLibraryRaidFrame then
      portalLibraryRaidFrame:SetShown(show)
   end
end

-- MARK: Portal Library Raid Frame
local PortalLibraryRaid = CreateFrame("Frame", "PortalLibraryRaid", PVEFrame)
PortalLibraryRaid:SetSize(348, 90)
PortalLibraryRaid:SetPoint("CENTER", PVEFrame, "CENTER", 860, -270)
PortalLibraryRaid:SetMovable(true)
PortalLibraryRaid:EnableMouse(true)
PortalLibraryRaid:RegisterForDrag("LeftButton")
PortalLibraryRaid:SetClampedToScreen(true)

PortalLibraryRaid.isBeingDragged = false

PortalLibraryRaid:SetScript(
   "OnDragStart",
   function(self)
      if IsShiftKeyDown() then
         self.isBeingDragged = true
         self:StartMoving()
      end
   end
)

PortalLibraryRaid:SetScript(
   "OnDragStop",
   function(self)
      if self.isBeingDragged then
         self:StopMovingOrSizing()
         SavePortalLibraryRaidPosition()
         self.isBeingDragged = false
         ReattachPortalLibraryRaid()
      end
   end
)

-- Portal Library Raid Background
local backgroundPortalLibRaid = PortalLibraryRaid:CreateTexture(nil, "Background")
backgroundPortalLibRaid:SetAllPoints(PortalLibraryRaid)
backgroundPortalLibRaid:SetTexture("Interface/AddOns/MDungeonTeleports/media/framepacks/frames/default_frames.blp")
backgroundPortalLibRaid:SetTexCoord(0.5, 0.839843, 0, 0.087890)
backgroundPortalLibRaid:SetVertexColor(1, 1, 1, 1)

-- MARK: Update Background Function
function UpdatePortalLibRaidBackground()
   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
   
   backgroundPortalLibRaid:SetTexture(selectedTexture)
end

-- Portal Library Raid Title
local PortalLibraryRaidTitle = PortalLibraryRaid:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
PortalLibraryRaidTitle:SetPoint("TOP", PortalLibraryRaid, "TOP", 0, -10)
PortalLibraryRaidTitle:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 13, "OUTLINE")
PortalLibraryRaidTitle:SetText("Portal Library [Raid]")

-- Shadowlands Title
local titleShadowlands = PortalLibraryRaid:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
titleShadowlands:SetPoint("TOP", PortalLibraryRaid, "TOPLEFT", 75, -27)
local font, _, flags = titleShadowlands:GetFont()
titleShadowlands:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 11, "OUTLINE")
titleShadowlands:SetTextColor(0.2, 0.8, 0.9)
titleShadowlands:SetText("Shadowlands")

-- Dragonflight Title
local titleDragonflight = PortalLibraryRaid:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
titleDragonflight:SetPoint("TOP", PortalLibraryRaid, "TOPRIGHT", -137, -27)
local font, _, flags = titleDragonflight:GetFont()
titleDragonflight:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 11, "OUTLINE")
titleDragonflight:SetTextColor(0.2, 0.8, 0.9)
titleDragonflight:SetText("Dragonflight")

-- The War Within Title
local titleTheWarWithin = PortalLibraryRaid:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
titleTheWarWithin:SetPoint("TOP", PortalLibraryRaid, "TOPRIGHT", -42, -27)
local font, _, flags = titleTheWarWithin:GetFont()
titleTheWarWithin:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 11, "OUTLINE")
titleTheWarWithin:SetTextColor(0.2, 0.8, 0.9)
titleTheWarWithin:SetText("The War Within")

-- 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())
         
         -- Hide text and overlay if remaining time is 5 seconds or less
         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, horizontalOffset)
   local button = CreateFrame("Button", nil, parent, "SecureActionButtonTemplate")
   button:SetSize(buttonSize, buttonSize)
   button:SetPoint(
      "TOPLEFT",
      parent,
      "TOPLEFT",
      (data.index - 1) * (buttonSize + buttonSpacing) + 10 + (horizontalOffset or 0),
      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("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 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("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 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 UpdatePortalLibraryRaidUI()
   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 row1HorizontalOffset = 12
local row2Offset = -40
local row2HorizontalOffset = 147
local row3Offset = -40
local row3HorizontalOffset = 281

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

for i, data in ipairs(MDT_PLR_DF) do
   data.index = i
   data.row = 2
   CreateSpellButton(PortalLibraryRaid, data, row2Offset, row2HorizontalOffset)
end

for i, data in ipairs(MDT_PLR_TWW) do
   data.index = i
   data.row = 3
   CreateSpellButton(PortalLibraryRaid, data, row3Offset, row3HorizontalOffset)
end

-- Compact Mode Start
function CompactModePortalLibraryRaid()
   if MDungeonTeleports.CompactMode == true then
      CompactModePortalLibraryRaidOn()
   else
      CompactModePortalLibraryRaidOff()
   end
end

-- MARK: Compact Mode Functions
function CompactModePortalLibraryRaidOn()
   PortalLibraryRaid:SetSize(314, 64)
   backgroundPortalLibRaid:SetTexCoord(0.5, 0.839843, 0, 0.087890)
   PortalLibraryRaidTitle:SetPoint("TOP", PortalLibraryRaid, "TOP", 0, -4)
   PortalLibraryRaidTitle:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 11, "OUTLINE")
   
   titleShadowlands:SetPoint("TOP", PortalLibraryRaid, "TOPLEFT", 75, -15)
   titleDragonflight:SetPoint("TOP", PortalLibraryRaid, "TOPRIGHT", -120, -15)
   titleTheWarWithin:SetPoint("TOP", PortalLibraryRaid, "TOPRIGHT", -40, -15)
   
   titleShadowlands:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 10, "OUTLINE")
   titleDragonflight:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 10, "OUTLINE")
   titleTheWarWithin:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 10, "OUTLINE")
   
   local buttonSpacing = 2
   local compactRowOffset = -27
   local row1HorizontalOffset = 24
   local row2HorizontalOffset = 142
   local row3HorizontalOffset = 258
   
   for _, button in ipairs(spellButtons) do
      local data = button.data
      local isRow2 = (data.row == 2)
      local isRow3 = (data.row == 3)
      local rowOffset = compactRowOffset
      local horizontalOffset = isRow3 and row3HorizontalOffset or (isRow2 and row2HorizontalOffset or row1HorizontalOffset)
      local colOffset = (data.index - 1) * (buttonSize + buttonSpacing) + horizontalOffset
      
      -- Move button
      button:SetPoint("TOPLEFT", button:GetParent(), "TOPLEFT", colOffset, rowOffset)
      
      -- Adjust button text
      local buttonText = button.buttonText
      if buttonText then
         buttonText:ClearAllPoints()
         buttonText:SetPoint("TOP", button, "BOTTOM", 1, 12)
         buttonText:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 10, "OUTLINE")
      end
      local cooldownText = button.cooldownText
      if cooldownText then
         cooldownText:ClearAllPoints()
         cooldownText:SetPoint("CENTER", button, "CENTER", 0, 4)
         cooldownText:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 12, "OUTLINE")
      end
   end
end

function CompactModePortalLibraryRaidOff()
   PortalLibraryRaid:SetSize(348, 90)
   backgroundPortalLibRaid:SetTexCoord(0.5, 0.839843, 0, 0.087890)
   PortalLibraryRaidTitle:SetPoint("TOP", PortalLibraryRaid, "TOP", 0, -10)
   PortalLibraryRaidTitle:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 13, "OUTLINE")
   
   titleShadowlands:SetPoint("TOP", PortalLibraryRaid, "TOPLEFT", 75, -27)
   titleDragonflight:SetPoint("TOP", PortalLibraryRaid, "TOPRIGHT", -137, -27)
   titleTheWarWithin:SetPoint("TOP", PortalLibraryRaid, "TOPRIGHT", -42, -27)
   
   titleShadowlands:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 11, "OUTLINE")
   titleDragonflight:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 11, "OUTLINE")
   titleTheWarWithin:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 11, "OUTLINE")
   
   local buttonSpacing = 5
   local normalRowOffset = -40
   local row1HorizontalOffset = 22
   local row2HorizontalOffset = 157
   local row3HorizontalOffset = 291
   
   for _, button in ipairs(spellButtons) do
      local data = button.data
      local isRow2 = (data.row == 2)
      local isRow3 = (data.row == 3)
      local rowOffset = normalRowOffset
      local horizontalOffset = isRow3 and row3HorizontalOffset or (isRow2 and row2HorizontalOffset or row1HorizontalOffset)
      local colOffset = (data.index - 1) * (buttonSize + buttonSpacing) + horizontalOffset
      
      -- Move button
      button:SetPoint("TOPLEFT", button:GetParent(), "TOPLEFT", colOffset, rowOffset)
      
      -- Adjust button text
      local buttonText = button.buttonText
      if buttonText then
         buttonText:ClearAllPoints()
         buttonText:SetPoint("TOP", button, "BOTTOM", 1, -2)
         buttonText:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 11, "OUTLINE")
      end
      local cooldownText = button.cooldownText
      if cooldownText then
         cooldownText:ClearAllPoints()
         cooldownText:SetPoint("CENTER", button, "CENTER", 1, 0)
         cooldownText:SetFont("Interface/AddOns/MDungeonTeleports/media/fonts/expressway.ttf", 14, "OUTLINE")
      end
   end
end

UpdatePortalLibraryRaidUI()