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

if not MDungeonTeleports then
    MDungeonTeleports = {}
end

if not MDungeonTeleports.Season2Pos then
    MDungeonTeleports.Season2Pos = {x = 975, y = -152}
end

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

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

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

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

-- Global Toggle
function TrayModeToggleSeason2()
    if InCombatLockdown() then
        return
    end
    if MDungeonTeleports.TrayMode then
        TrayIconSeason2:Show()
        TrayIconBackdropSeason2:Show()
    else
        TrayIconSeason2:Hide()
        TrayIconBackdropSeason2:Hide()
        TrayModeSeason2Off()
    end
end

-- TrayMode Function
local function TrayMode()
    if MDungeonTeleports.TrayModeSeason2 == true then
        TrayModeSeason2On()
    else
        TrayModeSeason2Off()
    end
end

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

        local season2Left = Season2:GetLeft() * frameScale
        local season2Top = Season2:GetTop() * frameScale
        local dungeonLeft = DungeonFrame:GetLeft() * parentScale
        local dungeonTop = DungeonFrame:GetTop() * parentScale

        local offsetX = (season2Left - dungeonLeft) / parentScale
        local offsetY = (season2Top - dungeonTop) / parentScale

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

-- Load Function
local function LoadSeason2Position()
    local VarVersionS2 = 1
    if MDungeonTeleports.VarVersionS2 == nil or MDungeonTeleports.VarVersionS2 < VarVersionS2 then
        MDungeonTeleports.VarVersionS2 = VarVersionS2
        MDungeonTeleports.Season2Pos = {x = 975, y = -152}
    end
    local DungeonFrame = PVEFrame
    if DungeonFrame and MDungeonTeleports.Season2Pos then
        local pos = MDungeonTeleports.Season2Pos

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

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

        Season2:ClearAllPoints()
        Season2:SetPoint("TOPLEFT", DungeonFrame, "TOPLEFT", adjustedX, adjustedY)
    end
end

-- Reattach Function
local function ReattachSeason2()
    local DungeonFrame = PVEFrame
    if DungeonFrame and MDungeonTeleports.Season2Pos then
        local pos = MDungeonTeleports.Season2Pos

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

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

        Season2:ClearAllPoints()
        Season2:SetPoint("TOPLEFT", DungeonFrame, "TOPLEFT", adjustedX, adjustedY)
    end
end

-- MARK: Save Function on Close
local function OnPVEFrameClose()
    SaveSeason2Position()
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()
    LoadSeason2Position()
    UpdateSeason2Background()
    UpdateSeason2UI()
    CompactModeSeason2()
    TrayModeToggleSeason2()
    TrayMode()
    ShiftDragToggleS2()
    showTooltips = MDungeonTeleports.showTooltips
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 Season2_UpdateVisibility(show)
    local seasonFrame = _G["Season2"]
    if seasonFrame then
        seasonFrame:SetShown(show)
    end
end

-- MARK: Season 2 Frame
local Season2 = CreateFrame("Frame", "Season2", PVEFrame)
Season2:SetSize(164, 130)
Season2:SetMovable(true)
Season2:EnableMouse(true)
Season2:RegisterForDrag("LeftButton")
Season2:SetClampedToScreen(true)

Season2.isBeingDragged = false

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

Season2:SetScript(
    "OnDragStop",
    function(self)
        if self.isBeingDragged then
            self:StopMovingOrSizing()
            SaveSeason2Position()
            self.isBeingDragged = false
            ReattachSeason2()
        end
    end
)

-- Season 2 Background
local backgroundSeason2 = Season2:CreateTexture(nil, "BACKGROUND")
backgroundSeason2:SetAllPoints(Season2)
backgroundSeason2:SetTexture("Interface/AddOns/MDungeonTeleports/media/framepacks/frames/default_frames.blp")
backgroundSeason2:SetTexCoord(0, 0.160156, 0.126953, 0.253906)
backgroundSeason2:SetVertexColor(1, 1, 1, 1)

