package net.greencouchgames.ld27;

import org.andengine.audio.music.Music;
import org.andengine.audio.music.MusicFactory;
import org.andengine.audio.sound.Sound;
import org.andengine.audio.sound.SoundFactory;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;

import tv.ouya.console.api.OuyaController;

import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MotionEvent;

public class Main extends SimpleBaseGameActivity {
 
    static int gameWidth = 1280;
    static int gameHeight = 720;
    static float deadzone = 0.5f;

    public static BitmapTextureAtlas buf_bgmenu;
    public static TextureRegion spr_bgmenu;
    public static BitmapTextureAtlas buf_multiprep;
    public static TextureRegion spr_multiprep;
    public static BitmapTextureAtlas buf_logo;
    public static TextureRegion spr_logo;
    
    public static BitmapTextureAtlas buf_bgsp;
    public static TextureRegion spr_bgsp;
    public static BitmapTextureAtlas buf_multi;
    public static TextureRegion spr_multi;
    public static BitmapTextureAtlas buf_bgscore;
    public static TextureRegion spr_bgscore;

    public static BitmapTextureAtlas buf_circle;
    public static TextureRegion spr_circle;
    public static BitmapTextureAtlas buf_ex1;
    public static TextureRegion spr_ex1;
    public static BitmapTextureAtlas buf_ex2;
    public static TextureRegion spr_ex2;
    
    public static BitmapTextureAtlas buf_baked1;
    public static TextureRegion spr_baked1;
    public static BitmapTextureAtlas buf_baked2;
    public static TextureRegion spr_baked2;
    public static BitmapTextureAtlas buf_baked3;
    public static TextureRegion spr_baked3;
    public static BitmapTextureAtlas buf_tankshell;
    public static TextureRegion spr_tankshell;
    
    public static BitmapTextureAtlas buf_player;
    public static TextureRegion spr_player;
    public static BitmapTextureAtlas buf_player1;
    public static TextureRegion spr_player1;
    public static BitmapTextureAtlas buf_player2;
    public static TextureRegion spr_player2;
    
    public static BitmapTextureAtlas buf_enemy;
    public static TextureRegion spr_enemy;
    public static BitmapTextureAtlas buf_enemydown;
    public static TextureRegion spr_enemydown;
    public static BitmapTextureAtlas buf_tank;
    public static TextureRegion spr_tank;
    public static BitmapTextureAtlas buf_tankcannon;
    public static TextureRegion spr_tankcannon;
    
    public static BitmapTextureAtlas buf_count1;
    public static TextureRegion spr_count1;
    public static BitmapTextureAtlas buf_count2;
    public static TextureRegion spr_count2;
    public static BitmapTextureAtlas buf_count3;
    public static TextureRegion spr_count3;
    public static BitmapTextureAtlas buf_count4;
    public static TextureRegion spr_count4;
    public static BitmapTextureAtlas buf_count5;
    public static TextureRegion spr_count5;
    public static BitmapTextureAtlas buf_count6;
    public static TextureRegion spr_count6;
    public static BitmapTextureAtlas buf_count7;
    public static TextureRegion spr_count7;
    public static BitmapTextureAtlas buf_count8;
    public static TextureRegion spr_count8;
    public static BitmapTextureAtlas buf_count9;
    public static TextureRegion spr_count9;
    public static BitmapTextureAtlas buf_count10;
    public static TextureRegion spr_count10;
    
    public static BitmapTextureAtlas buf_tutorial1;
    public static TextureRegion spr_tutorial1;
    public static BitmapTextureAtlas buf_tutorial2;
    public static TextureRegion spr_tutorial2;
    public static BitmapTextureAtlas buf_tutorial3;
    public static TextureRegion spr_tutorial3;
    public static BitmapTextureAtlas buf_tutorial4;
    public static TextureRegion spr_tutorial4;
    public static BitmapTextureAtlas buf_tutorial5;
    public static TextureRegion spr_tutorial5;
    public static BitmapTextureAtlas buf_tutorial6;
    public static TextureRegion spr_tutorial6;
    public static BitmapTextureAtlas buf_close;
    public static TextureRegion spr_close;
    
    public static Font font1;
	public static Font font2;
	public static Font font3;
	
	public static Sound hurt;
	public static Sound thrown;
	public static Sound dodge;
	public static Sound gameover;
	public static Sound select;
	public static Sound tankdie;
	public static Sound tankshoot;
	public static Sound enemydie;
	
	public static Music tankmusic;
	public static Music menumusic;
	public static Music gsmusic;
    
    public Scene scene;
    public Camera camera;
    public static Main root;
    public static float multi = 1;
 
