package com.aerodroidrace;


import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.font.FontFactory;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.view.MotionEvent;

public class Puntuaciones extends BaseGameActivity{
	
	private static final float CAMERA_WIDTH = 1024;
	private static final float CAMERA_HEIGHT = 512;
	private Camera mCamera2;
	private BitmapTextureAtlas mBitmapTextureAtlas1, mBitmapTextureAtlas2, texturaFuente, texturaFuente1;
	private SharedPreferences Puntuacion1;
	private SharedPreferences Puntuacion2;
	private SharedPreferences Puntuacion3;
	private SharedPreferences Puntuacion4;
	private SharedPreferences Puntuacion5;
	private Font fuente, fuente1;
	private Text puntos1;
	private Text puntos2;
	private Text puntos3;
	private Text puntos4;
	private Text puntos5;
	private TextureRegion mFaceTextureRegion1, mFaceTextureRegion2;
	private Sprite picture;
	
	
	

	public void onLoadComplete() {
		
	}

	public Engine onLoadEngine() {
		
		this.mCamera2 = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE,
				new FillResolutionPolicy(), this.mCamera2);
        engineOptions.getTouchOptions().setRunOnUpdateThread(true);
        final Engine engine = new Engine(engineOptions);
        return engine;
        
	}

	public void onLoadResources() {
		
		this.mBitmapTextureAtlas1 = new BitmapTextureAtlas(1024, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mBitmapTextureAtlas2 = new BitmapTextureAtlas(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("graficos/");
		
		this.mFaceTextureRegion1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas1, this, "fondopuntos.png", 0, 0);
		this.mFaceTextureRegion2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas2, this, "atras.png", 0, 0);
		
		this.texturaFuente = new BitmapTextureAtlas(256, 256,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.texturaFuente1 = new BitmapTextureAtlas(256, 256,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		
		FontFactory.setAssetBasePath("fuente/");
		this.fuente = FontFactory.createFromAsset(this.texturaFuente, this,
				"Droid.ttf", 40, true, Color.BLACK);
		this.fuente1 = FontFactory.createFromAsset(this.texturaFuente1, this,
				"Droid.ttf", 40, true, Color.RED);
		this.mEngine.getFontManager().loadFont(this.fuente);
		this.mEngine.getFontManager().loadFont(this.fuente1);
		
		
		this.mEngine.getTextureManager().loadTextures(this.mBitmapTextureAtlas1, this.mBitmapTextureAtlas2, this.texturaFuente, this.texturaFuente1);
		
	}

	public Scene onLoadScene() {
		
		this.mEngine.registerUpdateHandler(new FPSLogger());
		
        final Scene scene = new Scene();
        scene.setBackground(new ColorBackground(0, 1, 0));
        
        
        Puntuacion1 = getSharedPreferences("puntuacion1",MODE_PRIVATE);
		int pts1=0; 
		pts1=Puntuacion1.getInt("puntos",0);
		puntos1 = new Text(230, 30, this.fuente1,"  MAXIMA PUNTUACION: "+String.valueOf(pts1));
		puntos1.setScale(1.3f);
		Puntuacion2 = getSharedPreferences("puntuacion2",MODE_PRIVATE);
		int pts2=0; 
		pts2=Puntuacion2.getInt("puntos",0);
		puntos2 = new Text(230,100,this.fuente,"2ª MAXIMA PUNTUACION: "+String.valueOf(pts2));
		puntos2.setScale(1.1f);
		Puntuacion3 = getSharedPreferences("puntuacion3",MODE_PRIVATE);
		int pts3=0; 
		pts3=Puntuacion3.getInt("puntos",0);
		puntos3 = new Text(230,170,this.fuente,"3ª MAXIMA PUNTUACION: "+String.valueOf(pts3));
		puntos3.setScale(1.1f);
		Puntuacion4 = getSharedPreferences("puntuacion4",MODE_PRIVATE);
		int pts4=0; 
		pts4=Puntuacion4.getInt("puntos",0);
		puntos4 = new Text(230,240,this.fuente,"4ª MAXIMA PUNTUACION: "+String.valueOf(pts4));
		puntos4.setScale(1.1f);
		Puntuacion5 = getSharedPreferences("puntuacion5",MODE_PRIVATE);
		int pts5=0; 
		pts5=Puntuacion5.getInt("puntos",0);
		puntos5 = new Text(230,310,this.fuente,"5ª MAXIMA PUNTUACION: "+String.valueOf(pts5));
		puntos5.setScale(1.1f);
		
       
        picture = new Sprite(0, 0, this.mFaceTextureRegion1);
        scene.attachChild(picture);
       
        
        final Sprite sp1 = new Sprite(mCamera2.getCenterX() - 64, 380, this.mFaceTextureRegion2){
        	
        	public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
					final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				if (pSceneTouchEvent.getAction() == MotionEvent.ACTION_DOWN) {
				} else if (pSceneTouchEvent.getAction() == MotionEvent.ACTION_UP) {

					Intent a = new Intent(Puntuaciones.this,Menu.class);
					Puntuaciones.this.finish();
					Puntuaciones.this.startActivity(a);
									}
				return true;
			} 
        };
        
       
		scene.attachChild(sp1);
		scene.registerTouchArea(sp1);
		scene.attachChild(puntos1);
		scene.attachChild(puntos2);
		scene.attachChild(puntos3);
		scene.attachChild(puntos4);
		scene.attachChild(puntos5);
        
		return scene;
		
	}
	
	public void onPauseGame(){
		
		super.onPauseGame();
		finish();
	}


}
