/*
    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 "font.h"

static PyTypeObject PysFontType;

static void pysFontDealloc(PysFont* self)
{
    sgFontDestroy(self->font);
    self->ob_type->tp_free(self);
}
static PyObject* pysFontNew(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
    PysFont* self = (PysFont*)type->tp_alloc(type, 0);
    if(self != NULL)
        self->font = NULL;
    return (PyObject*)self;
}
static int pysFontInit(PysFont* self, PyObject* args, PyObject* kwargs)
{
    char* fname;
    float height;
    SGuint preload;
    if(!PyArg_ParseTuple(args, "sfI", &fname, &height, &preload));
        return -1;

    self->font = sgFontCreate(fname, height, preload);
    return 0;
}

static PyObject* pysFontResizeCopy(PysFont* self, PyObject* args)
{
    float height;
    if(!PyArg_ParseTuple(args, "f", &height))
        return NULL;

    PysFont* font = (PysFont*)pysFontNew(&PysFontType, NULL, NULL);
    font->font = sgFontResizeCopy(self->font, height);

    return (PyObject*)font;
}
static PyObject* pysFontResize(PysFont* self, PyObject* args)
{
    float height;
    if(!PyArg_ParseTuple(args, "f", &height))
        return NULL;

    SGFont* sgfont = sgFontResize(self->font, height);
    if(sgfont == self->font)
    {
        Py_INCREF(self);
        return (PyObject*)self;
    }

    PysFont* font = (PysFont*)pysFontNew(&PysFontType, NULL, NULL);
    font->font = sgfont;

    return (PyObject*)font;
}

static PyObject* pysFontPrint(PysFont* self, PyObject* args)
{
    size_t size = PyTuple_Size(args);
    if(size < 2)
        return NULL;
    size_t i;

    float x = PyFloat_AsDouble(PyTuple_GET_ITEM(args, 0));
    float y = PyFloat_AsDouble(PyTuple_GET_ITEM(args, 1));

    PyObject* concat = PyString_FromStringAndSize(NULL, 0);
    for(i = 2; i < size; i++)
    {
        PyObject* obj = PyTuple_GET_ITEM(args, i);

        PyObject* str = PyObject_Str(obj);
        if(str == NULL)
        {
            Py_DECREF(concat);
            return NULL;
        }

        if(PyString_Size(concat) != 0)
            PyString_ConcatAndDel(&concat, PyString_FromStringAndSize(" ", 1));
        PyString_ConcatAndDel(&concat, str);
    }

    char* cstr = PyString_AsString(concat);
    sgFontPrint(self->font, x, y, cstr);
    Py_DECREF(concat);
    Py_RETURN_NONE;
}

static PyObject* pysFontPrintCentered(PysFont* self, PyObject* args)
{
    size_t size = PyTuple_Size(args);
    if(size < 2)
        return NULL;
    size_t i;

    float x = PyFloat_AsDouble(PyTuple_GET_ITEM(args, 0));
    float y = PyFloat_AsDouble(PyTuple_GET_ITEM(args, 1));

    PyObject* concat = PyString_FromStringAndSize(NULL, 0);
    for(i = 2; i < size; i++)
    {
        PyObject* obj = PyTuple_GET_ITEM(args, i);

        PyObject* str = PyObject_Str(obj);
        if(str == NULL)
        {
            Py_DECREF(concat);
            return NULL;
        }

        if(PyString_Size(concat) != 0)
            PyString_ConcatAndDel(&concat, PyString_FromStringAndSize(" ", 1));
        PyString_ConcatAndDel(&concat, str);
    }

    char* cstr = PyString_AsString(concat);
    sgFontPrintCentered(self->font, x, y, cstr);
    Py_DECREF(concat);
    Py_RETURN_NONE;
}

static PyObject* pysFontPrintXCentered(PysFont* self, PyObject* args)
{
    size_t size = PyTuple_Size(args);
    if(size < 2)
        return NULL;
    size_t i;

    float x = PyFloat_AsDouble(PyTuple_GET_ITEM(args, 0));
    float y = PyFloat_AsDouble(PyTuple_GET_ITEM(args, 1));

    PyObject* concat = PyString_FromStringAndSize(NULL, 0);
    for(i = 2; i < size; i++)
    {
        PyObject* obj = PyTuple_GET_ITEM(args, i);

        PyObject* str = PyObject_Str(obj);
        if(str == NULL)
        {
            Py_DECREF(concat);
            return NULL;
        }

        if(PyString_Size(concat) != 0)
            PyString_ConcatAndDel(&concat, PyString_FromStringAndSize(" ", 1));
        PyString_ConcatAndDel(&concat, str);
    }

    char* cstr = PyString_AsString(concat);
    sgFontPrintXCentered(self->font, x, y, cstr);
    Py_DECREF(concat);
    Py_RETURN_NONE;
}

static PyObject* pysFontPrintYCentered(PysFont* self, PyObject* args)
{
    size_t size = PyTuple_Size(args);
    if(size < 2)
        return NULL;
    size_t i;

    float x = PyFloat_AsDouble(PyTuple_GET_ITEM(args, 0));
    float y = PyFloat_AsDouble(PyTuple_GET_ITEM(args, 1));

    PyObject* concat = PyString_FromStringAndSize(NULL, 0);
    for(i = 2; i < size; i++)
    {
        PyObject* obj = PyTuple_GET_ITEM(args, i);

        PyObject* str = PyObject_Str(obj);
        if(str == NULL)
        {
            Py_DECREF(concat);
            return NULL;
        }

        if(PyString_Size(concat) != 0)
            PyString_ConcatAndDel(&concat, PyString_FromStringAndSize(" ", 1));
        PyString_ConcatAndDel(&concat, str);
    }

    char* cstr = PyString_AsString(concat);
    sgFontPrintYCentered(self->font, x, y, cstr);
    Py_DECREF(concat);
    Py_RETURN_NONE;
}

static PyObject* pysFontStrSize(PysFont* self, PyObject* args)
{
    size_t size = PyTuple_Size(args);
    size_t i;

    PyObject* concat = PyString_FromStringAndSize(NULL, 0);
    for(i = 0; i < size; i++)
    {
        PyObject* obj = PyTuple_GET_ITEM(args, i);

        PyObject* str = PyObject_Str(obj);
        if(str == NULL)
        {
            Py_DECREF(concat);
            return NULL;
        }

        if(PyString_Size(concat) != 0)
            PyString_ConcatAndDel(&concat, PyString_FromStringAndSize(" ", 1));
        PyString_ConcatAndDel(&concat, str);
    }

    char* cstr = PyString_AsString(concat);
    float x, y;
    sgFontStrSize(self->font, &x, &y, cstr);
    Py_DECREF(concat);
    return Py_BuildValue("(ff)", x, y);
}

static PyMethodDef PysFontMethods[] = {
    {"resizeCopy", (PyCFunction)pysFontResizeCopy, METH_VARARGS, ""},
    {"resize", (PyCFunction)pysFontResize, METH_VARARGS, ""},

    {"print", (PyCFunction)pysFontPrint, METH_VARARGS, ""},

    {"printCentered", (PyCFunction)pysFontPrintCentered, METH_VARARGS, ""},

    {"printXCentered", (PyCFunction)pysFontPrintXCentered, METH_VARARGS, ""},

    {"printYCentered", (PyCFunction)pysFontPrintYCentered, METH_VARARGS, ""},

    {"strSize", (PyCFunction)pysFontStrSize, METH_VARARGS, ""},
    {NULL, NULL, 0, NULL}
};

static PyMemberDef PysFontMembers[] = {
    {NULL, 0, 0, 0, NULL}
};

static PyTypeObject PysFontType = {
    PyObject_HEAD_INIT(NULL)
    0,                         /*ob_size*/
    "siege.graphics.Font",   /*tp_name*/
    sizeof(PysFont),         /*tp_basicsize*/
    0,                         /*tp_itemsize*/
    (destructor)pysFontDealloc, /*tp_dealloc*/
    0,                         /*tp_print*/
    0,                         /*tp_getattr*/
    0,                         /*tp_setattr*/
    0,                         /*tp_compare*/
    0,                         /*tp_repr*/
    0,                         /*tp_as_number*/
    0,                         /*tp_as_sequence*/
    0,                         /*tp_as_mapping*/
    0,                         /*tp_hash */
    0,                         /*tp_call*/
    0,                         /*tp_str*/
    0,                         /*tp_getattro*/
    0,                         /*tp_setattro*/
    0,                         /*tp_as_buffer*/
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
    "",                        /* tp_doc */
    0,		                   /* tp_traverse */
    0,		                   /* tp_clear */
    0,		                   /* tp_richcompare */
    0,		                   /* tp_weaklistoffset */
    0,		                   /* tp_iter */
    0,		                   /* tp_iternext */
    PysFontMethods,          /* tp_methods */
    PysFontMembers,          /* tp_members */
    0,                         /* tp_getset */
    0,                         /* tp_base */
    0,                         /* tp_dict */
    0,                         /* tp_descr_get */
    0,                         /* tp_descr_set */
    0,                         /* tp_dictoffset */
    (initproc)pysFontInit,   /* tp_init */
    0,                         /* tp_alloc */
    pysFontNew,                 /* tp_new */
};

int pysFontCheck(PyObject* o)
{
    return PyObject_TypeCheck(o, &PysFontType);
}
void sgpyInitGraphicsFont(PyObject* parent)
{
    PysFontType.tp_new = PyType_GenericNew;
    if(PyType_Ready(&PysFontType) < 0)
        return;

    Py_INCREF(&PysFontType);
    PyModule_AddObject(parent, "Font", (PyObject*)&PysFontType);
}
