#include "py_classes.h"
#include "engine.h"

extern Engine *engine;

PyObject* ship_init(ShipObject* self, PyObject* args)
{
	int x=0;
	int y=0;
	char* img = NULL;
	char* fpk = NULL;
	float zoom=(float)0.0;
	if (!PyArg_ParseTuple(args, "iissf", &x, &y, &img, &fpk, &zoom)) return NULL;

	self->unit->Init(x, y, img, fpk, zoom);

	Py_INCREF(Py_None);
	return Py_None;
}

PyObject* ship_display(ShipObject* self, PyObject* args)
{
	int x=0;
	int y=0;

	if (!PyArg_ParseTuple(args, "ii", &x, &y)) return NULL;
	self->unit->Display(x,y);

	Py_INCREF(Py_None);
	return Py_None;
}

PyObject* ship_setlocation(ShipObject* self, PyObject* args, PyObject* kw)
{
	static char* keywords[] = { "x", "y", 0 };
	int x=0;
	int y=0;
	if (!PyArg_ParseTupleAndKeywords(args, kw, "ii:ShipSetLocation", keywords, &x, &y)) return 0;
	self->unit->SetX(x);
	self->unit->SetY(y);

	Py_INCREF(Py_None);
	return Py_None;
}

PyObject* ship_colorize(ShipObject* self, PyObject* args, PyObject* kw)
{
	static char* keywords[] = { "r", "g", "b", 0 };
	Uint8 r=0;
	Uint8 g=0;
	Uint8 b=0;
	if (!PyArg_ParseTupleAndKeywords(args, kw, "iii:ShipColorize", keywords, &r, &g, &b)) return 0;
	self->unit->Colorize(r, g, b);

	Py_INCREF(Py_None);
	return Py_None;
}

PyObject* ship_ai(ShipObject* self, PyObject* args, PyObject* kw)
{
	static char* keywords[] = { "aistate", 0 };
	char* saistate = NULL;
	int aistate=AI_STATE_STOP;

	if (!PyArg_ParseTupleAndKeywords(args, kw, "s:ShipAI", keywords, &saistate)) return 0;
	self->unit->SetAI("SIMPLE");
	if (!strcmp(saistate,"AI_STATE_STOP")) aistate=AI_STATE_STOP;
	else if (!strcmp(saistate,"AI_STATE_RUN")) aistate=AI_STATE_RUN;
	else if (!strcmp(saistate,"AI_STATE_ATTACK")) aistate=AI_STATE_ATTACK;
	else if (!strcmp(saistate,"AI_STATE_LOOK")) aistate=AI_STATE_LOOK;
	else if (!strcmp(saistate,"AI_STATE_IGNORE")) aistate=AI_STATE_IGNORE;
	else if (!strcmp(saistate,"AI_STATE_WAITLOOK")) aistate=AI_STATE_WAITLOOK;
	self->unit->SetAIState(aistate);

	Py_INCREF(Py_None);
	return Py_None;
}

PyMethodDef shipMethods[] =
{
	{"Init", (PyCFunction)ship_init, METH_VARARGS, "Inits ship"},
	{"Display", (PyCFunction)ship_display, METH_VARARGS, "Displays the ship"},
	{"SetLocation", (PyCFunction)ship_setlocation, METH_KEYWORDS, "SetLocation(x, y)\nSets the location of a ship"},
	{"Colorize", (PyCFunction)ship_colorize, METH_KEYWORDS, "Colorize(r, g, b)\nApplies a color to a ship"},
	{"AI", (PyCFunction)ship_ai, METH_KEYWORDS, "AI(aistate)\nGive the Ship an AI"},
	{NULL, NULL, 0, NULL}
};
//PyGetSetDef properties[] = {
//	{   0   }
//};

PyTypeObject pyship_type;
void Init_ShipType()
{
	memset(&pyship_type, 0, sizeof pyship_type);

	pyship_type.ob_refcnt = 1;
	pyship_type.ob_type = &PyType_Type;
	pyship_type.tp_name = "Ship";
	pyship_type.tp_basicsize = sizeof pyship_type;
	pyship_type.tp_dealloc = (destructor)Destroy_Ship;
	pyship_type.tp_methods = shipMethods;
	//pyship_type.tp_getset = properties;
	pyship_type.tp_doc = "Ship object";
	pyship_type.tp_new = New_Ship;

	PyType_Ready(&pyship_type);
}

PyObject* New_Ship(PyTypeObject* type, PyObject* args, PyObject* kw)
{
	ShipObject* unit1 = PyObject_New(ShipObject, &pyship_type);

	if (!unit1) return 0;
	unit1->unit=new Unit();
	Map *map=engine->GetMap();
	map->AddToUnits(unit1->unit);

	return (PyObject*)unit1;
}

void Destroy_Ship(ShipObject* self)
{
	PyObject_Del(self);
}
