/*
 * See the files COPYING and README for copyright information and how to reach
 * the author.
 *
 *  $Id: setup.c,v 1.12 2006/05/16 17:00:19 lordjaxom Exp $
 */

#include "setup.h"
#include "menuitems.h"
#include "common.h"
#include "i18n.h"
#include <algorithm>
#include <vdr/menuitems.h>
#include <vdr/videodir.h>
#include <getopt.h>

namespace vdr_burn
{

	using namespace std;
	using namespace proctools;

// --- cBurnParameters --------------------------------------------------------

cBurnParameters BurnParameters;

cBurnParameters::cBurnParameters(void):
		DataPath(VideoDirectory),
		TempPath(TMPDIR),
		DvdDevice(DVDDEV),
		IsoPath(ISODIR)
{
}

bool cBurnParameters::ProcessArgs(int argc, char *argv[])
{
	static struct option opts[] = {
			{ "tempdir", required_argument, NULL, 't' },
			{ "datadir", required_argument, NULL, 'd' },
			{ "dvd",     required_argument, NULL, 'D' },
			{ "iso",     required_argument, NULL, 'i' },
			{ NULL }
	};

	int c, optind = 0;
	while ((c = getopt_long(argc, argv, "t:d:D:i:", opts, &optind)) != -1) {
		switch (c) {
			case 't': TempPath = optarg; break;
			case 'd': DataPath = optarg; break;
			case 'D': DvdDevice = optarg; break;
			case 'i': IsoPath = optarg; break;
			default:  return false;
		}
	}

	struct stat sbuf;
	if (stat(DvdDevice.c_str(), &sbuf) != 0) {
		isyslog("burn: couldn't stat %s, assuming iso-creation only",
				DvdDevice.c_str());
#ifdef STONEAGE_GCC
		DvdDevice = string();
#else
		DvdDevice.clear();
#endif
	}

	if (stat(IsoPath.c_str(), &sbuf) != 0) {
		isyslog("burn: couldn't stat %s, assuming writing to disc only",
				IsoPath.c_str());
#ifdef STONEAGE_GCC
		IsoPath = string();
#else
		IsoPath.clear();
#endif
	}

	if (DvdDevice.empty() && IsoPath.empty()) {
		esyslog("ERROR[burn]: no targets left, check --dvd and --iso");
		fprintf(stderr,
				"ERROR[burn]: no targets left, check --dvd and --iso\n");
		return false;
	}
	return true;
}

// --- cBurnSetup -------------------------------------------------------------

cBurnSetup BurnSetup;

cBurnSetup::cBurnSetup(void):
		PROCTOOLS_INIT_PROPERTY( UseIso2UTF,      0 ),
		PROCTOOLS_INIT_PROPERTY( RemovePath,      0 ),
		PROCTOOLS_INIT_PROPERTY( OfferChapters,   1 ),
		PROCTOOLS_INIT_PROPERTY( OfferDiskSize,   1 ),
		PROCTOOLS_INIT_PROPERTY( OfferStoreMode,  1 ),
		PROCTOOLS_INIT_PROPERTY( ChaptersMode,    cm10Mins ),
		PROCTOOLS_INIT_PROPERTY( StoreMode,       smBurnISO ),
		PROCTOOLS_INIT_PROPERTY( DiskSize,        disk_size::single_layer ),
		PROCTOOLS_INIT_PROPERTY( CustomDiskSize,  200 ),
		PROCTOOLS_INIT_PROPERTY( BurnSpeed,       0 ),
		PROCTOOLS_INIT_PROPERTY( DemuxType,       demux_type::vdrsync ),
		PROCTOOLS_INIT_PROPERTY( CutOnDemux,      0 ),
		PROCTOOLS_INIT_PROPERTY( OfferCutOnDemux, 1 )
{
}

bool cBurnSetup::SetupParse(const char *Name, const char *Value)
{
	return set(Name, Value);
	//if (strcmp(Name, "UseIso2UTF") == 0)
		//UseIso2UTF = atoi(Value);
	//else if (strcmp(Name, "RemovePath") == 0)
		//RemovePath = atoi(Value);
	/*if (strcmp(Name, "OfferChapters") == 0)
		OfferChapters = atoi(Value);
	else if (strcmp(Name, "ChaptersMode") == 0)
		ChaptersMode = static_cast<eChaptersMode>(atoi(Value));
	else if (strcmp(Name, "StoreMode") == 0)
		StoreMode = static_cast<eStoreMode>(atoi(Value));
	else if (strcmp(Name, "DiskSize") == 0)
		DiskSize = static_cast<disk_size::type>(atoi(Value));
	else if (strcmp(Name, "CustomDiskSize") == 0)
		CustomDiskSize = atoi(Value);
	else if (strcmp(Name, "OfferDiskSize") == 0)
		OfferDiskSize = atoi(Value);
	else if (strcmp(Name, "OfferStoreMode") == 0)
		OfferStoreMode = atoi(Value);
	else if (strcmp(Name, "BurnSpeed") == 0)
		BurnSpeed = atoi(Value);
	else if (strcmp(Name, "DemuxType") == 0)
		DemuxType = static_cast<demux_type::type>(atoi(Value));
	else if (strcmp(Name, "CutOnDemux") == 0)
		CutOnDemux = atoi(Value);
	else if (strcmp(Name, "OfferCutOnDemux") == 0)
		OfferCutOnDemux = atoi(Value);
	else return false;
	return true;*/
}

// --- cBurnMenuSetupPage helper functors -------------------------------------

struct setup_storer
{
	setup_storer(cBurnMenuSetupPage* menu): m_menu( menu ) {}
	void operator()(pair<const string, property_base*>& value)
	{
		m_menu->SetupStore(value.first.c_str(), value.second->str().c_str());
	}
	cBurnMenuSetupPage* m_menu;
};

// --- cBurnMenuSetupPage -----------------------------------------------------

cBurnMenuSetupPage::cBurnMenuSetupPage(void)
{
	mData = BurnSetup;

	Add(new cMenuEditBoolItem(tr("Convert texts to UTF-8"), &mData.UseIso2UTF));
	Add(new cMenuEditBoolItem(tr("Remove path component"), &mData.RemovePath));
	Add(new cMenuEditStraItem(tr("Demux using"), 
			reinterpret_cast<int*>(&mData.DemuxType), demux_type::count, 
			i18n::get_demux_types()));
	Add(new cMenuEditBoolItem(tr("Offer chapters"), &mData.OfferChapters));
	Add(new cMenuEditBoolItem(tr("Offer target"), &mData.OfferStoreMode));
	Add(new cMenuEditBoolItem(tr("Offer disk size"), &mData.OfferDiskSize));
	Add(new cMenuEditBoolItem(tr("Offer cutting"), &mData.OfferCutOnDemux));

	Add(new menu::text_item(tr("--- Defaults --------------------------------"
							   "-------------------------")));
	Add(new cMenuEditStraItem(tr("Chapters"),
							  reinterpret_cast<int*>(&mData.ChaptersMode),
							  cm_Count, i18n::ChaptersModes()));
	Add(new cMenuEditStraItem(tr("Target"),
							  reinterpret_cast<int*>(&mData.StoreMode),
							  sm_Count, i18n::StoreModes()));
	Add(new cMenuEditStraItem(tr("Disk size"),
							  reinterpret_cast<int*>(&mData.DiskSize),
							  disk_size::count, i18n::get_disk_sizes()));
	Add(new cMenuEditIntItem(tr("Custom disk size (MB)"), &mData.CustomDiskSize,
							 0, 9999));
	Add(new menu::speed_edit_item(tr("Burn speed"), mData.BurnSpeed));
	Add(new cMenuEditBoolItem(tr("Cut while processing"), &mData.CutOnDemux));
}

void cBurnMenuSetupPage::Store(void)
{
	for_each(mData.begin(), mData.end(), setup_storer(this));
	/*SetupStore("UseIso2UTF", mData.UseIso2UTF);
	SetupStore("RemovePath", mData.RemovePath);
	SetupStore("OfferChapters", mData.OfferChapters);
	SetupStore("OfferDiskSize", mData.OfferDiskSize);
	SetupStore("ChaptersMode", mData.ChaptersMode);
	SetupStore("StoreMode", mData.StoreMode);
	SetupStore("DiskSize", mData.DiskSize);
	SetupStore("CustomDiskSize", mData.CustomDiskSize);
	SetupStore("OfferStoreMode", mData.OfferStoreMode);
	SetupStore("BurnSpeed", mData.BurnSpeed);
	SetupStore("DemuxType", mData.DemuxType);
	SetupStore("CutOnDemux", mData.CutOnDemux);
	SetupStore("OfferCutOnDemux", mData.OfferCutOnDemux);*/
	BurnSetup = mData;
}

}
