/*
 * See the files COPYING and README for copyright information and how to reach 
 * the author.
 *
 *  $Id: config.c,v 1.3 2006/04/20 18:59:26 lordjaxom Exp $
 */

#include "config.h"

namespace vdr_burn
{

	using namespace std;
	using proctools::logger;
	using proctools::format;

	// --- dvdauthor_xml  -----------------------------------------------------

	dvdauthor_xml::dvdauthor_xml(const job& Job):
			m_job(Job)
	{
	}

	void dvdauthor_xml::write_main_menu(void)
	{
		int pages = ScanPageCount(m_job.get_paths().data);
		for (int p = 0; p < pages; ++p) {
			m_file << "    <pgc" << (p == 0 ? " entry=\"title\"" : "") << ">" << endl
			  << "      <vob file=\"" << m_job.get_paths().data << "/menu-bg-"
			  << p << ".mpg\"/>" << endl;

			for (uint i = p * MAXPAGETITLES; 
				 i < std::min(static_cast<recording_list::size_type>((p + 1) 
						 * MAXPAGETITLES), m_job.get_recordings().size()); 
				 ++i)
				m_file << "      <button>jump titleset " << (i + 1) 
				  << " menu;</button>" << endl;

			if (p > 0)
				m_file << "      <button>jump menu " << p << ";</button>" << endl;

			if (p < pages - 1)
				m_file << "      <button>jump menu " << (p + 2) << ";</button>" 
				  << endl;

			m_file << "    </pgc>" << endl;
		}
	}

	void dvdauthor_xml::write_title_menu(recording_list::const_iterator rec)
	{
		int pages = ScanPageCount(rec->get_paths().data);

		m_file << "<menus>" << endl;
	
		for (int i = 0; i < pages; ++i) {
			m_file << "    <pgc" << (i == 0 ? " entry=\"root\"" : "") << ">" 
			  << endl
			  << "      <vob file=\"" << rec->get_menu_mpeg(i) << "\"/>" 
			  << endl;

			if (i > 0)
				m_file << "      <button>jump menu " << i << ";</button>" 
				  << endl;

			if (i < pages - 1)
				m_file << "      <button>jump menu " << (i + 2) << ";</button>" 
				  << endl;

			m_file << "      <button>jump title 1 chapter 1;</button>" << endl
			  << "      <button>jump vmgm menu;</button>" << endl
			  << "    </pgc>" << endl;
		}

		m_file << "  </menus>";
	}

	void dvdauthor_xml::write(void) 
	{
		m_file.open(get_xml_path().c_str(), ios::out);
		if (!m_file) {
			logger::error(format("couldn't create {0}") % get_xml_path());
			return;
		}
		
		m_file << "<dvdauthor dest=\"" << get_author_path() << "\">" << endl
		  << "  <vmgm><menus>" << endl;

		bool createMenu = (m_job.get_disk_type() == disk_type::dvd_menu);
		if (createMenu)
			write_main_menu();

		m_file << "  </menus></vmgm>" << endl;

		for (recording_list::const_iterator rec = m_job.get_recordings().begin();
			 rec != m_job.get_recordings().end(); ++rec) {

			m_file << "  <titleset>";
			
			if (createMenu)
				write_title_menu(rec);
			
			m_file << "<titles>" << endl
			  << "    <pgc>" << endl
			  << "      <vob file=\"" << rec->get_movie_path() 
			  << "\" chapters=\""
			  << rec->get_chapters(m_job.get_chapters_mode()) << "\"/>" << endl;

			if (createMenu)
				m_file << "      <post>call vmgm menu;</post>" << endl;

			m_file << "    </pgc>" << endl
			  << "  </titles></titleset>" << endl;
		}
		m_file << "</dvdauthor>\n";
	}

}
