package com.engine;


import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.timer.ITimerCallback;
import org.anddev.andengine.engine.handler.timer.TimerHandler;
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.engine.options.resolutionpolicy.IResolutionPolicy;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.SplashScene;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas.BitmapTextureFormat;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasFactory;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import android.app.Activity;
import android.content.Intent;

public abstract class CustomBaseSplashActivity extends BaseGameActivity {
    // ===========================================================
    // Constants
    // ===========================================================

    // ===========================================================
    // Fields
    // ===========================================================

    private Camera mCamera;
    private IBitmapTextureAtlasSource mSplashTextureAtlasSource;
    private TextureRegion mLoadingScreenTextureRegion;

    // ===========================================================
    // Constructors
    // ===========================================================

    // ===========================================================
    // Getter & Setter
    // ===========================================================

    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================

    protected abstract ScreenOrientation getScreenOrientation();

    protected abstract IBitmapTextureAtlasSource onGetSplashTextureAtlasSource();

    protected abstract float getSplashDuration();

    protected abstract Class<? extends Activity> getFollowUpActivity();

    protected float getSplashScaleFrom() {
            return 1f;
    }

    protected float getSplashScaleTo() {
            return 1f;
    }

    public void onLoadComplete() {
    }

    public Engine onLoadEngine() {
            this.mSplashTextureAtlasSource = this.onGetSplashTextureAtlasSource();

            final int width = this.mSplashTextureAtlasSource.getWidth();
            final int height = this.mSplashTextureAtlasSource.getHeight();

            this.mCamera = this.getSplashCamera(width, height);
            return new Engine(new EngineOptions(true, this.getScreenOrientation(), new FillResolutionPolicy(), this.mCamera));
    }

    public void onLoadResources() {
            final BitmapTextureAtlas loadingScreenBitmapTextureAtlas = BitmapTextureAtlasFactory.createForTextureAtlasSourceSize(BitmapTextureFormat.RGBA_8888, this.mSplashTextureAtlasSource, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
            this.mLoadingScreenTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromSource(loadingScreenBitmapTextureAtlas, this.mSplashTextureAtlasSource, 0, 0);

            this.getEngine().getTextureManager().loadTexture(loadingScreenBitmapTextureAtlas);
    }

    public Scene onLoadScene() {
            final float splashDuration = this.getSplashDuration();

            final SplashScene splashScene = new SplashScene(this.mCamera, this.mLoadingScreenTextureRegion, splashDuration, this.getSplashScaleFrom(), this.getSplashScaleTo());

            splashScene.registerUpdateHandler(new TimerHandler(splashDuration, new ITimerCallback() {
                    public void onTimePassed(final TimerHandler pTimerHandler) {
                    	CustomBaseSplashActivity.this.startActivity(new Intent(CustomBaseSplashActivity.this, CustomBaseSplashActivity.this.getFollowUpActivity()));
                    	CustomBaseSplashActivity.this.finish();
                    }
            }));

            return splashScene;
    }

    // ===========================================================
    // Methods
    // ===========================================================

    protected Camera getSplashCamera(final int pSplashwidth, final int pSplashHeight) {
            return new Camera(0, 0, pSplashwidth, pSplashHeight);
    }

    protected IResolutionPolicy getSplashResolutionPolicy(final int pSplashwidth, final int pSplashHeight) {
            return new RatioResolutionPolicy(pSplashwidth, pSplashHeight);
    }

    // ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================
}