    // Ouya Controller Handling
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        OuyaController.init(this);
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	if (scene instanceof s_Menu) {
    		if (((s_Menu)scene).introFinished) {
    			if (keyCode == OuyaController.BUTTON_O) {
        			setCurrentScene(new s_Game(event.getDeviceId()));
        			Main.menumusic.pause();
    			} else if (keyCode == OuyaController.BUTTON_U) {
        			setCurrentScene(new s_MultiPrepare(event.getDeviceId()));
        			Main.menumusic.pause();
    			} else if (keyCode == OuyaController.BUTTON_Y) {
        			setCurrentScene(new s_Tutorial(event.getDeviceId()));
        			Main.menumusic.pause();
    			}
    		}
    	} else if (scene instanceof s_MultiPrepare) {
    		if (keyCode == OuyaController.BUTTON_O) {
    			((s_MultiPrepare)scene).controller2 = event.getDeviceId();
    		} else if (keyCode == OuyaController.BUTTON_MENU) {
				setCurrentScene(new s_Menu(true));
    		}
    	} else if (scene instanceof s_Game) {
    		if (keyCode == OuyaController.BUTTON_MENU) {
    			((s_Game)scene).paused = !((s_Game)scene).paused;
    			select.play();
    		}
    	}
    	
        boolean handled = OuyaController.onKeyDown(keyCode, event);
        return handled || super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        boolean handled = OuyaController.onKeyUp(keyCode, event);
        return handled || super.onKeyUp(keyCode, event);
    }

    @Override
    public boolean onGenericMotionEvent(MotionEvent event) {
        boolean handled = OuyaController.onGenericMotionEvent(event);
        return handled || super.onGenericMotionEvent(event);
    }
    
    @Override
    public EngineOptions onCreateEngineOptions() {
    	root = this;
    	
    	Display display = getWindowManager().getDefaultDisplay();
    	Point size = new Point();
    	display.getSize(size);
    	
        camera = new Camera(0, 0, gameWidth, gameHeight);
        EngineOptions eo =  new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED,
            new RatioResolutionPolicy(gameWidth, gameHeight), camera);
        eo.getAudioOptions().setNeedsSound(true);
        eo.getAudioOptions().setNeedsMusic(true);
        return eo;
    }
 
    @Override
    protected void onCreateResources() {
    	BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("graphics/720p/");
    	
    	buf_bgmenu = new BitmapTextureAtlas(this.getTextureManager(), 2048, 1024, TextureOptions.DEFAULT);
    	spr_bgmenu = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_bgmenu, this, "menu.png", 0, 0);
    	buf_bgmenu.load();
    	buf_logo = new BitmapTextureAtlas(this.getTextureManager(), 1024, 512, TextureOptions.DEFAULT);
    	spr_logo = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_logo, this, "logo.png", 0, 0);
    	buf_logo.load();
    	buf_multiprep = new BitmapTextureAtlas(this.getTextureManager(), 2048, 1024, TextureOptions.DEFAULT);
    	spr_multiprep = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_multiprep, this, "bg_multiprep.png", 0, 0);
    	buf_multiprep.load();
    	
    	buf_bgsp = new BitmapTextureAtlas(this.getTextureManager(), 2048, 1024, TextureOptions.DEFAULT);
    	spr_bgsp = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_bgsp, this, "sp_bg.png", 0, 0);
    	buf_bgsp.load();
    	buf_multi = new BitmapTextureAtlas(this.getTextureManager(), 2048, 1024, TextureOptions.DEFAULT);
    	spr_multi = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_multi, this, "bg_multi.png", 0, 0);
    	buf_multi.load();
    	buf_bgscore = new BitmapTextureAtlas(this.getTextureManager(), 1024, 128, TextureOptions.DEFAULT);
    	spr_bgscore = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_bgscore, this, "bg_score.png", 0, 0);
    	buf_bgscore.load();
    	
    	buf_circle = new BitmapTextureAtlas(this.getTextureManager(), 16, 16, TextureOptions.DEFAULT);
    	spr_circle = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_circle, this, "circle.png", 0, 0);
    	buf_circle.load();
    	buf_ex1 = new BitmapTextureAtlas(this.getTextureManager(), 512, 512, TextureOptions.DEFAULT);
    	spr_ex1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_ex1, this, "explosion1.png", 0, 0);
    	buf_ex1.load();
    	buf_ex2 = new BitmapTextureAtlas(this.getTextureManager(), 256, 256, TextureOptions.DEFAULT);
    	spr_ex2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_ex2, this, "explosion2.png", 0, 0);
    	buf_ex2.load();
    	
    	buf_baked1 = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.DEFAULT);
    	spr_baked1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_baked1, this, "baked1.png", 0, 0);
    	buf_baked1.load();
    	buf_baked2 = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.DEFAULT);
    	spr_baked2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_baked2, this, "baked2.png", 0, 0);
    	buf_baked2.load();
    	buf_baked3 = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.DEFAULT);
    	spr_baked3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_baked3, this, "baked3.png", 0, 0);
    	buf_baked3.load();
    	buf_tankshell = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.DEFAULT);
    	spr_tankshell = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_tankshell, this, "tankshell.png", 0, 0);
    	buf_tankshell.load();
    	
    	buf_player = new BitmapTextureAtlas(this.getTextureManager(), 64, 64, TextureOptions.DEFAULT);
    	spr_player = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_player, this, "player1.png", 0, 0);
    	buf_player.load();
    	buf_player1 = new BitmapTextureAtlas(this.getTextureManager(), 64, 64, TextureOptions.DEFAULT);
    	spr_player1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_player1, this, "player2.png", 0, 0);
    	buf_player1.load();
    	buf_player2 = new BitmapTextureAtlas(this.getTextureManager(), 64, 64, TextureOptions.DEFAULT);
    	spr_player2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_player2, this, "player3.png", 0, 0);
    	buf_player2.load();

    	buf_enemy = new BitmapTextureAtlas(this.getTextureManager(), 64, 64, TextureOptions.DEFAULT);
    	spr_enemy = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_enemy, this, "enemy.png", 0, 0);
    	buf_enemy.load();
    	buf_enemydown = new BitmapTextureAtlas(this.getTextureManager(), 64, 64, TextureOptions.DEFAULT);
    	spr_enemydown = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_enemydown, this, "enemydown.png", 0, 0);
    	buf_enemydown.load();

    	buf_tank = new BitmapTextureAtlas(this.getTextureManager(), 128, 128, TextureOptions.DEFAULT);
    	spr_tank = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_tank, this, "tank.png", 0, 0);
    	buf_tank.load();
    	buf_tankcannon = new BitmapTextureAtlas(this.getTextureManager(), 64, 64, TextureOptions.DEFAULT);
    	spr_tankcannon = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_tankcannon, this, "tankcannon.png", 0, 0);
    	buf_tankcannon.load();

    	buf_count1 = new BitmapTextureAtlas(this.getTextureManager(), 64, 64, TextureOptions.DEFAULT);
    	spr_count1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_count1, this, "count1.png", 0, 0);
    	buf_count1.load();
    	buf_count2 = new BitmapTextureAtlas(this.getTextureManager(), 64, 64, TextureOptions.DEFAULT);
    	spr_count2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_count2, this, "count2.png", 0, 0);
    	buf_count2.load();
    	buf_count3 = new BitmapTextureAtlas(this.getTextureManager(), 64, 64, TextureOptions.DEFAULT);
    	spr_count3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_count3, this, "count3.png", 0, 0);
    	buf_count3.load();
    	buf_count4 = new BitmapTextureAtlas(this.getTextureManager(), 64, 64, TextureOptions.DEFAULT);
    	spr_count4 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_count4, this, "count4.png", 0, 0);
    	buf_count4.load();
    	buf_count5 = new BitmapTextureAtlas(this.getTextureManager(), 64, 64, TextureOptions.DEFAULT);
    	spr_count5 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_count5, this, "count5.png", 0, 0);
    	buf_count5.load();
    	buf_count6 = new BitmapTextureAtlas(this.getTextureManager(), 64, 64, TextureOptions.DEFAULT);
    	spr_count6 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_count6, this, "count6.png", 0, 0);
    	buf_count6.load();
    	buf_count7 = new BitmapTextureAtlas(this.getTextureManager(), 64, 64, TextureOptions.DEFAULT);
    	spr_count7 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_count7, this, "count7.png", 0, 0);
    	buf_count7.load();
    	buf_count8 = new BitmapTextureAtlas(this.getTextureManager(), 64, 64, TextureOptions.DEFAULT);
    	spr_count8 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_count8, this, "count8.png", 0, 0);
    	buf_count8.load();
    	buf_count9 = new BitmapTextureAtlas(this.getTextureManager(), 64, 64, TextureOptions.DEFAULT);
    	spr_count9 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_count9, this, "count9.png", 0, 0);
    	buf_count9.load();
    	buf_count10 = new BitmapTextureAtlas(this.getTextureManager(), 64, 64, TextureOptions.DEFAULT);
    	spr_count10 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_count10, this, "count10.png", 0, 0);
    	buf_count10.load();
    	
    	buf_tutorial1 = new BitmapTextureAtlas(this.getTextureManager(), 512, 512, TextureOptions.DEFAULT);
    	spr_tutorial1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_tutorial1, this, "tutorial1.png", 0, 0);
    	buf_tutorial1.load();
    	buf_tutorial2 = new BitmapTextureAtlas(this.getTextureManager(), 512, 512, TextureOptions.DEFAULT);
    	spr_tutorial2 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_tutorial2, this, "tutorial2.png", 0, 0);
    	buf_tutorial2.load();
    	buf_tutorial3 = new BitmapTextureAtlas(this.getTextureManager(), 512, 512, TextureOptions.DEFAULT);
    	spr_tutorial3 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_tutorial3, this, "tutorial3.png", 0, 0);
    	buf_tutorial3.load();
    	buf_tutorial4 = new BitmapTextureAtlas(this.getTextureManager(), 512, 512, TextureOptions.DEFAULT);
    	spr_tutorial4 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_tutorial4, this, "tutorial4.png", 0, 0);
    	buf_tutorial4.load();
    	buf_tutorial5 = new BitmapTextureAtlas(this.getTextureManager(), 512, 512, TextureOptions.DEFAULT);
    	spr_tutorial5 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_tutorial5, this, "tutorial5.png", 0, 0);
    	buf_tutorial5.load();
    	buf_tutorial6 = new BitmapTextureAtlas(this.getTextureManager(), 512, 512, TextureOptions.DEFAULT);
    	spr_tutorial6 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_tutorial6, this, "tutorial6.png", 0, 0);
    	buf_tutorial6.load();
    	buf_close = new BitmapTextureAtlas(this.getTextureManager(), 256, 32, TextureOptions.DEFAULT);
    	spr_close = BitmapTextureAtlasTextureRegionFactory.createFromAsset(buf_close, this, "close.png", 0, 0);
    	buf_close.load();
    	
    	font1 = FontFactory.create(this.getFontManager(),this.getTextureManager(), 1024, 256,Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 16, Color.WHITE);
    	font1.load();
    	font2 = FontFactory.create(this.getFontManager(),this.getTextureManager(), 1024, 256,Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32, Color.WHITE);
    	font2.load();
    	font3 = FontFactory.create(this.getFontManager(),this.getTextureManager(), 1024, 256,Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 96, Color.WHITE);
    	font3.load();
    	
    	try {
    		SoundFactory.setAssetBasePath("sounds/");
    		MusicFactory.setAssetBasePath("sounds/");
			hurt = SoundFactory.createSoundFromAsset(this.mEngine.getSoundManager(), this, "hurt.wav");
	    	thrown = SoundFactory.createSoundFromAsset(this.mEngine.getSoundManager(), this, "thrown.wav");
	    	dodge = SoundFactory.createSoundFromAsset(this.mEngine.getSoundManager(), this, "dodge.wav");
	    	gameover = SoundFactory.createSoundFromAsset(this.mEngine.getSoundManager(), this, "gameover.wav");
	    	select = SoundFactory.createSoundFromAsset(this.mEngine.getSoundManager(), this, "select.wav");
	    	tankdie = SoundFactory.createSoundFromAsset(this.mEngine.getSoundManager(), this, "tankdie.wav");
	    	tankshoot = SoundFactory.createSoundFromAsset(this.mEngine.getSoundManager(), this, "tankshoot.wav");
	    	enemydie = SoundFactory.createSoundFromAsset(this.mEngine.getSoundManager(), this, "enemydie.wav");

	    	tankmusic = MusicFactory.createMusicFromAsset(this.mEngine.getMusicManager(), this, "tankmusic.wav");
	    	tankmusic.play();
	    	tankmusic.pause();
	    	tankmusic.seekTo(0);
	    	menumusic = MusicFactory.createMusicFromAsset(this.mEngine.getMusicManager(), this, "menu.wav");
	    	menumusic.play();
	    	menumusic.pause();
	    	menumusic.seekTo(0);
	    	menumusic.setLooping(true);
	    	gsmusic = MusicFactory.createMusicFromAsset(this.mEngine.getMusicManager(), this, "godspeed.wav");
	    	gsmusic.play();
	    	gsmusic.pause();
	    	gsmusic.seekTo(0);
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
    }
 
    @Override
    protected Scene onCreateScene() {
        return setCurrentScene(new s_Menu(false));
    }
    public Scene setCurrentScene(Scene s) {
    	multi = 1;
        scene = s;
        getEngine().setScene(scene);
        return scene;
    }
    
    public void shakeCamera() {
    	camera.set((float)Math.random()*30*u_Explosion.timer, (float)Math.random()*30*u_Explosion.timer, 1280-(float)Math.random()*30*u_Explosion.timer, 720-(float)Math.random()*30*u_Explosion.timer);
    }
    public void resetCamera() {
    	camera.set(0, 0, 1280, 720);
    }
}