#include <Python.h>
#include <ygNodeDB.h>
#include <ygUtil.h>
#include "pyInterface.h"
#include "pySelector.h"

using namespace std;

extern "C" ygNode* construct_pySelector(const char* name,bool master) { return new pySelector(name,master); }

struct _pySelectorPrivateData
	{
	pyInterface * python;
	};


static PyObject* emb_select(PyObject *self, PyObject *args)
	{
	char *nodeName, *childName;
	if (!PyArg_ParseTuple(args, "ss", &nodeName, &childName))
        	return NULL;
	ygNode * node = ygNodeDB::find(nodeName);
	if ((node) && (node->isOfClass("ygSelector")))
		{
		ygSelector * selector = (ygSelector *) node;
		selector->select(childName);
		}
	Py_INCREF(Py_None);
	return Py_None;
	}


static PyObject* emb_selectNum(PyObject *self, PyObject *args)
	{
	char *nodeName;
	int childNum;
	if (!PyArg_ParseTuple(args, "si", &nodeName, &childNum))
        	return NULL;
	ygNode * node = ygNodeDB::find(nodeName);
	if ((node) && (node->isOfClass("ygSelector")))
		{
		ygSelector * selector = (ygSelector *) node;
		selector->select(childNum);
		}
	Py_INCREF(Py_None);
	return Py_None;
	}


static PyObject* emb_selected(PyObject *self, PyObject *args)
	{
	char *nodeName;
	ygString value("");
	if (!PyArg_ParseTuple(args, "s", &nodeName))
        	return NULL;
	ygNode * node = ygNodeDB::find(nodeName);
	if ((node) && (node->isOfClass("ygSelector")))
		{
		ygSelector * selector = (ygSelector *) node;
		if (selector->selected())
			value = selector->selected()->name();
		}
	return Py_BuildValue("s",value.c_str());
	}


static PyObject* emb_selectedNum(PyObject *self, PyObject *args)
	{
	char *nodeName;
	int value=0;
	if (!PyArg_ParseTuple(args, "s", &nodeName))
        	return NULL;
	ygNode * node = ygNodeDB::find(nodeName);
	if ((node) && (node->isOfClass("ygSelector")))
		{
		ygSelector * selector = (ygSelector *) node;
		value = selector->selectedNum();
		}
	return Py_BuildValue("i",value);
	}


static PyMethodDef EmbMethods[] =
	{
	{"select", emb_select, METH_VARARGS,
		"Activate a selector node's child, by name."},
	{"selectNum", emb_selectNum, METH_VARARGS,
		"Activate a selector node's child, by number."},
	{"selected", emb_selected, METH_VARARGS,
		"Get name of a selector node's currently active child."},
	{"selectedNum", emb_selectedNum, METH_VARARGS,
		"Get number of a selector node's currently active child."},
	{NULL, NULL, 0, NULL}
	};


void pySelector::initializePythonYgSelector(void)
	{
	static bool initted = false;
	if (initted)
		return;
	initted = true;
	Py_InitModule("ygselector", EmbMethods);
	PyRun_SimpleString("import ygselector\n"
			"class ygSelector(ygNode):\n"
			"	def select(s,name):\n"
			"		ygselector.select(s.ygNodeName,name)\n"
			"	def selectNum(s,num):\n"
			"		ygselector.selectNum(s.ygNodeName,num)\n"
			"	def selected(s):\n"
			"		return ygselector.selected(s.ygNodeName)\n"
			"	def selectedNum(s):\n"
			"		return ygselector.selectedNum(s.ygNodeName)\n"
			);
	}


pySelector::pySelector(const char* name,bool master) : ygSelector(name,master)
	{
	setClassName("pySelector");
	p_ = new struct _pySelectorPrivateData;
	p_->python = new pyInterface(this);
	initializePythonYgSelector();
	}

pySelector::~pySelector(void)
	{
	delete p_->python;
	delete p_;
	}


void pySelector::message(const ygMessage& msg)
	{
	if (p_->python->message(msg))
		;
	else
		ygSelector::message(msg);
	}


void pySelector::app(void)
	{
	p_->python->app();
	ygSelector::app();
	}