-- MARK: Update Background Function
function UpdateSeason2Background()
    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

    backgroundSeason2:SetTexture(selectedTexture)
end

-- Season 1 Title
local Season2Title = Season2:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
Season2Title:SetPoint("TOP", Season2, "TOP", 0, -10)
Season2Title:SetFont(UserFont, 13, "OUTLINE")
Season2Title:SetText(L["TITLE_S2"])

-- 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 UpdateSeason2UI()
    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 = -30
local row2Offset = -79

for i, data in ipairs(MDT_Season2Row1) do
    data.index = i
    data.row = 1
    CreateSpellButton(Season2, data, rowOffset)
end

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

-- Compact Mode Start
function TrayModeToggleSeason2()
    if InCombatLockdown() then
        return
    end
    if MDungeonTeleports.TrayMode then
        TrayButtonSeason2:Show()
    else
        TrayButtonSeason2:Hide()
        TrayModeSeason2Off()
    end
end

-- Compact Mode Start
function CompactModeSeason2()
    if MDungeonTeleports.CompactMode == true then
        CompactModeSeason2On()
    else
        CompactModeSeason2Off()
    end
end

--MARK: Tray Icon Button and Backdrop
local TrayButtonSeason2 = CreateFrame("Button", "TrayButtonSeason2", Season2)
TrayButtonSeason2:SetSize(12, 130)
TrayButtonSeason2:SetPoint("CENTER", Season2, "RIGHT", 8, 0)

local TrayButtonTextureSeason2 = TrayButtonSeason2:CreateTexture(nil, "BACKGROUND")
TrayButtonTextureSeason2:SetAllPoints(TrayButtonSeason2)
TrayButtonTextureSeason2:SetTexture("Interface/AddOns/MDungeonTeleports/media/ui/ui_elements/chevrons.blp")
TrayButtonTextureSeason2:SetTexCoord(0.929687, 1, 0, 0.761718)

local TrayButtonHighlightSeason2 = TrayButtonSeason2:CreateTexture(nil, "HIGHLIGHT")
TrayButtonHighlightSeason2:SetAllPoints(TrayButtonSeason2)
TrayButtonHighlightSeason2:SetTexture("Interface/AddOns/MDungeonTeleports/media/ui/ui_elements/chevrons.blp")
TrayButtonHighlightSeason2:SetTexCoord(0.859375, 0.929687, 0, 0.761718)
TrayButtonHighlightSeason2:Hide()

TrayButtonSeason2.TrayButtonHighlightSeason2 = TrayButtonHighlightSeason2
TrayButtonSeason2.TrayButtonTextureSeason2 = TrayButtonTextureSeason2

TrayButtonSeason2:SetScript(
    "OnEnter",
    function(self)
        TrayButtonTextureSeason2:Hide()
        TrayButtonHighlightSeason2:Show()
    end
)

TrayButtonSeason2:SetScript(
    "OnLeave",
    function(self)
        TrayButtonTextureSeason2:Show()
        TrayButtonHighlightSeason2:Hide()
    end
)

TrayButtonSeason2:SetScript(
    "OnClick",
    function(self)
        if InCombatLockdown() then
            print("Cannot use this in combat!")
            return
        end
        TrayModeSeason2On()
    end
)

