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.util.FPSLogger;
import org.anddev.andengine.input.touch.TouchEvent;
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.view.MotionEvent;

public class FinDeJuego extends BaseGameActivity{
	
	private static final float CAMERA_WIDTH = 1024;
	private static final float CAMERA_HEIGHT = 512;
	private Camera mCamera;
	private BitmapTextureAtlas mBitmapTextureAtlas1, mBitmapTextureAtlas2, mBitmapTextureAtlas3, mBitmapTextureAtlas4;
	private TextureRegion mFaceTextureRegion1, mFaceTextureRegion2, mFaceTextureRegion3, mFaceTextureRegion4;
	private Sprite picture, sp, sp1, sp2;
	

	public void onLoadComplete() {
		
		
	}

	public Engine onLoadEngine() {
		
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		
		final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE,
				new FillResolutionPolicy(), this.mCamera);
		
        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(1024, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mBitmapTextureAtlas3 = new BitmapTextureAtlas(512, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mBitmapTextureAtlas4 = new BitmapTextureAtlas(512, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("graficos/");
		
		this.mFaceTextureRegion1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas1, this, "fondoagua.png", 0, 0);
		this.mFaceTextureRegion2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas2, this, "gameover.png", 0, 0);
		this.mFaceTextureRegion3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas3, this, "verpuntuaciones.png", 0, 0);
		this.mFaceTextureRegion4 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas4, this, "volvermenu.png", 0, 0);
		
		
		
		this.mEngine.getTextureManager().loadTextures(this.mBitmapTextureAtlas1, this.mBitmapTextureAtlas2, this.mBitmapTextureAtlas3, this.mBitmapTextureAtlas4);
		
		
	}

	public Scene onLoadScene() {
		
		this.mEngine.registerUpdateHandler(new FPSLogger());
		
        final Scene scene = new Scene();
        scene.setBackground(new ColorBackground(0, 1, 0));
        
        
        picture = new Sprite(0, 0, this.mFaceTextureRegion1);
       
        
        sp = new Sprite(256, 0, this.mFaceTextureRegion2);
       
        
        sp1 = new Sprite(212, 350, this.mFaceTextureRegion3){
        	
        	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(FinDeJuego.this,Puntuaciones.class);
					finish();
					startActivity(a);
									}
				return true;
			} 
        };
        
         sp2 = new Sprite(600, 350, this.mFaceTextureRegion4){
        	
        	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(FinDeJuego.this,Menu.class);
					finish();
					startActivity(a);
									}
				return true;
			} 
        };
        
        scene.attachChild(picture);
        scene.attachChild(sp);
        scene.attachChild(sp1);
        scene.attachChild(sp2);
		scene.registerTouchArea(sp1);
		scene.registerTouchArea(sp2);
		
		return scene;
	}
	
	public void onPauseGame(){
		
		super.onPauseGame();
		finish();
	}

}
