package com.view.coverflow;


import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import javax.microedition.khronos.opengles.GL10;


import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
public class Texture {
	private int bHeight;
	private int bWeight;
	private int tex;
	
	private FloatBuffer cubeBuff;
	private FloatBuffer texBuff;
	private String pathName;
	int textureId = 1;
	
	private Resources _r;
	private int _rid;
	
	float box[] = new float[] {
			-0.5f, -0.5f,  0.5f,
			 0.5f, -0.5f,  0.5f,
			-0.5f,  0.5f,  0.5f,
			 0.5f,  0.5f,  0.5f,
	};
	float texCoords[] = new float[] {
			 0.0f, 0.0f,
			 1.0f, 0.0f,
			 0.0f, 1.0f,
			 1.0f, 1.0f,
	};
	
	public Texture(String pathName){
		cubeBuff = GLBase.makeFloatBuffer(box);
		texBuff = GLBase.makeFloatBuffer(texCoords);
		this.pathName = pathName;
		
	}
	
	public Texture(Resources r,int rid)
	{
		cubeBuff = GLBase.makeFloatBuffer(box);
		texBuff = GLBase.makeFloatBuffer(texCoords);
		_r=r;
		_rid=rid;
	}
	
	public FloatBuffer getCubeBuff() {
		return cubeBuff;
	}

	public FloatBuffer getTexBuff() {
		return texBuff;
	}

	void getTexture(GL10 gl){
		
		Bitmap bitmap ;
		if(pathName!=null) bitmap= BitmapFactory.decodeFile(pathName);
		else 
			bitmap=BitmapFactory.decodeResource(_r, _rid);
		//BitmapFactory.
		this.setBHeight(bitmap.getHeight());
		this.setBWeight(bitmap.getWidth());
		
		ByteBuffer bb = ByteBuffer.allocateDirect(bitmap.getHeight()*bitmap.getWidth()*4);
		bb.order(ByteOrder.BIG_ENDIAN);
		IntBuffer ib = bb.asIntBuffer();

		for (int y=bitmap.getHeight()-1;y>-1;y--){
			for (int x=0;x<bitmap.getWidth();x++) {
				int pix = bitmap.getPixel(x,bitmap.getHeight()-y-1);
				// Convert ARGB -> RGBA
				@SuppressWarnings("unused")
				byte alpha = (byte)((pix >> 24)&0xFF);
				byte red = (byte)((pix >> 16)&0xFF);
				byte green = (byte)((pix >> 8)&0xFF);
				byte blue = (byte)((pix)&0xFF);
				
				// It seems like alpha is currently broken in Android...
				ib.put(red << 24 | green << 16 | blue << 8 | 0xFF);//255-alpha);
			}
			Log.v("yyyyyyyyyy", String.valueOf(y));
		}
		Log.v("yyyyyyyyyy", "=============");
		ib.position(0);
		bb.position(0);

		int[] tmp_tex = new int[textureId];

		gl.glGenTextures(1, tmp_tex, 0);
		tex = tmp_tex[0];
		gl.glBindTexture(GL10.GL_TEXTURE_2D, tex);
		gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, bitmap.getWidth(), bitmap.getHeight(), 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
		gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
		textureId++; 
	}

	public int getTex() {
		return tex;
	}

	public int getBHeight() {
		return bHeight;
	}

	public void setBHeight(int height) {
		bHeight = height;
	}

	public int getBWeight() {
		return bWeight;
	}

	public void setBWeight(int weight) {
		bWeight = weight;
	}
	
	
}
