AddSongWindow = nil

function ShowAddSongWindow()
    if AddSongWindow then
        AddSongWindow:Show()
        AddSongWindow:Raise()
		
        AddSongWindow.nameBox:SetText("")
        AddSongWindow.fileBox:SetText("")
        AddSongWindow.durBox:SetText("")
		
        return
	end
	
    local f = CreateFrame("Frame", "AddSongWindow", UIParent, "BasicFrameTemplateWithInset")
    AddSongWindow = f
	
    f:SetSize(380, 300)
    f:SetPoint("CENTER")
    f:SetMovable(true)
    f:EnableMouse(true)
    f:RegisterForDrag("LeftButton")
    f:SetScript("OnDragStart", f.StartMoving)
    f:SetScript("OnDragStop", f.StopMovingOrSizing)
    f:SetFrameStrata("DIALOG")
    f:SetFrameLevel(100)
    f:Raise()
	
    if not tContains(UISpecialFrames, "AddSongWindow") then
        table.insert(UISpecialFrames, "AddSongWindow")
	end
	
    f.title = f:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
    f.title:SetPoint("TOP", 0, -5)
    f.title:SetText("Add Custom Song")
	
    local info = f:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
    info:SetPoint("TOPLEFT", 15, -30)
    info:SetWidth(350)
    info:SetJustifyH("LEFT")
    info:SetText("To add a custom song, install the 'Pedro Bloodlust Custom' addon and place your .ogg file in: Interface/AddOns/Pedro_Custom/Media/\n\nIf you already added custom songs in the original Pedro addon, remove them first, then add them again.\n\nEnter the exact filename (e.g. MySong.ogg).\nThe duration you provide ensures the animation matches the song length.")
	
	
	
    local nameLabel = f:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    nameLabel:SetPoint("TOPLEFT", info, "BOTTOMLEFT", 0, -15)
    nameLabel:SetText("Display Name:")
	
    local nameBox = CreateFrame("EditBox", nil, f, "InputBoxTemplate")
    f.nameBox = nameBox
    nameBox:SetSize(200, 20)
    nameBox:SetPoint("LEFT", nameLabel, "RIGHT", 10, 0)
    nameBox:SetAutoFocus(false)
	
    local fileLabel = f:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    fileLabel:SetPoint("TOPLEFT", nameLabel, "BOTTOMLEFT", 0, -15)
    fileLabel:SetText("Filename:")
	
    local fileBox = CreateFrame("EditBox", nil, f, "InputBoxTemplate")
    f.fileBox = fileBox
    fileBox:SetSize(200, 20)
    fileBox:SetPoint("LEFT", fileLabel, "RIGHT", 10, 0)
    fileBox:SetAutoFocus(false)
	
    local durLabel = f:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    durLabel:SetPoint("TOPLEFT", fileLabel, "BOTTOMLEFT", 0, -15)
    durLabel:SetText("Duration (sec):")
	
    local durBox = CreateFrame("EditBox", nil, f, "InputBoxTemplate")
    f.durBox = durBox
    durBox:SetSize(50, 20)
    durBox:SetPoint("LEFT", durLabel, "RIGHT", 10, 0)
    durBox:SetAutoFocus(false)
    durBox:SetNumeric(true)
	
    nameBox:SetScript("OnTabPressed", function() fileBox:SetFocus() end)
    fileBox:SetScript("OnTabPressed", function() durBox:SetFocus() end)
    durBox:SetScript("OnTabPressed", function() nameBox:SetFocus() end)
	
    local addBtn = CreateFrame("Button", nil, f, "UIPanelButtonTemplate")
    addBtn:SetSize(100, 22)
    addBtn:SetPoint("BOTTOMLEFT", 15, 15)
    addBtn:SetText("Add Song")
	
    addBtn:SetScript("OnClick", function()
        local name = nameBox:GetText()
        local filename = fileBox:GetText()
        local dur = tonumber(durBox:GetText())
		
        if filename == "" then
            print("Pedro: Please enter a filename (e.g. MySong.ogg).")
            return
		end
		
        if not filename:lower():match("%.ogg$") then
            filename = filename .. ".ogg"
		end
		
        if not dur or dur < 1 or dur > 60 then
			print("Pedro: Duration must be between 1 and 60 seconds.")
			return
		end
		
        if name == "" then
            print("Pedro: Please enter a display name.")
            return
		end
		
        G.customSongs = G.customSongs or {}
		
        if #G.customSongs >= 10 then
			print("Pedro: You can only add up to 10 custom songs.")
			return
		end
		
		-- Only use the new custom folder
		local finalPath = "Interface\\AddOns\\Pedro_Custom\\Media\\" .. filename
		
		-- Validate the file exists
		local ok = PlaySoundFile(finalPath, "Dialog")
		if not ok then
			print("Pedro: File not found in Pedro_Custom/Media. If you just added it, type /reload.")
			return
		end
		
		table.insert(G.customSongs, {
			name = name,
			path = finalPath,
			duration = dur
		})
		
		print("Pedro: Added custom song '" .. name .. "'.")
		
		RebuildPedroSounds()
		RefreshSoundListUI()
		ResizePedroMainUI()
		
		f:Hide()
		
	end)
	
    local closeBtn = CreateFrame("Button", nil, f, "UIPanelButtonTemplate")
    closeBtn:SetSize(100, 22)
    closeBtn:SetPoint("BOTTOMRIGHT", -15, 15)
    closeBtn:SetText("Cancel")
    closeBtn:SetScript("OnClick", function() f:Hide() end)
	
    f:Show()
end
