package com.aerodroidrace;

import java.io.IOException;

import org.anddev.andengine.audio.music.Music;
import org.anddev.andengine.audio.music.MusicFactory;
import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.IUpdateHandler;
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 org.anddev.andengine.util.Debug;

import com.aerodroidrace.Game.PantallaJuego;

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

public class Menu extends BaseGameActivity {
	
	static final int CAMERA_WIDTH = 1024;
	static final int CAMERA_HEIGHT = 512;
	 
	private Camera mCamara;
	
	public static Music musicaMenu;
	
	private BitmapTextureAtlas mBitmapTextureAtlas, texFuente, imagen, sonido;
	private BitmapTextureAtlas mNubes, mNubes1, mNubes2, mSalir, von, vof;
	private TextureRegion mFaceTextureRegion, img, mFacevon, mFacevof, son;
	private SharedPreferences estadosonido;
	private TextureRegion nub;
	private TextureRegion nub1;
	private TextureRegion nub2;
	private TextureRegion sal;
	private Font fuente;
	private Text titulo;
	private Text titulo1;
	
	protected  Boolean ismusicON;
	
	private Sprite picture, i1, i2, i3, exit, imag, vozon, vozoff, sound;

	public void onLoadComplete() {
		
		if(ismusicON)
			musicaMenu.play();
		
	}

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

	public void onLoadResources() {
		
		//Aquí se cargan las texturas,  fondos e imágenes
		this.mBitmapTextureAtlas = new BitmapTextureAtlas(1024, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mNubes = new BitmapTextureAtlas(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mNubes1 = new BitmapTextureAtlas(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mNubes2 = new BitmapTextureAtlas(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.mSalir = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.imagen = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.von = new BitmapTextureAtlas(64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.vof = new BitmapTextureAtlas(64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.sonido = new BitmapTextureAtlas(256, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("graficos/");
		
		this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "fondopuntos.png", 0, 0);
		this.nub = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mNubes,this,"jugar.png",0,0);
		this.nub1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mNubes1,this,"comojugar2.png",0,0);
		this.nub2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mNubes2,this,"puntuaciones.png",0,0);
		this.sal = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mSalir,this,"salir.png",0,0);
		this.img = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.imagen,this,"imagenmenu.png",0,0);
		this.mFacevon = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.von,this,"altavozOn.png",0,0);
		this.mFacevof = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.vof,this,"altavozOff.png",0,0);
		this.son = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.sonido,this,"sound.png",0,0);
		
		this.texFuente = new BitmapTextureAtlas(256, 256,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		
		FontFactory.setAssetBasePath("fuente/");
		this.fuente = FontFactory.createFromAsset(this.texFuente, this,
				"Droid.ttf", 40, true, Color.RED);
		
		this.mEngine.getFontManager().loadFont(this.fuente);
		
		
		
		//Aquí cargamos la música
		MusicFactory.setAssetBasePath("sonidos/");
		try{
			musicaMenu = MusicFactory.createMusicFromAsset(this.mEngine.getMusicManager(), this, "musicainicial.mp3");
			musicaMenu.setLooping(true);
			
			
		} catch (final IOException e){
			
			Debug.e(e);
		}
		
		this.mEngine.getTextureManager().loadTextures(this.mBitmapTextureAtlas, mNubes,  mNubes1, mNubes2, mSalir, texFuente, imagen, von, vof, sonido);
		
	}

	public Scene onLoadScene() {
		
		this.mEngine.registerUpdateHandler(new FPSLogger());
		
        final Scene scene = new Scene();
        scene.setBackground(new ColorBackground(0, 0, 0.8784f));
        
        
        picture = new Sprite(0, 0, this.mFaceTextureRegion);
        
        scene.attachChild(picture);
        
        titulo = new Text(250, 50, this.fuente,"AERODROID");
		titulo.setScale(1.8f);
		titulo1 = new Text(250, 140, this.fuente, "   RACE   ");
		titulo1.setScale(1.8f);
		scene.attachChild(titulo);
		scene.attachChild(titulo1);
		
		imag = new Sprite(256, 200, this.img);
		imag.setScale(1.3f);
		scene.attachChild(imag);
	
		vozon = new Sprite(910, 435, this.mFacevon);
		vozoff = new Sprite(910, 435, this.mFacevof);
	
		
		 i1 = new Sprite(700, 0, this.nub){
			
			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) {
					
					//musicaMenu.stop();
					Intent a = new Intent(Menu.this,PantallaJuego.class);
					finish();
					Menu.this.startActivity(a);
						
					}
				return true;
			} 
			

		
		};
        
		 i2 = new Sprite(700, 140, this.nub1){
			
			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) {
					
					//musicaMenu.stop();
					Intent a = new Intent(Menu.this,Reglas.class);
					finish();
					Menu.this.startActivity(a);
								
					}
				return true;
			} 
		};
		
		 i3 = new Sprite(700, 280, this.nub2){
			
			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) {
					
					//musicaMenu.stop();
					Intent a = new Intent(Menu.this,Puntuaciones.class);
					finish();
				    Menu.this.startActivity(a);
									
					}
				return true;
			} 
		};
		
			
		estadosonido = getSharedPreferences("sonido",MODE_PRIVATE);
		
		ismusicON = estadosonido.getBoolean("sonido", true);
		
	
		if (ismusicON){
			
			scene.attachChild(vozon);
		}
		else
			scene.attachChild(vozoff);
	
		
		scene.registerUpdateHandler(new IUpdateHandler() {
	
			
			public void reset() {
			}
 
			
			public void onUpdate(float pSecondsElapsed) {
				
				sound = new Sprite(700, 420, Menu.this.son){
				
				public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
						final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
					if (pSceneTouchEvent.getAction() == MotionEvent.ACTION_DOWN) {
						
						if(musicaMenu.isPlaying()){
							
							musicaMenu.pause();
							ismusicON = false;
							scene.detachChild(vozon);
							scene.attachChild(vozoff);
							SharedPreferences.Editor admin = estadosonido.edit();
							admin.putBoolean("sonido", false);
							admin.commit();
							
						}else{
							
							musicaMenu.play();
							ismusicON = true;
							scene.detachChild(vozoff);
							scene.attachChild(vozon);
							SharedPreferences.Editor admin = estadosonido.edit();
							admin.putBoolean("sonido", true);
							admin.commit();
							
						}	
					}
					
					return true;
				}
				
			};
			
			scene.attachChild(sound);
			scene.registerTouchArea(sound);
				
			}});
		
		
		 exit = new Sprite(20, 415, this.sal){
			
			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) {
					
					SharedPreferences.Editor admin = estadosonido.edit();
					admin.putBoolean("sonido", true);
					admin.commit();
					finish();
					
				}
				return true;
			} 
		};
		
		
		
		
		scene.attachChild(i1);
		scene.attachChild(i2);
		scene.attachChild(i3);
		scene.attachChild(exit);
		
		scene.registerTouchArea(i1);
		scene.registerTouchArea(i2);
		scene.registerTouchArea(i3);
		scene.registerTouchArea(exit);
		

        return scene;
	}
	
	public void onPauseGame(){
		
		this.finish();
		super.onPause();	
		
	}	
}
