package go.graphics.swing.contextcreator;

import org.lwjgl.BufferUtils;
import org.lwjgl.PointerBuffer;
import org.lwjgl.egl.EGL10;
import org.lwjgl.egl.EGL12;
import org.lwjgl.egl.EGL13;
import org.lwjgl.egl.EGL14;
import org.lwjgl.system.Platform;
import org.lwjgl.system.jawt.JAWTWin32DrawingSurfaceInfo;
import org.lwjgl.system.jawt.JAWTX11DrawingSurfaceInfo;

import java.nio.IntBuffer;

import go.graphics.swing.GLContainer;

public class EGLContextCreator extends JAWTContextCreator {

	private static long egl_config;
	private static long egl_display;
	private long egl_surface;
	private static long egl_context;

	private long native_drawable;

	public EGLContextCreator(GLContainer container) {
		super(container);
	}

	@Override
	public void stop() {
		EGL10.eglDestroySurface(egl_display, egl_surface);
		//EGL10.eglDestroyContext(egl_display, egl_context);
		//EGL10.eglTerminate(egl_display);
	}

	@Override
	protected void swapBuffers() {
		EGL10.eglSwapBuffers(egl_display, egl_surface);
	}

	@Override
	protected void makeCurrent(boolean draw) {
		if(draw) {
			EGL10.eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context);
		} else {
			EGL10.eglMakeCurrent(egl_display, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
		}
	}

	static {
		egl_display = EGL10.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY);
		EGL10.eglInitialize(egl_display, new int[1], new int[1]);
		EGL12.eglBindAPI(EGL14.EGL_OPENGL_API);


		int[] attrs = {EGL13.EGL_CONFORMANT, EGL14.EGL_OPENGL_BIT,
				EGL10.EGL_NONE};
		PointerBuffer cfgs = BufferUtils.createPointerBuffer(1);
		int[] num_config = new int[1];

		EGL10.eglChooseConfig(egl_display, attrs, cfgs, num_config);
		if(num_config[0] == 0) throw new Error("could not found egl configs!");
		egl_config = cfgs.get(0);

		int[] ctx_attrs = new int[] {EGL10.EGL_NONE};

		egl_context = EGL10.eglCreateContext(egl_display, egl_config, 0, ctx_attrs);

	}

	@Override
	protected void initContext() {
		/*egl_display = EGL10.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY);
		EGL10.eglInitialize(egl_display, new int[1], new int[1]);
		EGL12.eglBindAPI(EGL14.EGL_OPENGL_API);


		int[] attrs = {EGL13.EGL_CONFORMANT, EGL14.EGL_OPENGL_BIT,
				EGL10.EGL_NONE};
		PointerBuffer cfgs = BufferUtils.createPointerBuffer(1);
		int[] num_config = new int[1];

		EGL10.eglChooseConfig(egl_display, attrs, cfgs, num_config);
		if(num_config[0] == 0) throw new Error("could not found egl configs!");
		egl_config = cfgs.get(0);*/

		egl_surface = EGL10.eglCreateWindowSurface(egl_display, egl_config, native_drawable, (IntBuffer)null);

		/*int[] ctx_attrs = new int[] {EGL10.EGL_NONE};

		egl_context = EGL10.eglCreateContext(egl_display, egl_config, 0, ctx_attrs);*/
	}

	@Override
	protected void createNewSurfaceInfo() {
		if(Platform.get() == Platform.LINUX) {
			JAWTX11DrawingSurfaceInfo x11surfaceInfo = JAWTX11DrawingSurfaceInfo.create(surfaceinfo.platformInfo());
			native_drawable = x11surfaceInfo.drawable();
		} else if(Platform.get() == Platform.WINDOWS) {
			JAWTWin32DrawingSurfaceInfo win32surfaceInfo = JAWTWin32DrawingSurfaceInfo.create(surfaceinfo.platformInfo());
			native_drawable = win32surfaceInfo.hwnd();
		}
	}
}

@@ -813,100 +813,8 @@
 e);%0A
-%09%09//EGL10.eglDestroyContext(egl_display, egl_context);%0A%09%09//EGL10.eglTerminate(egl_display);%0A
 %09%7D%0A%0A
@@ -1879,756 +1879,110 @@
 %7B%0A%09%09
-/*egl_display = EGL10.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY);%0A%09%09EGL10.eglInitialize(egl_display, new int%5B1%5D, new int%5B1%5D);%0A%09%09EGL12.eglBindAPI(EGL14.EGL_OPENGL_API);%0A%0A%0A%09%09int%5B%5D attrs = %7BEGL13.EGL_CONFORMANT, EGL14.EGL_OPENGL_BIT,%0A%09%09%09%09EGL10.EGL_NONE%7D;%0A%09%09PointerBuffer cfgs = BufferUtils.createPointerBuffer(1);%0A%09%09int%5B%5D num_config = new int%5B1%5D;%0A%0A%09%09EGL10.eglChooseConfig(egl_display, attrs, cfgs, num_config);%0A%09%09if(num_config%5B0%5D == 0) throw new Error(%22could not found egl configs!%22);%0A%09%09egl_config = cfgs.get(0);*/%0A%0A%09%09egl_surface = EGL10.eglCreateWindowSurface(egl_display, egl_config, native_drawable, (IntBuffer)null);%0A%0A%09%09/*int%5B%5D ctx_attrs = new int%5B%5D %7BEGL10.EGL_NONE%7D;%0A%0A%09%09egl_context = EGL10.eglCreateContext(egl_display, egl_config, 0, ctx_attrs);*/
+egl_surface = EGL10.eglCreateWindowSurface(egl_display, egl_config, native_drawable, (IntBuffer)null);
 %0A%09%7D%0A

remove unused code