package com.android.draw3d;

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

import android.content.Context;
import android.opengl.GLES10;
import android.opengl.GLSurfaceView;

public class ModelRenderer implements GLSurfaceView.Renderer {
	private Context mContext;
	private Model mModel;
	
	private int zoom = 0;
	private float panX = 0.0f;
	private float panY = 0.0f;
	private float distance = 6;
	private float elevation = 45;
	private float rotation = 45;
	
	
	public ModelRenderer(Context context){
		mContext = context;
		mModel = new Model(mContext);
	}
	
	private void bind(GL10 gl, int type, int id){
		return;
	}
	
	private void bindTextures(GL10 gl){
		return;
	}
	
	public void changeDistance(float distance) {
    	if (this.distance + distance >= 0 && this.distance + distance <= 9) {
        	this.distance += distance;	
    	}
    }
	
	public void changeElevation(float elevation) {
    	if (this.elevation + elevation >= 0 && this.elevation + elevation <= 90) {
    		this.elevation += elevation;
    	}
	}
	
	public void changePanX(float panX) {
    	this.panX += panX;
    }
	
    public void changePanY(float panY) {
    	this.panY += panY;
    }
    
    public void changeRotation(float rotation) {
    	this.rotation += rotation;
    }
    
    public int[] getConfigSpec() { 
    	int[] configSpec = {
		        EGL10.EGL_DEPTH_SIZE,   16,
		        EGL10.EGL_NONE
		};
		return configSpec;
    }
    
    private void polarView(GL10 gl, float panX, float panY, float distance, float elevation, float rotation) {
		gl.glTranslatef(panX, 0, 0);
		gl.glTranslatef(0, panY, 0);
    	gl.glTranslatef(0.0f, 0.0f, -distance);
		gl.glRotatef(elevation, 1.0f, 0.0f, 0.0f);
		gl.glRotatef(rotation, 0.0f, 1.0f, 0.0f);
	}
    
    public void setZoom(int zoom){
    	this.zoom = zoom;
    }
    
    public void sizeChanged(GL10 gl, int width, int height) {
         gl.glViewport(0, 0, width, height);
         float ratio = (float) width / height;
         gl.glMatrixMode(GL10.GL_PROJECTION);
         gl.glLoadIdentity();
         gl.glFrustumf(-ratio, ratio, -1, 1, 1, 25);
    }

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		gl.glDisable(GL10.GL_DITHER);
    	gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
    	gl.glShadeModel(GL10.GL_SMOOTH);
    	gl.glEnable(GL10.GL_DEPTH_TEST);
    	gl.glEnable(GL10.GL_TEXTURE_2D);
    	gl.glClearColor(0,0,0,0);
    	
    	sizeChanged(gl, 640, 720);
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onDrawFrame(GL10 gl) {
		//gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
		//gl.glClear(GLES10.GL_COLOR_BUFFER_BIT | GLES10.GL_DEPTH_BUFFER_BIT);
		
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        gl.glClearColor(0.25f, 0.25f, 0.25f, 1.0f);
        
       sizeChanged(gl, 640, 720);
        
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        //gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
        
        if (zoom != 0) changeDistance((float)zoom/50);
        polarView(gl, panX, panY, distance, elevation, rotation);
//        gl.glScalef(1.1f, 1.1f, 1.1f);

        //mIndicators.draw(gl);
        mModel.draw(gl);
	}
	
	public static int loadShader(int type, String shaderCode){
        int shader = 0;

        return shader;
    }
}
