/*
 * UPIWIN - Micro Pi Windowing Framework Kernel
 * Copyright (C) 2019 Amy Bowersox/Erbosoft Metaverse Design Solutions
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *-------------------------------------------------------------------------
 */
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "scode.h"
#include "fbprimitive.h"
#include "ep_init.h"
#include "ep_util.h"

static PyObject *do_setpixel(PyObject *self, PyObject *args)
{
  INT32 x, y;
  UINT16 color, oldcolor;
  BOOL xor;

  if (!PyArg_ParseTuple(args, "iiHi", &x, &y, &color, &xor))
    return NULL;
  oldcolor = Fb_setpixel(x, y, color, xor);
  return PyLong_FromLong((long)oldcolor);
}

static PyObject *do_line(PyObject *self, PyObject *args)
{
  INT32 x1, y1, x2, y2;
  UINT16 color;
  BOOL xor;

  if (!PyArg_ParseTuple(args, "iiiiHi", &x1, &y1, &x2, &y2, &color, &xor))
    return NULL;
  Fb_line(x1, y1, x2, y2, color, xor);
  Py_RETURN_NONE;
}

static PyObject *do_rectangle(PyObject *self, PyObject *args)
{
  INT32 x1, y1, x2, y2;
  UINT16 color;
  BOOL xor;

  if (!PyArg_ParseTuple(args, "iiiiHi", &x1, &y1, &x2, &y2, &color, &xor))
    return NULL;
  Fb_rectangle(x1, y1, x2, y2, color, xor);
  Py_RETURN_NONE;
}

static PyObject *do_filled_rectangle(PyObject *self, PyObject *args)
{
  INT32 x1, y1, x2, y2;
  UINT16 color;
  BOOL xor;

  if (!PyArg_ParseTuple(args, "iiiiHi", &x1, &y1, &x2, &y2, &color, &xor))
    return NULL;
  Fb_filled_rectangle(x1, y1, x2, y2, color, xor);
  Py_RETURN_NONE;
}

static PyObject *do_textout(PyObject *self, PyObject *args)
{
  INT32 x, y;
  PSTR text;

  if (!PyArg_ParseTuple(args, "iis", &x, &y, &text))
    return NULL;
  Fb_textout(x, y, text);
  Py_RETURN_NONE;
}

static PyMethodDef UPIWIN_tmpMethods[] = {
  {"setpixel", do_setpixel, METH_VARARGS, "Set a single pixel on the display."},
  {"line", do_line, METH_VARARGS, "Draw a line on the display."},
  {"rectangle", do_rectangle, METH_VARARGS, "Draw a rectangle on the display."},
  {"filled_rectangle", do_filled_rectangle, METH_VARARGS, "Draw a filled rectangle on the display."},
  {"textout", do_textout, METH_VARARGS, "Draw text on the display."},
  {NULL, NULL, 0, NULL}
};

static PyModuleDef DefUPIWIN_tmp = {
  PyModuleDef_HEAD_INIT,   /* standard garbage */
  MOD_NAME_UPIWIN_TMP,     /* module name */
  NULL,                    /* no doc string */
  -1,                      /* no per-module memory */
  UPIWIN_tmpMethods,       /* method defs */
  NULL,                    /* no slots for multi-phase init */
  NULL,                    /* no traversal proc */
  NULL,                    /* no clear function */
  NULL                     /* no free function */
};

BEGIN_CONSTANT_TABLE(UPIWIN_tmpConstants)
  /* primitive color values */
  CONSTANT_INT_MACRO(FBPRIMCLR_BLACK)
  CONSTANT_INT_MACRO(FBPRIMCLR_RED)
  CONSTANT_INT_MACRO(FBPRIMCLR_GREEN)
  CONSTANT_INT_MACRO(FBPRIMCLR_BLUE)
  CONSTANT_INT_MACRO(FBPRIMCLR_YELLOW)
  CONSTANT_INT_MACRO(FBPRIMCLR_CYAN)
  CONSTANT_INT_MACRO(FBPRIMCLR_MAGENTA)
  CONSTANT_INT_MACRO(FBPRIMCLR_WHITE)
END_CONSTANT_TABLE

PyObject *Epython_init_upiwin_tmp_module(void)
{
  PyObject *module;

  module = PyModule_Create(&DefUPIWIN_tmp);
  if (module)
  {
    if (FAILED(Epython_register_constants(module, UPIWIN_tmpConstants)))
    {
      Py_DECREF(module);
      module = NULL;
    }
  }
  return module;
}
