package com.view.coverflow;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import android.content.res.Resources;
import android.opengl.GLU;
import android.util.Log;

class Cover {
	
	static float SPREADIMAGE = 0.40f;// spread between images (screen measured from -1 to 1)
	static float FLANKSPREAD = 0.4f; // flank spread out; this is how much an
	// image moves way from center
	static float FRICTION = 10.0f;// friction
	static float MAXSPEED = 10.0f; // throttle speed to this value
	
	
	static Texture texture;
	static boolean current;
	static float Z_NEAR = 1.0f;
	static float Z_FAR  = 50.0f;
	static float FOVY   = 20.0f;
	static float scale ;
	
	static InterpolatedAnimator pValueCoverTrack, pValueCoverAlpha;
	InterpolatedAnimator fZ,fAngle,fAlpha;
	ConstantAnimator fX,fY;
	
	float z_base,z_focus,y_base,angle;
	float w,h,fWidth,fHeight;

	int width = 0;
	int height = 0;
	
	static{
	    // set some animation variables */
	    pValueCoverTrack = new InterpolatedAnimator(0.f,0.f,.5f,"sine");
	    pValueCoverAlpha = new InterpolatedAnimator(0.f,1.f,2.f,"tial");
	    current = false;
	}
	
	public Cover(GL10 gl,int fSize,String coverImagePath,int _angle){
		texture = new Texture(coverImagePath);
		texture.getTexture(gl);
		w = texture.getBWeight();
		h = texture.getBHeight();
		if(w>h){
			fWidth = fSize;
			fHeight = fSize*h/w;
		}
		else{
			fWidth = fSize * w / h;
			fHeight = fSize;
		}

		angle = _angle;
		z_base = -4;
		z_focus = -2;
		y_base = fHeight / 2 - 1;
	
		//retrun object ConstantAnimator
		fX = new ConstantAnimator(0);
		fY = new ConstantAnimator(0);
		fZ = new InterpolatedAnimator(z_base,z_base,.5f,"back");
		fAngle = new InterpolatedAnimator(angle,angle,.5f,"circ");
		fAlpha = pValueCoverAlpha;
		current = false;
	}	
	
	public Cover(GL10 gl,int fSize,Resources res,int rid,int _angle){
		texture = new Texture(res,rid);
		texture.getTexture(gl);
		w = texture.getBWeight();
		h = texture.getBHeight();
		if(w>h){
			fWidth = fSize;
			fHeight = fSize*h/w;
		}
		else{
			fWidth = fSize * w / h;
			fHeight = fSize;
		}

		angle = _angle;
		z_base = -4;
		z_focus = -2;
		y_base = fHeight / 2 - 1;
	
		//retrun object ConstantAnimator
		fX = new ConstantAnimator(0);
		fY = new ConstantAnimator(0);
		fZ = new InterpolatedAnimator(z_base,z_base,.5f,"back");
		fAngle = new InterpolatedAnimator(angle,angle,.5f,"circ");
		fAlpha = pValueCoverAlpha;
		current = false;
	}	
	
	void focus(){
		current = true;
		fAngle.setVal(0);
		fZ.setVal(z_focus);
	}
	
	void unfocus(int multiplier){
		current = false;
		fAngle.setVal(angle * multiplier);
		fZ.setVal(z_base);
	}
	
	void drawing(GL10 gl,float off){
		float m[] = new float[] { 
				0, 0, 0, 0, 
				0, 0, 0, 0, 
				0, 0, 0, 0, 
				0, 0, 0, 0, };

		m[10] = 1;
		m[15] = 1;
		m[0] = 1;
		m[5] = 1;
		float trans = off * SPREADIMAGE;

		float f = off * FLANKSPREAD;
		if (f < -FLANKSPREAD) {
			f = -FLANKSPREAD;
		} else if (f > FLANKSPREAD) {
			f = FLANKSPREAD;
		}
		m[3] = -f;
		m[0] = 1 - Math.abs(f);
		float sc = 0.95f * (1 - Math.abs(f));//0.45f * (1 - Math.abs(f));
		trans += f * 1;

		gl.glPushMatrix();
		// 设置平行投影的投影矩阵
		gl.glOrthof(-1f, 1f, -1f, 1f, -1f, 1f);
		//绑定纹理
		gl.glBindTexture(GL10.GL_TEXTURE_2D, texture.getTex());
		// 视图变换
		gl.glTranslatef(trans, 0f, 0f); 
		double _fAngle = fAngle.getVal();
		gl.glRotatef((float)_fAngle, 0.0f, 1.0f, 0.0f);
		// 模型变换（比例变换）
		gl.glScalef(sc, sc, 1.0f);

	
		gl.glMultMatrixf(m, 0);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

		// reflect
		gl.glTranslatef(0, -2, 0);
		gl.glScalef(1, -1, 1);
		gl.glColor4f(0.5f, 0.5f, 0.5f, 1.0f);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
		gl.glColor4f(1, 1, 1, 1);

		gl.glPopMatrix();
	}
	
