package com.view.coverflow;

import java.io.File;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import net.HttpActionManager;

import com.view.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.opengl.GLU;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

public class CoverFlow extends Activity {
	private static final int SWIPE_MIN_DISTANCE = 120;
	public static int downX = 0;
	public static int downY = 0;
	Advance advance;
	private HttpActionManager mManager;

	private List<Integer> event_ids=new ArrayList<Integer>();
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new GLCoverFlowView(this));
		
		/*
		 *  the following statements is for getting events info and pictures,
		 *  which is passed by the parent intent as an int arraylist,
		 *  using bundle param key "EVENT_ID" 
		 */
//		 Bundle b=this.getIntent().getExtras();
//		 event_ids=b.getIntegerArrayList("EVENT_ID");
		 
		 //.... To connnect to server to get event info and pictures

	}

	class GLCoverFlowView extends GLBase {

		//String ImagePath = "/sdcard/covers";
		String ImagePath="/res/drawable";
		float lightAmbient[] = new float[] { 0.2f, 0.2f, 0.2f, 1.0f };
		float lightDiffuse[] = new float[] { 1f, 1f, 1f, 1.0f };
		float[] lightPos = new float[] { 0, 0, 3, 1 };

		float matAmbient[] = new float[] { 1f, 1f, 1f, 1.0f };
		float matDiffuse[] = new float[] { 1f, 1f, 1f, 1.0f };

		int tex;
		Bitmap bmp;

		float box[] = new float[] { -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f,
				-1.0f, 0.5f, 0.0f, 1.0f, 0.5f, 0.0f, };
		float texCoords[] = new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
				1.0f, 1.0f, };
		int intCoords[] = new int[] { 0, 0, 1, 0, 0, 1, 1, 1, };
		FloatBuffer cubeBuff;
		FloatBuffer texBuff;
		IntBuffer inBuff;

		float Z_NEAR = 1.0f;
		float Z_FAR = 50.0f;
		float FOVY = 20.0f;
		List<Cover> covers = new ArrayList<Cover>();

		public GLCoverFlowView(Context c) {
			super(c, 20);
			cubeBuff = makeFloatBuffer(box);
			texBuff = makeFloatBuffer(texCoords);
			inBuff = makeFloatBuffer(intCoords);
		}

		public void step(GL10 gl) {

			float afLightDiffuse[] = { 0.76f, 0.75f, 0.65f, 1.0f };

			// set some GL-states
			gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
			// gl.glClearColor(1.0f, 0.5f, 0.25f, 1.0f);
			// gl.glColor4f(0.25f, 0.5f, 1.0f, 1.0f);
			gl.glEnable(GL10.GL_TEXTURE_2D);
			// gl.glEnable(GL_TEXTURE_RECTANGLE_ARB)
			gl.glEnable(GL10.GL_BLEND);
			gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
			gl.glLightModelf(GL10.GL_LIGHT_MODEL_TWO_SIDE, 1);
			gl.glDisable(GL10.GL_DEPTH_TEST);

			// lights and so on */
			gl.glDisable(GL10.GL_LIGHTING);
			gl.glDisable(GL10.GL_LIGHT0);
			gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, afLightDiffuse, 0);

			gl.glVertexPointer(3, GL10.GL_FLOAT, 0, cubeBuff);
			gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
			gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuff);
			// gl.glMultiTexCoord4x(GL10.GL_TEXTURE_2D, 0, 0, 3, 1);
			gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

			// setup "camera" */
			gl.glMatrixMode(GL10.GL_MODELVIEW);
			gl.glLoadIdentity();
			GLU.gluLookAt(gl, 0.0f, 0.0f, Z_NEAR, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
					0.0f);
			gl.glTranslatef(0.0f, 0.0f, -Z_NEAR);

		}

		protected void init(GL10 gl) {
			/**
			 * float afLightDiffuse[] ={0.76f, 0.75f, 0.65f, 1.0f};
			 * 
			 * // set some GL-states gl.glClearColor(1.0f, 0.5f, 0.25f, 1.0f);
			 * gl.glColor4f(0.25f, 0.5f, 1.0f, 1.0f);
			 * gl.glEnable(GL10.GL_TEXTURE_2D);
			 * //gl.glEnable(GL_TEXTURE_RECTANGLE_ARB)
			 * gl.glEnable(GL10.GL_BLEND); gl.glBlendFunc(GL10.GL_SRC_ALPHA,
			 * GL10.GL_ONE_MINUS_SRC_ALPHA);
			 * gl.glLightModelf(GL10.GL_LIGHT_MODEL_TWO_SIDE, 1);
			 * gl.glDisable(GL10.GL_DEPTH_TEST);
			 * 
			 * //glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
			 * //lights and so on gl.glDisable(GL10.GL_LIGHTING);
			 * gl.glDisable(GL10.GL_LIGHT0); gl.glLightfv(GL10.GL_LIGHT0,
			 * GL10.GL_DIFFUSE, afLightDiffuse,0);
			 * 
			 * //setup "camera" gl.glMatrixMode(GL10.GL_MODELVIEW);
			 * gl.glLoadIdentity(); GLU.gluLookAt(gl, 0.0f, 0.0f, Z_NEAR, 0.0f,
			 * 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); gl.glTranslatef(0.0f, 0.0f,
			 * -Z_NEAR);
			 */
			this.step(gl);

			loadImage(gl);
			Log.v("CoverFlow", "load Image init OK....");

		}

		void loadImage(GL10 gl) {
/*			File file = new File(ImagePath);
			String[] fileList = file.list();
			String path = file.getPath();
*/			List<Float> tracks = new ArrayList<Float>();
			for (int i = 0; i < /*fileList.length8*/6; i++) {
				
				//String fname = path + "/" + fileList[i];
				
				//need overriding!!!!!!!
				int rid=R.drawable.bg2+i;
				Cover cover = new Cover(gl, 2, getResources(),rid, -70);
				float x = i * 0.75f;
				cover.fX.setVal(x);
				covers.add(cover);
				tracks.add(x);
			}
			covers.get(0).focus();
			advance = new Advance(tracks, covers);
		}

		@Override
		protected void drawFrame(GL10 gl) {

			gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
			gl.glMatrixMode(GL10.GL_MODELVIEW);
			gl.glLoadIdentity();
			GLU.gluLookAt(gl, 0, 0, 3, 0, 0, 0, 0, 1, 0); // ÊÓ½Ç

			// Cover.display(mMeasuredWidth, mMeasuredHeight, gl, covers);
			Cover.display(80, 60, gl, covers);
		}

	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {

		switch (keyCode) {
		case KeyEvent.KEYCODE_DPAD_LEFT:
			advance.setAdvance(false);
			break;

		case KeyEvent.KEYCODE_DPAD_RIGHT:
			advance.setAdvance(true);
			break;
		}
		return super.onKeyDown(keyCode, event);
	}
}