package com.ninjarun;

import java.util.ArrayList;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.IEntity;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.extension.tmx.TMXLayer;
import org.andengine.extension.tmx.TMXLoader;
import org.andengine.extension.tmx.TMXLoader.ITMXTilePropertiesListener;
import org.andengine.extension.tmx.TMXProperties;
import org.andengine.extension.tmx.TMXTile;
import org.andengine.extension.tmx.TMXTileProperty;
import org.andengine.extension.tmx.TMXTiledMap;
import org.andengine.extension.tmx.util.exception.TMXLoadException;
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.TiledTextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.debug.Debug;

import android.hardware.SensorManager;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

public class MainActivity extends SimpleBaseGameActivity {
	private Camera camera;
	private static final int CAMERA_WIDTH = 800;
	private static final int CAMERA_HEIGHT = 480;

	public Scene mScene;
	private TMXTiledMap mTMXTiledMap;

	private BitmapTextureAtlas mBitmapTextureAtlas;

	public TiledTextureRegion mPlayer_MPTextureRegion;
	private TiledTextureRegion mPlayerTextureRegion;

	// 적 선언
	private TiledTextureRegion mEnemyTextureRegion;
	// 총알 선언
	private TiledTextureRegion mShurikenTextureRegion;

	public PhysicsWorld mPhysicsWorld;

	private static int mapOffset = 100;

	// FixtureDefs 생성을 위한 Category bits
	private static final short CATEGORYBIT_WALL = 0x0001;
	private static final short CATEGORYBIT_PLAYER = 0x0002;
	private static final short CATEGORYBIT_ENEMY = 0x0004;
	private static final short CATEGORYBIT_SHURIKEN = 0x0008;

	// FixtureDefs 생성을 위한 Mask bits
	private static final short MASKBITS_PLAYER = CATEGORYBIT_WALL + CATEGORYBIT_ENEMY;

	@Override
	public EngineOptions onCreateEngineOptions() {
		camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		EngineOptions engineOptions = new EngineOptions(true,
				ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(),
				camera);
		return engineOptions;
	}

	@Override
	protected void onCreateResources() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

		loadImages();
	}

	private void loadImages() {

		// create atlas of 512*512
		this.mBitmapTextureAtlas = new BitmapTextureAtlas(
				this.getTextureManager(), 512, 512, TextureOptions.BILINEAR);

		// 플레이어 캐릭터
		this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(this.mBitmapTextureAtlas, this,
						"ninja.gif", 0, 0, 4, 1);

		// 적 캐릭터
		// 적캐릭터 경우는 그냥 원하는대로 만들어서 넣으면 됩니다.
		this.mEnemyTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(this.mBitmapTextureAtlas, this,
						"enemy.gif", 0, 128, 4, 1);

		// 표창
		this.mShurikenTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createTiledFromAsset(this.mBitmapTextureAtlas, this,
						"weapon.gif", 0, 256, 1, 1);

		this.mBitmapTextureAtlas.load();
	}

	@Override
	protected Scene onCreateScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());

		mScene = new Scene();

		try {
			final TMXLoader tmxLoader = new TMXLoader(this.getAssets(),
					this.mEngine.getTextureManager(),
					TextureOptions.BILINEAR_PREMULTIPLYALPHA,
					this.getVertexBufferObjectManager(),
					new ITMXTilePropertiesListener() {
						@Override
						public void onTMXTileWithPropertiesCreated(
								final TMXTiledMap pTMXTiledMap,
								final TMXLayer pTMXLayer,
								final TMXTile pTMXTile,
								final TMXProperties<TMXTileProperty> pTMXTileProperties) {
						}
					});

			// 타일맵 로드
			this.mTMXTiledMap = tmxLoader.loadFromAsset("tmx/back.tmx");

		} catch (final TMXLoadException e) {
			Debug.e(e);
		}

		final TMXLayer tmxLayer = this.mTMXTiledMap.getTMXLayers().get(0);
		mScene.attachChild(tmxLayer);

		this.mPhysicsWorld = new PhysicsWorld(new Vector2(0,
				SensorManager.GRAVITY_EARTH), false);
		mScene.registerUpdateHandler(this.mPhysicsWorld);

		spawnPlayer(mPlayerTextureRegion);

		return mScene;
	}

	/*********************************************************************
	 * 
	 * 플레이어 캐릭터 등장
	 * 
	 ********************************************************************/

	public void spawnPlayer(TiledTextureRegion playerTexture) {

		AnimatedSprite mPlayerSprite = new AnimatedSprite(mapOffset, 0,
				playerTexture, this.getVertexBufferObjectManager());

		final FixtureDef mPlayerFixtureDef = PhysicsFactory.createFixtureDef(0,
				0f, 0f, false, CATEGORYBIT_PLAYER, MASKBITS_PLAYER, (short) 0);

		Body mPlayerBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld,
				mPlayerSprite, BodyType.DynamicBody, mPlayerFixtureDef);
		this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(
				mPlayerSprite, mPlayerBody, true, false));

		mScene.attachChild(mPlayerSprite);
	}

}