	void draw(GL10 gl){
		float _fAngle = fAngle.getVal();
		//float _fAlpha = fAlpha.getVal();
		float _fAlpha = 1.0f;
		//Log.v("=========cover-", String.valueOf(this));
		Log.v("=========fAlpha", String.valueOf(fAlpha.getVal()));
		Log.v("=========fAngle", String.valueOf(fAngle.getVal()));
		float _fX = fX.getVal();
		float _fY = fY.getVal();
		float _fZ = fZ.getVal();
		Log.v("============x", String.valueOf(_fX));
		Log.v("============y", String.valueOf(_fY));
		Log.v("============z", String.valueOf(_fZ));
		Log.v("textureId=============", String.valueOf(texture.getTex()));
		//double fOffset =this.fHeight;
		gl.glBindTexture (GL10.GL_TEXTURE_2D, texture.getTex());
		 // draw cover and its reflection
		gl.glPushMatrix();	
	    gl.glTranslatef(_fX,_fY,_fZ);
	    gl.glRotatef(_fAngle, 0.0f, 1.0f, 0.0f);
	    /**
	    float sc = 0.45f * (1 - Math.abs(scale));;
	    gl.glScalef(sc, sc, 1.0f);
	    */
	    gl.glColor4f(_fAlpha, _fAlpha, _fAlpha, _fAlpha);
	    /**
	    float texCoords[] = new float[]{
	    		0f,0f,
	    		0f,0f,
	    		1f,0f,
	    		1f,1f,
	    };
	    
	   
	    float vertex[] = new float[]{
	    		-1.0f, -1.0f, 0.0f,
	    		-1.0f, 1.0f, 0.0f,
	    		 1.0f, 1.0f, 0.0f,
	    		 1.0f, -1.0f, 0.0f,
	    };
	    
	    */
	    
	    float texCoords[] = new float[]{
	    		0.0f, 0.0f,
	    		0.0f, 1.0f,
	    		1.0f, 0.0f,
	    		1.0f, 1.0f,
	    };
	       
	   
	    float vertex[] = 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,

		}; 
        
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, GLBase.makeFloatBuffer(vertex));
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, GLBase.makeFloatBuffer(texCoords));
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
		
	    
		// reflect
	    
		gl.glTranslatef(0, -2, 0);
		gl.glScalef(1, -1, 1);
		gl.glColor4f(0.5f, 0.5f, 0.5f, 1.0f);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
		gl.glColor4f(1, 1, 1, 1);
		
	
		gl.glPopMatrix();
		
	}
	
	static void change_projection(boolean bParallel, GL10 gl ,int width, int height){
	    gl.glMatrixMode (GL10.GL_PROJECTION);
	    gl.glLoadIdentity();
	    if (bParallel){
	    	gl.glOrthof(-1f, 1f, -1f, 1f,  Z_NEAR, Z_FAR);
	        //gl.glOrthof(-width / 2, width / 2, -height / 2, height / 2, Z_NEAR, Z_FAR);
	    }
	    else{
	    	GLU.gluPerspective(gl,(float) (2.0 * FOVY), width/height, Z_NEAR, Z_FAR);
	    }
	    gl.glMatrixMode(GL10.GL_MODELVIEW);
	}

	static void display(int width,int height,GL10 gl,List<Cover> covers){
		
		float afBGColor[] = {0.0f, 0.0f, 0.0f, 1.0f};
		//clear background
		float fAlpha = 0.5f;
		float clear[] = afBGColor;
		gl.glClearColor(clear[0], clear[1], clear[2], clear[3]);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		// draw covers with perspective projection
		change_projection(false, gl, width, height);
		gl.glPushMatrix();
		gl.glTranslatef(-pValueCoverTrack.getVal(), 0.0f, 0.0f);
		
		//find the current one
		
		for(int i=covers.size();i>0;i--){
			((Cover)covers.get(i-1)).draw(gl);
		}
		
		((Cover)covers.get(0)).draw(gl);
		
		gl.glPopMatrix();
		
		change_projection(true, gl, width, height);
		//draw left mask
		//gl.glDisable(GL10.GL_TEXTURE_2D);
		//glDisable(GL_TEXTURE_RECTANGLE_ARB);
		/**
		fAlpha = 1.0f;
		
		gl.glColor4f(0.5f, 0.5f, 0.5f, 1.0f);
		float vert[]  = new float[]{
				(float)(-width / 2.0), (float)(-height / 2.0),
				(float)(-width / 2.5), (float)(-height / 2.0),
				(float)(-width / 2.5), (float)(-height / 2.0),
				(float)(-width / 2.0), (float)(-height / 2.0),
		};
		
		float vert2[]  = new float[]{
				(float)(-width / 2.5), (float)(-height / 2.0),
				(float)(-width / 2.0), (float)(-height / 2.0),
				(float)(-width / 2.0), (float)(-height / 2.0),
				(float)(-width / 2.5), (float)(-height / 2.0),
		};
	    
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, GLBase.makeFloatBuffer(vert));
	    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, GLBase.makeFloatBuffer(vert2));
	    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
	    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
	    
	    //gl.glColor4f(1, 1, 1, 1);
	   */
	   // gl.glEnable(GL10.GL_TEXTURE_2D);
	
	    
	}
}
