package de.tudarmstadt.informatik.hostage.ui2.fragment.opengl;

import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

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

import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;

import android.opengl.GLES20;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLUtils;
import android.opengl.Matrix;
import android.util.Log;

/**
 * @author Fabio Arnold
 * 
 * ThreatIndicatorGLRenderer
 * This class is responsible for drawing an animation representing the current threat level.
 * Use the method setThreatLevel to set the state (0 to 3).
 */

public class ThreatIndicatorGLRenderer implements Renderer {
	public enum ThreatLevel {
		NOT_MONITORING,
		NO_THREAT,
		PAST_THREAT,
		LIVE_THREAT
	}
	
	/**
	 * Set the threat level which should be indicated
	 * @param threatLevel
	 */
	public static void setThreatLevel(ThreatLevel threatLevel) {
		mNextThreatLevel = threatLevel;
	}

	/**
	 * Match the background color of the view holding this renderer
	 * @param color 32 bit integer encoding the color
	 */
	public static void setBackgroundColor(int color) {
		mBackgroundColor[0] = (float)Color.red(color) / 255.0f;
		mBackgroundColor[1] = (float)Color.green(color) / 255.0f;
		mBackgroundColor[2] = (float)Color.blue(color) / 255.0f;
	}
	private static float[] mBackgroundColor = new float[3];
	
	public static AssetManager assets = null; // needs to be set by the Activity using this class

	// OpenGL data
	private int mProgram;
	private float [] mModelview;
	private float [] mProjection;
	private float [] mMVP;
	
	private AnimatedMesh androidMesh = null;
	private AnimatedMesh beeMesh = null;
	private int androidTexture;
	private int beeTexture;
	
	// threat state
	private static ThreatLevel mNextThreatLevel = ThreatLevel.NO_THREAT;
	
	private ThreatLevel mCurrentThreatLevel = ThreatLevel.NO_THREAT;
	private ThreatLevel mTargetThreatLevel = ThreatLevel.NO_THREAT;
	private float mThreatLevelTransition = 1.0f; // 1.0 means transition is complete

	private static boolean sPlayGreetingAnimation = true; // greet the first time

	private long mStartTimeMillis; // for animation
	public ThreatIndicatorGLRenderer() {
		mStartTimeMillis = System.currentTimeMillis();
	}
	
