package com.google.ar.core.examples.java.common.framework.RWT;

import android.opengl.GLES20;
import android.opengl.GLSurfaceView.Renderer;

import com.google.ar.core.Camera;
import com.google.ar.core.Frame;
import com.google.ar.core.Session;
import com.google.ar.core.examples.java.common.framework.model3D.Universe;
import com.google.ar.core.examples.java.common.framework.view3D.Viewer3D;
import com.google.ar.core.examples.java.common.java3d.GraphicsContext3D;
import com.google.ar.core.examples.java.common.rendering.BackgroundRenderer;
import com.google.ar.core.examples.java.helloar.HelloArActivity;
import com.google.ar.core.exceptions.CameraNotAvailableException;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class RWTRenderer implements Renderer {
	protected Viewer3D viewer;
	protected Session session;
	protected BackgroundRenderer backgroundRenderer;
	protected HelloArActivity helloArActivity;
	protected Universe universe;
	protected GraphicsContext3D gc3D = null;

	public RWTRenderer(HelloArActivity helloArActivity, BackgroundRenderer backgroundRenderer, Universe universe) {
		super();
		this.universe = universe;
		this.helloArActivity = helloArActivity;
		this.backgroundRenderer = backgroundRenderer;
	}

	public void setSession(Session session) {
		this.session = session;
	}

//    public void setBackgroundRenderer(BackgroundRenderer backgroundRenderer) {
//		this.backgroundRenderer = backgroundRenderer;
//	}

//	public void attachCamera(Camera3D camera) {
//		// onSurfaceCreated()より先に呼ばれる
//		this.camera = camera;
//		viewer = new Viewer3D(camera);
//	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		if (viewer == null) {
			gc3D = new GraphicsContext3D(gl);
			viewer = new Viewer3D(gc3D);
		} else {
			gc3D.setGL10(gl);
		}
//		ArrayList<Light> lights = universe.getLights();
//		for (int i = 0; i < lights.size(); i++){
//			Light l = lights.get(i);
//			gc3D.setLight(l,i);
//		}

		helloArActivity.onSurfaceCreated(gl, config);
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		GLES20.glViewport(0, 0, width, height);

		if (viewer == null) {
			gc3D = new GraphicsContext3D(gl);
			viewer = new Viewer3D(gc3D);
		} else {
			gc3D.setGL10(gl);
		}
		viewer.surfaceChanged(width, height);

		helloArActivity.onSurfaceChanged(gl, width, height);
	}

	@Override
	public void onDrawFrame(GL10 gl) {
//		GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

		if(session == null) {
			return;
		}
		try {
			// Viewerの更新
			if (viewer == null) {
				gc3D = new GraphicsContext3D(gl);
				viewer = new Viewer3D(gc3D);
			} else {
				gc3D.setGL10(gl);
			}
			viewer.onDrawFrame();

			// Frameの取得
			session.setCameraTextureName(backgroundRenderer.getTextureId());
			Frame frame = session.update();
			// 背景（カメラ映像）の描画
			backgroundRenderer.draw(gl, frame);

			// 3Dモデルのレンダリング
			gc3D.pushMatrix();
			Camera camera = frame.getCamera();
			viewer.updateCamera(camera);
			universe.render(viewer);
			gc3D.popMatrix();
			helloArActivity.onDrawFrame(gl, frame, camera);
		} catch (CameraNotAvailableException e) {
			e.printStackTrace();
		}
	}
}
