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, 260)
    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("Place your .ogg file inside:\nInterface/AddOns/Pedro/Media/\n\nThen enter the filename exactly (e.g. MySong.ogg).")
	
    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 <= 0 then
            print("Pedro: Please enter a valid duration.")
            return
		end
		
        if name == "" then
            print("Pedro: Please enter a display name.")
            return
		end
		
        G.customSongs = G.customSongs or {}
		
        if #G.customSongs >= 5 then
            print("Pedro: You can only add up to 5 custom songs.")
            return
		end
		
        local fullPath = "Interface\\AddOns\\Pedro\\Media\\" .. filename
		
        local ok = PlaySoundFile(fullPath, "Dialog")
        if not ok then
            print("Pedro: File not found or unsupported format. If you just added it, type /reload.")
            return
		end
		
        table.insert(G.customSongs, {
            name = name,
            path = fullPath,
            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