	/**
	 * Initialization will be called after GL context is created and is current
	 */
	public void onSurfaceCreated(GL10 arg0, EGLConfig arg1) {
		GLES20.glClearColor(mBackgroundColor[0], mBackgroundColor[1], mBackgroundColor[2], 1.0f);
		GLES20.glEnable(GLES20.GL_DEPTH_TEST);
		GLES20.glEnable(GLES20.GL_CULL_FACE);
		GLES20.glEnable(GLES20.GL_TEXTURE_2D);
		
		try {
			InputStream is = assets.open("meshes/android.amh");
			androidMesh = new AnimatedMesh(is);
			/* play the greeting animation the first time the gets opened
			   not each time the threatindicator gets created */
			if (sPlayGreetingAnimation) {
				androidMesh.startAction("greet", false, false);
				sPlayGreetingAnimation = false;
			} else if (mCurrentThreatLevel == ThreatLevel.NO_THREAT) { // default state
				androidMesh.startAction("happy", true, false); // play NO_THREAT animation
			}
		} catch (IOException e) {
			Log.e("gl", "Couldn't open android mesh");
		}
		androidTexture = loadTexture("textures/android-tex.png");
		try {
			InputStream is = assets.open("meshes/bee.amh");
			beeMesh = new AnimatedMesh(is);
			beeMesh.startAction("bee_armatureAct", true, false);
		} catch (IOException e) {
			Log.e("gl", "Couldn't open bee mesh");
		}
		beeTexture = loadTexture("textures/bee-tex.png");
		
		mModelview = new float[16];
		Matrix.setIdentityM(mModelview, 0);
		mProjection = new float[16];
		mMVP = new float[16];
		
		// default shader
		String vertexSource = "attribute vec3 position; void main() {gl_Position = vec4(position, 1.0);}";
		String fragmentSource = "void main() {gl_FragColor = vec4(1.0, 0.0, 1.0, 1.0);}";
		try {
			vertexSource = inputStreamToString(assets.open("shaders/skinned.vert"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {fragmentSource = inputStreamToString(assets.open("shaders/skinned.frag"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		mProgram = loadProgram(vertexSource, fragmentSource);
	}
	
	/**
	 * Tries to render at 30 Hz (see bottom part)
	 */
	public void onDrawFrame(GL10 arg0) {
		long timeMillis = System.currentTimeMillis() - mStartTimeMillis;
		double animTime = 0.001 * (double)timeMillis; // in seconds
		
		// threat level state machine
		if (mTargetThreatLevel != mCurrentThreatLevel) {
			boolean blocked = false; // block until current action is completed
			if (mThreatLevelTransition == 0.0f) {
				if (androidMesh.isActionDone()) {
					switch (mTargetThreatLevel) {
					case NOT_MONITORING:
						androidMesh.startAction("sleep", false, false);
						break;
					case NO_THREAT:
						androidMesh.startAction("happy", true, false);
						break;
					case PAST_THREAT:
						androidMesh.startAction("fear", true, false);
						break;
					case LIVE_THREAT:
						androidMesh.startAction("panic", true, false);
						break;
					}
				} else blocked = true;
			}
			
			if (!blocked) {
				mThreatLevelTransition += 0.016f;
				if (mThreatLevelTransition >= 1.0f) {
					mCurrentThreatLevel = mTargetThreatLevel;
					mThreatLevelTransition = 1.0f;
				}
			}
		} else {
			if (mNextThreatLevel != mTargetThreatLevel) {
				mTargetThreatLevel = mNextThreatLevel;
				mThreatLevelTransition = 0.0f;
				
				// HACK!!! reverses the sleep animation to create smooth transition into other states
				if (mCurrentThreatLevel == ThreatLevel.NOT_MONITORING)
					androidMesh.startAction("sleep", false, true);
			}
		}
		
		androidMesh.tick(); // animate android
		
		// OpenGL drawing
		GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
		
		GLES20.glUseProgram(mProgram);
		int colorUniformLoc = GLES20.glGetUniformLocation(mProgram, "color");
		int textureUniformLoc = GLES20.glGetUniformLocation(mProgram, "texture");
		int mvpUniformLoc = GLES20.glGetUniformLocation(mProgram, "mvp");

		//  animate color
		final float[] whiteColor = {1.0f, 1.0f, 1.0f, 1.0f};
		final float[] greyColor = {0.5f, 0.5f, 0.5f, 1.0f};
		final float[] redColor = {2.0f, 0.4f, 0.2f, 1.0f};
		final float[] yellowColor = {1.1f * 255.0f / 166.0f, 1.2f * 255.0f / 200.0f, 0.0f, 1.0f};
		
		float[] currentColor = whiteColor;
		float blink = 0.5f + 0.5f * (float)Math.sin(12.0 * animTime);
		switch (mCurrentThreatLevel) {
		case NOT_MONITORING:
			currentColor = greyColor;
			break;
		case PAST_THREAT:
			currentColor = mixColor(blink, whiteColor, yellowColor);
			break;
		case LIVE_THREAT:
			currentColor = mixColor(blink, whiteColor, redColor);
			break;
		}
		if (mTargetThreatLevel != mCurrentThreatLevel) {
			float[] targetColor = whiteColor;
			switch (mTargetThreatLevel) {
			case NOT_MONITORING:
				targetColor = greyColor;
				break;
			case PAST_THREAT:
				targetColor = mixColor(blink, whiteColor, yellowColor);
				break;
			case LIVE_THREAT:
				targetColor = mixColor(blink, whiteColor, redColor);
				break;
			}
			currentColor = mixColor(mThreatLevelTransition, currentColor, targetColor);
		}
		GLES20.glUniform4fv(colorUniformLoc, 1, currentColor, 0);
		GLES20.glUniform1i(textureUniformLoc, 0);
		
		// animate camera
		Matrix.setIdentityM(mModelview, 0);
		if (mCurrentThreatLevel == ThreatLevel.LIVE_THREAT || mTargetThreatLevel == ThreatLevel.LIVE_THREAT) {
			float delta = 1.0f;
			if (mThreatLevelTransition < 0.4f) { // animate only during the first 40% of the transition
				delta = mThreatLevelTransition / 0.4f;
				delta = -2.0f * delta * delta * delta + 3.0f * delta * delta; // ease in/out
			}
			if (mTargetThreatLevel != ThreatLevel.LIVE_THREAT)
				delta = 1.0f - delta;
			Matrix.translateM(mModelview, 0, 0.0f, -0.6f - 0.2f * delta, -1.6f - 0.4f * delta); // 0.0f, -0.8f, -2.0f
			Matrix.rotateM(mModelview, 0, -85.0f + 5.0f * delta, 1.0f, 0.0f, 0.0f); // -80.0f
			
		} else {
			Matrix.translateM(mModelview, 0, 0.0f, -0.6f, -1.6f);
			Matrix.rotateM(mModelview, 0, -85.0f, 1.0f, 0.0f, 0.0f);
		}
		Matrix.multiplyMM(mMVP, 0, mProjection, 0, mModelview, 0);
		GLES20.glUniformMatrix4fv(mvpUniformLoc, 1, false, mMVP, 0);
		
		GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, androidTexture);
		androidMesh.draw(mProgram);
		
		// restore color
		GLES20.glUniform4fv(colorUniformLoc, 1, whiteColor, 0);

		if (mCurrentThreatLevel == ThreatLevel.LIVE_THREAT || mTargetThreatLevel == ThreatLevel.LIVE_THREAT) {
			// draw a bee rotating around the android
			
			float fadeIn = mThreatLevelTransition;
			if (mTargetThreatLevel != ThreatLevel.LIVE_THREAT) fadeIn = 1.0f - fadeIn; // fade out
			float beePositionZ = 2.0f * (1.0f - fadeIn) * (1.0f - fadeIn); // animate the bee going in/out
			
			final float beeSize = 0.2f;
			Matrix.rotateM(mModelview, 0, (float)((-240.0 * animTime) % 360.0), 0.0f, 0.0f, 1.0f); // rotate around android
			Matrix.translateM(mModelview, 0, 0.6f, 0.0f, 0.7f + 0.1f * (float)Math.sin(12.0 * animTime) + beePositionZ); // go up and down
			Matrix.rotateM(mModelview, 0, 20.0f * (float)Math.cos(12.0 * animTime), 1.0f, 0.0f, 0.0f); // rock back and forth
			Matrix.scaleM(mModelview, 0, beeSize, beeSize, beeSize);
			Matrix.multiplyMM(mMVP, 0, mProjection, 0, mModelview, 0);
			GLES20.glUniformMatrix4fv(mvpUniformLoc, 1, false, mMVP, 0);
	
			GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, beeTexture);
			beeMesh.tick();
			beeMesh.draw(mProgram);
		}
		
		long deltaTime = System.currentTimeMillis() - mStartTimeMillis - timeMillis; // time for one frame
		if (deltaTime < 33) {
			try {
				Thread.sleep(33 - deltaTime); // sleep remaining time for 30 Hz
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * Informs renderer of changed surface dimensions
	 */
	public void onSurfaceChanged(GL10 arg0, int w, int h) {
		int width = w;
		int height = h;
		float aspectRatio = (float) w / (float) h;
		//Matrix.orthoM(mProjection, 0, -aspectRatio, aspectRatio, -1.0f, 1.0f, -1.0f, 1.0f);
		float near = 0.1f;
		float fov = 2.0f;
		Matrix.frustumM(mProjection, 0, near * -aspectRatio, near * aspectRatio, -near, near, fov * near, 100.0f);
		GLES20.glViewport(0, 0, width, height);
	}
	
	// some private helper methods
	
	private float[] mixColor(float alpha, float[] color1, float[] color2) {
		float[] color3 = new float[4];
		color3[0] = (1.0f - alpha) * color1[0] + alpha * color2[0];
		color3[1] = (1.0f - alpha) * color1[1] + alpha * color2[1];
		color3[2] = (1.0f - alpha) * color1[2] + alpha * color2[2];
		color3[3] = (1.0f - alpha) * color1[3] + alpha * color2[3];
		return color3;
	}
	
	private int loadTexture(String filePath) {
		Bitmap bitmap;
		try {
			bitmap = BitmapFactory.decodeStream(assets.open(filePath));
		} catch (IOException e) {
			e.printStackTrace();
			return 0;
		}
		
		int[] names = {0};
		GLES20.glGenTextures(1, names, 0);
		int tex = names[0];
		GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tex);

		GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
		GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
		GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
		bitmap.recycle(); // memory is now gpu -> free it
		
		GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tex);
		
		return tex;
	}
	
	// see http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string
	private static String inputStreamToString(InputStream is) {
	    Scanner scanner = new Scanner(is);
		Scanner s = scanner.useDelimiter("\\A");
	    String result = s.hasNext() ? s.next() : "";
	    scanner.close();
	    return result;
	}
	
	private static int loadShader(int type, String source) {
		int shader = GLES20.glCreateShader(type);
		GLES20.glShaderSource(shader, source);
		GLES20.glCompileShader(shader);
		Log.i("gl", GLES20.glGetShaderInfoLog(shader));
		return shader;
	}
	
	private static int loadProgram(String vertexSource, String fragmentSource) {
		int program = GLES20.glCreateProgram();
		GLES20.glAttachShader(program, loadShader(GLES20.GL_VERTEX_SHADER, vertexSource));
		GLES20.glAttachShader(program, loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource));
		GLES20.glLinkProgram(program);
		return program;
	}
}
