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

using namespace std;

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

struct _pyTransformPrivateData
	{
	pyInterface * python;
	};


static PyObject* emb_setPosition(PyObject *self, PyObject *args)
	{
	char *nodeName;
	float x,y,z;
	if (!PyArg_ParseTuple(args, "sfff", &nodeName, &x, &y, &z))
        	return NULL;
	ygNode * node = ygNodeDB::find(nodeName);
	if ((node) && (node->isOfClass("ygTransform")))
		{
		ygTransform * transform = (ygTransform *) node;
		transform->setPosition(x,y,z);
		}
	Py_INCREF(Py_None);
	return Py_None;
	}


static PyObject* emb_setOrientation(PyObject *self, PyObject *args)
	{
	char *nodeName;
	float x,y,z;
	if (!PyArg_ParseTuple(args, "sfff", &nodeName, &x, &y, &z))
        	return NULL;
	ygNode * node = ygNodeDB::find(nodeName);
	if ((node) && (node->isOfClass("ygTransform")))
		{
		ygTransform * transform = (ygTransform *) node;
		transform->setOrientation(x,y,z);
		}
	Py_INCREF(Py_None);
	return Py_None;
	}


static PyObject* emb_setSize(PyObject *self, PyObject *args)
	{
	char *nodeName;
	float x,y,z;
	if (!PyArg_ParseTuple(args, "sfff", &nodeName, &x, &y, &z))
        	return NULL;
	ygNode * node = ygNodeDB::find(nodeName);
	if ((node) && (node->isOfClass("ygTransform")))
		{
		ygTransform * transform = (ygTransform *) node;
		transform->setSize(x,y,z);
		}
	Py_INCREF(Py_None);
	return Py_None;
	}


static PyMethodDef EmbMethods[] =
	{
	{"setPosition", emb_setPosition, METH_VARARGS,
		"Set a transform node's position."},
	{"setOrientation", emb_setOrientation, METH_VARARGS,
		"Set a transform node's orientation."},
	{"setSize", emb_setSize, METH_VARARGS,
		"Set a transform node's size."},
	{NULL, NULL, 0, NULL}
	};


void pyTransform::initializePythonYgTransform(void)
	{
	static bool initted = false;
	if (initted)
		return;
	initted = true;
	Py_InitModule("ygtransform", EmbMethods);
	PyRun_SimpleString("import ygtransform\n"
			"class ygTransform(ygNode):\n"
			"	def setPosition(s,x,y,z):\n"
			"		ygtransform.setPosition(s.ygNodeName,x,y,z)\n"
			"	def setOrientation(s,x,y,z):\n"
			"		ygtransform.setOrientation(s.ygNodeName,x,y,z)\n"
			"	def setSize(s,x,y,z):\n"
			"		ygtransform.setSize(s.ygNodeName,x,y,z)\n"
			);
	}


pyTransform::pyTransform(const char* name,bool master) : ygTransform(name,master)
	{
	setClassName("pyTransform");
	p_ = new struct _pyTransformPrivateData;
	p_->python = new pyInterface(this);
	initializePythonYgTransform();
	}


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


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


void pyTransform::app(void)
	{
	p_->python->app();
	ygTransform::app();
	}
