#include "setup.h"

#include <vdr/menuitems.h>
#include "plugins.h"

cMediadSetup MediadSetup;

cMediadSetup::cMediadSetup(void) {
	// add all known plugins
	mediaplugins.Add(new dmharchivedvd());
	mediaplugins.Add(new vdrrecording());
	mediaplugins.Add(new dvd());
	mediaplugins.Add(new vcd());
	mediaplugins.Add(new mplayer());
	mediaplugins.Add(new mp3());
	mediaplugins.Add(new music());
	mediaplugins.Add(new photocd());
	mediaplugins.Add(new ripit());
	mediaplugins.Add(new cdda());
	mediaplugins.Add(new burn());
	mediaplugins.Add(new image());
	mediaplugin *src = mediaplugins.First();
	while(src) {
		src=mediaplugins.Next(src);
	}
	HideMenu = false;
}

cMediadSetup::cMediadSetup(const cMediadSetup &setup)
{
	mediaplugin *pPlugin = setup.mediaplugins.First();
	while(pPlugin) {
		mediaplugins.Add(pPlugin->clone());
		pPlugin = setup.mediaplugins.Next(pPlugin);
	}
	HideMenu = setup.HideMenu;
}

cMediadSetup& cMediadSetup::operator=(const cMediadSetup &setup)
{
	mediaplugins.Clear();
	mediaplugin *pPlugin = setup.mediaplugins.First();
	while(pPlugin) {
		mediaplugins.Add(pPlugin->clone());
		pPlugin = setup.mediaplugins.Next(pPlugin);
	}
	HideMenu = setup.HideMenu;
	return *this;
}

bool cMediadSetup::SetupParse(const char *Name, const char *Value) {
	mediaplugin *pPlugin = mediaplugins.First();
	while(pPlugin) {
		char *pluginName;
		asprintf(&pluginName, "AutoStart%s", pPlugin->getName());
		if(strcmp(Name, pluginName) == 0) {
			pPlugin->setAutoStart(atoi(Value));
			free(pluginName);
			return true;
			break;
		}
		free(pluginName);
		asprintf(&pluginName, "Activated%s", pPlugin->getName());
		if(strcmp(Name, pluginName) == 0) {
			pPlugin->setActive(atoi(Value));
			free(pluginName);
			return true;
			break;
		}
		free(pluginName);
		pPlugin = mediaplugins.Next(pPlugin);
	}
  if (strcmp(Name, "HideMenu" )  == 0) {
		HideMenu = atoi(Value);
		return true;
	}
	return false;
}

cMediadMenuSetupPage::cMediadMenuSetupPage(void) {
	_setup = MediadSetup;

	mediaplugin *pPlugin = _setup.mediaplugins.First();
	while(pPlugin) {
		if(pPlugin->checkPluginAvailable(true)) {
			char* menuEntry;
			asprintf(&menuEntry, "%s %s", tr(pPlugin->getName()), tr("AutoStart"));
			Add(new cMenuEditBoolItem(menuEntry, pPlugin->isAutoStartSetup()));
			free(menuEntry);
			asprintf(&menuEntry, "%s %s", tr(pPlugin->getName()), tr("activated"));
			Add(new cMenuEditBoolItem(menuEntry, pPlugin->isActiveSetup()));
			free(menuEntry);
		}
		pPlugin = _setup.mediaplugins.Next(pPlugin);
	}
	Add(new cMenuEditBoolItem(tr("Hide main menu entry"),
			&_setup.HideMenu));
}

cMediadMenuSetupPage::~cMediadMenuSetupPage() {
}

void cMediadMenuSetupPage::Store(void) {
	mediaplugin *pPlugin = _setup.mediaplugins.First();
	while(pPlugin) {
		char *pluginName;
		asprintf(&pluginName, "AutoStart%s", pPlugin->getName());
		SetupStore(pluginName, pPlugin->isAutoStart());
		asprintf(&pluginName, "Activated%s", pPlugin->getName());
		SetupStore(pluginName, pPlugin->isActive());
		free(pluginName);
		pPlugin = _setup.mediaplugins.Next(pPlugin);
	}
	SetupStore("HideMenu", _setup.HideMenu);

	MediadSetup = _setup;
}
