/*
    Copyright (c) 2007 SIEGE Development Team
    All rights reserved.
    
    This file is part of libSIEGE.
    
    This software is copyrighted work licensed under the terms of the
    2-clause BSD license. Please consult the file "license.txt" for
    details.
    
    If you did not recieve the file with this program, please email
    Tim Chas <darkuranium@gmail.com>.
*/

#include "core.h"
#include "window.h"
#include "entity.h"
#include "event.h"

static PyObject* pysLoadModules(PyObject* self, PyObject* args)
{
    if(!PyTuple_Check(args))
        goto end;

    size_t size = PyTuple_Size(args);
    size_t i;
    for(i = 0; i < size; i++)
    {
        char* arg = PyString_AsString(PyTuple_GET_ITEM(args, i));
        if(arg == NULL)
            return NULL;
        sgLoadModule(arg);
    }

end:
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject* pysLoadModule(PyObject* self, PyObject* args)
{
    char* name;
    PyArg_ParseTuple(args, "s", &name);

    PyObject* ret = sgLoadModule(name) ? Py_True : Py_False;
    Py_INCREF(ret);
    return ret;
}

static PyObject* pysInit(PyObject* self, PyObject* args)
{
    unsigned int width, height, bpp, flags;
    PyArg_ParseTuple(args, "IIII", &width, &height, &bpp, &flags);

    PyObject* ret = sgInit(width, height, bpp, flags) ? Py_True : Py_False;
    Py_INCREF(ret);
    return ret;
}

static PyObject* pysDeinit(PyObject* self)
{
    PyObject* ret = sgDeinit() ? Py_True : Py_False;
    Py_INCREF(ret);
    return ret;
}

static PyObject* pysRun(PyObject* self)
{
    return Py_BuildValue("i", sgRun());
}

static PyObject* pysLoop(PyObject* self)
{
    SGint code;
    PyObject* ret = sgLoop(&code) ? Py_True : Py_False;

    return Py_BuildValue("(Oi)", ret, code);
}

static PyObject* pysStop(PyObject* self, PyObject* args)
{
    int code;
    PyArg_ParseTuple(args, "i", &code);

    sgStop(code);
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject* pysGetTick(PyObject* self)
{
    return Py_BuildValue("K", sgGetTick());
}

static PyMethodDef CoreMethods[] = {
    {"loadModules", (PyCFunction)pysLoadModules, METH_VARARGS, ""},
    {"loadModule" , (PyCFunction)pysLoadModule , METH_VARARGS, ""},
    {"init"       , (PyCFunction)pysInit       , METH_VARARGS, ""},
    {"deinit"     , (PyCFunction)pysDeinit     , METH_NOARGS , ""},
    {"run"        , (PyCFunction)pysRun        , METH_NOARGS , ""},
    {"loop"       , (PyCFunction)pysLoop       , METH_NOARGS , ""},
    {"stop"       , (PyCFunction)pysStop       , METH_VARARGS, ""},
    {"getTick"    , (PyCFunction)pysGetTick    , METH_NOARGS , ""},
    {NULL, NULL, 0, NULL}
};

void sgpyInitCore(PyObject* parent)
{
    PyObject* core = Py_InitModule("siege.core", CoreMethods);
    if(parent != NULL)
        PyModule_AddObject(parent, "core", core);

    sgpyInitCoreEntity(core);
    sgpyInitCoreEvent(core);
    sgpyInitCoreWindow(core);
}

