package com.ninjarun;

import java.util.Vector;

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.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.controller.MultiTouch;
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.ITextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.debug.Debug;

import android.widget.Toast;

public class Test extends SimpleBaseGameActivity {

	BitmapTextureAtlas textureAtlas;
	ITextureRegion texture;

	@Override
	protected void onCreateResources() {
		// TODO Auto-generated method stub

	}

	@Override
	protected Scene onCreateScene() {
		Debug.w("onCreateScene()");

		s.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));

		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

		textureAtlas = new BitmapTextureAtlas(
				this.getTextureManager(), 512, 1024,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		texture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				textureAtlas, this, "health.png", 0, 0);

		this.mEngine.getTextureManager().loadTexture(textureAtlas);

		return s;
	}

	public static final int GAME_CAMERA_WIDTH = 1600;
	public static final int GAME_CAMERA_HEIGHT = 800;

	public static final Camera gameCamera = new Camera(0, 0, GAME_CAMERA_WIDTH,
			GAME_CAMERA_HEIGHT);

	public static EngineOptions gameEngineOptions;

	Vector<Sprite> S = new Vector<Sprite>();

	boolean done = false;

	public synchronized void more() {
		if (!done) {
			done = true;
			for (int i = 0; i < 25000; i++) {
				{
					Sprite sprite;
					sprite = (new Sprite(10 + S.size() * 1, 10 + S.size() * 1,
							texture, this.getVertexBufferObjectManager()));
					S.add(sprite);
					Debug.w("sprite number=" + i);
					// s.attachChild(sprite);
				}
			}
		}
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		// super.onBackPressed();
		this.runOnUpdateThread(new Runnable() {
			@Override
			public void run() {

				remove_all();
				System.gc();
				// TODO Auto-generated method stub
			}
		});

	}

	public synchronized void remove_all() {
		Sprite sprite = null;
		while (S.size() > 0) {
			sprite = S.remove(0);
			sprite.detachSelf();
			sprite.dispose();
		}
		done = false;

	}

	Scene s = new Scene() {

		public boolean onSceneTouchEvent(final TouchEvent pSceneTouchEvent) {
			// textureAtlas.load(mEngine.getTextureManager());

			more();

			return true;
		}

	};

	@Override
	public EngineOptions onCreateEngineOptions() {
		// TODO Auto-generated method stub
		if (gameEngineOptions == null) {
			gameEngineOptions = new EngineOptions(true,
					ScreenOrientation.LANDSCAPE_FIXED,
					new RatioResolutionPolicy(GAME_CAMERA_WIDTH,
							GAME_CAMERA_HEIGHT), gameCamera);

			gameEngineOptions.getTouchOptions().setNeedsMultiTouch(true);

			if (MultiTouch.isSupported(this)) {
				if (MultiTouch.isSupportedDistinct(this)) {
					Toast.makeText(
							this,
							"MultiTouch detected --> Both controls will work properly!",
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(
							this,
							"MultiTouch detected, but your device has problems distinguishing between fingers.\n\nControls are placed at different vertical locations.",
							Toast.LENGTH_LONG).show();
				}
			} else {
				Toast.makeText(
						this,
						"Sorry your device does NOT support MultiTouch!\n\n(Falling back to SingleTouch.)\n\nControls are placed at different vertical locations.",
						Toast.LENGTH_LONG).show();
			}

		}
		return gameEngineOptions;
	}

}


