// Playlist.cpp: Implementierung der Klasse Playlist.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Playlist.h"
#include "OutputInfo.h"
#include "Resource.h"
#include "CommonDlg.h"
#include "TrackInfo.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Konstruktion/Destruktion
//////////////////////////////////////////////////////////////////////

Playlist::Playlist()
{

}

Playlist::~Playlist()
{

}

Playlist* Playlist::m_instance = NULL;

void Playlist::CreateM3U()
{
	CString csPath = OutputInfo::Instance()->GetOutputFolder();

	CString csArtist = TrackInfo::Instance()->csArtist;
	CString csAlbum  = TrackInfo::Instance()->csAlbum;
	
	CString csFileName = _T("");
	if( csArtist == _T("") || csAlbum == _T("") ) {
		csFileName = _T("Playlist.m3u");
	}
	else {
		csFileName = csArtist;
		csFileName += _T(" - ");
		csFileName += csAlbum;
		csFileName += _T(".m3u");
	}

	CString csFilePath = csPath + csFileName;

	CFile file;
	file.Open( csFilePath, CFile::modeCreate | CFile::modeWrite );

	int iTracks = OutputInfo::Instance()->GetPlaylistEntries().size();
	CString csNewLine = _T("\n\r");
	for( int i=0; i<iTracks; i++ ) {
		file.Write( OutputInfo::Instance()->GetPlaylistEntries()[i],
			        OutputInfo::Instance()->GetPlaylistEntries()[i].GetLength() );
		file.Write( csNewLine, csNewLine.GetLength() );		
	}

	file.Close();
}

void Playlist::CreateB4S()
{
	CString csPath = OutputInfo::Instance()->GetOutputFolder();

	CString csFileName = TrackInfo::Instance()->csAlbum;
	csFileName += _T(" - ");
	csFileName += TrackInfo::Instance()->csArtist;
	csFileName += _T(".b4s");

	CString csFilePath = csPath + csFileName;

//  <playlist num_entries="67" label="Archiv 5">
//    <entry Playstring="file:E:\Mp3\Archiv5\A-Ha - Lifelines.mp3">
//      <Name>A-ha - Lifelines</Name>
//      <Length>257758</Length>
//    </entry>
//
//    <entry Playstring="file:E:\Mp3\Archiv5\All Saints - Never Ever.mp3">
//      <Name>All Saints - Never Ever</Name>
//      <Length>314653</Length>
//    </entry>

	CString csXMLStart = _T("<?xml version=\"1.0\">\n\r<WinampXML>\n\r<!-- Generated by Wrip 1.0 -->\n\r");
	CString csXMLEnd   = _T("</playlist>\n\r</WinampXML>\n\r");
	
	CString csXMLMid   = _T("");
	csXMLMid.Format( _T("<playlist num_entries=\"%d\" label=\"%s\">\n\r"), TrackInfo::Instance()->GetNumberOfTracks(), csFileName );

	CFile file;
	file.Open( csFilePath, CFile::modeCreate | CFile::modeWrite );
	
	file.Write( csXMLStart, csXMLStart.GetLength() );
	file.Write( csXMLMid, csXMLMid.GetLength() );

	int iTracks = TrackInfo::Instance()->GetNumberOfTracks();
	CString csTmp = _T("");
	for( int i=0; i<iTracks; i++ ) {
		csTmp.Format( _T("<entry Playstring=\"file:%s\">\n\r<Name>%s</Name>\n\r</entry>\n\r"),
					  OutputInfo::Instance()->GetMP3FileNames()[i],
					  OutputInfo::Instance()->GetMP3FileNames()[i] );
		file.Write( csTmp, csTmp.GetLength() );
	}

	file.Write( csXMLEnd, csXMLEnd.GetLength() );

	file.Close();
}

Playlist* Playlist::Instance()
{
	if( m_instance == NULL ) {
		m_instance = new Playlist;
	}

	return m_instance;
}