-- MARK: Compact Mode On
function CompactModeSeason2On()
    if MDungeonTeleports.TrayModeSeason2 == true then
        Season2:SetSize(12, 94)
    else
        Season2:SetSize(142, 94)
    end

    if MDungeonTeleports.CompactMode == true then
        TrayButtonSeason2:SetHeight(94)
        TrayButtonTextureSeason2:SetTexCoord(0, 0.070312, 0, 0.550781)
        TrayButtonHighlightSeason2:SetTexCoord(0.070312, 0.140625, 0, 0.550781)
    else
        TrayButtonSeason2:SetHeight(130)
        TrayButtonTextureSeason2:SetTexCoord(0.929687, 1, 0, 0.761718)
        TrayButtonHighlightSeason2:SetTexCoord(0.859375, 0.929687, 0, 0.761718)
    end

    backgroundSeason2:SetTexCoord(0.1, 0.140156, 0.1, 0.106953)
    Season2Title:SetPoint("TOP", Season2, "TOP", 0, -4)
    Season2Title:SetFont(UserFont, 11, "OUTLINE")

    local buttonSpacing = 2
    local compactRowOffset = -18
    local compactRow2Offset = -57

    local numColumns = #MDT_Season2Row1

    for _, button in ipairs(spellButtons) do
        local data = button.data
        local isRow2 = (data.row == 2)
        local rowOffset = isRow2 and compactRow2Offset or compactRowOffset
        local colOffset = (data.index - 1) * (buttonSize + buttonSpacing) + 4

        -- 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(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

-- MARK: Compact Mode Off
function CompactModeSeason2Off()
    if MDungeonTeleports.TrayModeSeason2 then
        Season2:SetSize(12, 130)
    else
        Season2:SetSize(164, 130)
    end

    if MDungeonTeleports.CompactMode then
        TrayButtonSeason2:SetHeight(94)
        TrayButtonTextureSeason2:SetTexCoord(0, 0.070312, 0, 0.550781)
        TrayButtonHighlightSeason2:SetTexCoord(0.070312, 0.140625, 0, 0.550781)
    else
        TrayButtonSeason2:SetHeight(130)
        TrayButtonTextureSeason2:SetTexCoord(0.929687, 1, 0, 0.761718)
        TrayButtonHighlightSeason2:SetTexCoord(0.859375, 0.929687, 0, 0.761718)
    end

    backgroundSeason2:SetTexCoord(0, 0.160156, 0, 0.126953)
    Season2Title:SetPoint("TOP", Season2, "TOP", 0, -10)
    Season2Title:SetFont(UserFont, 13, "OUTLINE")

    local buttonSpacing = 5
    local rowOffset = -30
    local row2Offset = -79

    local numColumns = #MDT_Season2Row1

    for _, button in ipairs(spellButtons) do
        local data = button.data
        local isRow2 = (data.row == 2)
        local rowOffset = isRow2 and row2Offset or rowOffset
        local colOffset = (data.index - 1) * (buttonSize + buttonSpacing) + 10

        -- 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(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: Tray Mode On
function TrayModeSeason2On()
    MDungeonTeleports.TrayModeSeason2 = true

    Season2:SetWidth(12)

    Season2Title:Hide()

    for _, button in ipairs(spellButtons) do
        button:Hide()
    end

    TrayButtonSeason2:SetPoint("CENTER", Season2, "RIGHT", -6, 0)

    TrayButtonTextureSeason2:SetRotation(math.pi)

    TrayButtonHighlightSeason2:SetRotation(math.pi)

    TrayButtonSeason2:SetScript(
    "OnClick",
    function(self)
        if InCombatLockdown() then
            print("Cannot use this in combat!")
            return
        end
        TrayModeSeason2Off()
    end
)
end

-- MARK: Tray Mode Off
function TrayModeSeason2Off()
    MDungeonTeleports.TrayModeSeason2 = false

    local curWidthRes

    if MDungeonTeleports.CompactMode == true then
        curWidthRes = 142
    else
        curWidthRes = 164
    end

    if MDungeonTeleports.CompactMode == true then
        trayHeight = 94
    else
        trayHeight = 130
    end

    Season2Title:Show()
    Season2:SetWidth(curWidthRes)

    for _, button in ipairs(spellButtons) do
        button:Show()
    end

    TrayButtonSeason2:SetHeight(trayHeight)
    TrayButtonSeason2:SetPoint("CENTER", Season2, "RIGHT", 8, 0)

    TrayButtonTextureSeason2:SetRotation(0)

    TrayButtonHighlightSeason2:SetRotation(0)

    TrayButtonSeason2:SetScript(
    "OnClick",
    function(self)
        if InCombatLockdown() then
            print("Cannot use this in combat!")
            return
        end
        TrayModeSeason2On()
    end
)
end

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

UpdateSeason2UI()