package jycessing;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

import org.python.core.Py;
import org.python.core.PyException;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.core.PyStringMap;
import org.python.util.InteractiveConsole;

import processing.core.PApplet;

public class Runner {
	private static String wrap(final Reader r) throws IOException {
		final BufferedReader reader = new BufferedReader(r);
		final StringBuilder sb = new StringBuilder(1024);
		String line;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line).append("\n");
			}
			//			sb.append("Test().runSketch()");
			return sb.toString();
		} finally {
			reader.close();
		}
	}

	public static class PSM extends PyStringMap {
		@Override
		public PyObject __finditem__(final String key) {
			System.err.println("finditem: " + key);
			return super.__finditem__(key);
		}

	}

	public static void main(final String[] args) throws Exception {
		final String pathname = "kinetictype.py";
		final String text = wrap(new FileReader(pathname));

		final InteractiveConsole interp = new InteractiveConsole(new PSM());
		final PyStringMap locals = (PyStringMap)interp.getLocals();
		final String path = new File(pathname).getCanonicalFile().getParent();
		Py.getSystemState().path.insert(0, new PyString(path));
		try {
			locals.__setitem__(new PyString("__file__"), new PyString(pathname));
			interp.exec(text);
		} catch (Throwable t) {
			Py.printException(t);
			interp.cleanup();
			System.exit(-1);
		}

		final PyObject setup = locals.__finditem__("setup");
		final PyObject draw = locals.__finditem__("draw");
		final PApplet applet = new PApplet() {
			@Override
			public void setup() {
				if (setup != null) {
					try {
						setup.__call__();
					} catch (PyException e) {
						if (e.getCause() instanceof RendererChangeException) {
							throw (RendererChangeException)e.getCause();
						}
					}
				}
			}

			@Override
			public void draw() {
				if (draw == null) {
					super.draw();
				} else {
					draw.__call__();
				}
			}
		};
		locals.__setitem__("P3D", new PyString(PApplet.P3D));
		locals.__setitem__("size", new PyObject() {
			@Override
			public PyObject __call__(final PyObject arg0, final PyObject arg1,
					final PyObject arg2) {
				applet.size(arg0.asInt(), arg1.asInt(), arg2.asString());
				return Py.None;
			}
		});
		PApplet.runSketch(new String[] { "Test" }, applet);
	}
}

@@ -203,24 +203,125 @@
 yException;%0A
+import org.python.core.PyFloat;%0Aimport org.python.core.PyInteger;%0Aimport org.python.core.PyJavaType;%0A
 import org.p
@@ -341,16 +341,16 @@
 Object;%0A
-
 import o
@@ -410,16 +410,47 @@
 ingMap;%0A
+import org.python.core.PyType;%0A
 import o
@@ -485,16 +485,16 @@
 nsole;%0A%0A
-
 import p
@@ -516,16 +516,47 @@
 PApplet;
+%0Aimport processing.core.PImage;
 %0A%0Apublic
@@ -2640,16 +2640,701 @@
 %7D%0A%09%09%7D);%0A
+%09%09locals.__setitem__(%22background%22, new PyObject() %7B%0A%09%09%09@Override%0A%09%09%09public PyObject __call__(final PyObject arg0) %7B%0A%09%09%09%09final PyType t = arg0.getType();%0A%09%09%09%09if (t == PyInteger.TYPE) %7B%0A%09%09%09%09%09applet.background(arg0.asInt());%0A%09%09%09%09%7D else if (t == PyFloat.TYPE) %7B%0A%09%09%09%09%09applet.background((float)arg0.asDouble());%0A%09%09%09%09%7D else if (t == PyJavaType.TYPE) %7B%0A%09%09%09%09%09applet.background((PImage)arg0.__tojava__(PImage.class));%0A%09%09%09%09%7D%0A%09%09%09%09return Py.None;%0A%09%09%09%7D%0A%0A%09%09%09@Override%0A%09%09%09public PyObject __call__(final PyObject arg0, final PyObject arg1,%0A%09%09%09%09%09final PyObject arg2) %7B%0A%09%09%09%09applet.background((float)arg0.asDouble(), (float)arg1.asDouble(), (float)arg2%0A%09%09%09%09%09%09.asDouble());%0A%09%09%09%09return Py.None;%0A%09%09%09%7D%0A%09%09%7D);%0A%0A
 %09%09PApple

add background() methods