/*
 * See the files COPYING and README for copyright information and how to reach 
 * the author.
 *
 *  $Id: config.c,v 1.4 2006/05/10 18:54:19 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)
	{
		m_file << "    <menus>" << endl;
		int pages = ScanPageCount(m_job.get_paths().data);
		for (int p = 0; p < pages; ++p) {
			if (p == 0) {
				m_file << "      <pgc entry=\"title\">" << endl // pause="inf"??
				  	   << "        <pre>" << endl;
				for (int q = 2; q <= pages; q++)
					m_file << "           if (g0 eq " << q << ") { jump menu " << q <<";}" << endl;
				m_file << "             button=g1;g3=0;" << endl
				  	   << "        </pre>" << endl;
			}
			else
			{
				m_file << "    <pgc>" << endl // pause="inf"??
				  	   << "      <pre>button=g1;g3=0;</pre>" << endl;
			}

			m_file << "        <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>g1=button;jump titleset " << (i + 1) 
				  	   << " menu;</button>" << endl;

			if (p > 0)
				m_file << "        <button>g0=" << p << ";g1=" << (MAXPAGETITLES*1024) 
					   << ";jump menu " << p << ";</button>" << endl;

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

			m_file << "      </pgc>" << endl;
		}
		m_file << "    </menus>" << 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;
			if ( i == 0) {
				m_file << "        <pre>button=1024;</pre>" << endl;
			}
			else
			{
				m_file << "        <pre>if (g3 eq 1){button=3072;}" << endl
				  	   << "             if (g3 eq 2){button=1024;}" << endl
				  	   << "             if (g3 eq 3){button=2048;}" << endl
				  	   << "        </pre>" << endl;
			}
			m_file << "        <vob file=\"" << rec->get_menu_mpeg(i) << "\"/>" << endl;

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

			m_file << "        <button>jump title 1 chapter 1;</button>" << endl;
			if (i < pages - 1)
				m_file << "        <button>g3=" << (i == pages - 2 ? 3 : 1) << ";jump menu " 
					   << (i + 2) << ";</button>" << endl;

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

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

	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 << "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" << endl;
		m_file << "<dvdauthor dest=\"" << get_author_path() << "\">" << endl
		  << "  <vmgm>" << endl;

		bool createMenu = (m_job.get_disk_type() == disk_type::dvd_menu);
		if (createMenu) {
			m_file << "    <fpc>g0=1;g1=1024;jump vmgm menu 1;</fpc>" << endl;
			write_main_menu();
			m_file << "  </vmgm>" << endl;

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

				m_file << "  <titleset>" << endl;
				write_title_menu(rec);
				m_file << "    <titles>" << endl
				  << "      <!-- to be added later: audio lang=\"de\"/ -->" << endl
				  << "      <pgc>" << endl
				  << "        <vob file=\"" << rec->get_movie_path() 
				  << "\" chapters=\""
				  << rec->get_chapters(m_job.get_chapters_mode()) << "\"/>" << endl;

				m_file << "        <post>call vmgm menu;</post>" << endl;
				m_file << "      </pgc>" << endl
				  << "    </titles>" << endl 
				  << "  </titleset>" << endl;
			}
		}
		else
		{
			m_file << "    <fpc>g3=1;jump vmgm menu 1;</fpc>" << endl
			  << "    <menus>" << endl
			  << "      <pgc entry=\"title\">" << endl
			  << "        <pre>" << endl;
			int titleset = 1;
			for (recording_list::const_iterator rec = m_job.get_recordings().begin();
				 rec != m_job.get_recordings().end(); ++rec) {
				m_file << "          if (g3 eq " << titleset << ") {jump title " << titleset 
					   << ";}" << endl;
				titleset++;
			}
			m_file << "        </pre>" << endl 
			  << "        <!-- dummy vob required: vob file= -->" << endl
			  << "      </pgc>" << endl
			  << "    </menus>" << endl
			  << "  </vmgm>" << endl;
			int t = 1;
			for (recording_list::const_iterator rec = m_job.get_recordings().begin();
				 rec != m_job.get_recordings().end(); ++rec) {
				m_file << "  <titleset>" << endl;
				m_file << "    <titles>" << endl
				  << "      <!-- to be added later: audio lang=\"de\"/ -->" << endl
				  << "      <pgc>" << endl
				  << "        <vob file=\"" << rec->get_movie_path() 
				  << "\" chapters=\""
				  << rec->get_chapters(m_job.get_chapters_mode()) << "\" pause=\"2\" />" << endl;
				if (t+1 < titleset) {
					m_file << "        <post>g3=" << ++t <<";call vmgm menu 1;</post>" << endl;
				}
				else {
					m_file << "        <post>exit;</post>" << endl;
				}

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

}
