package com.backfire.doudizhu;
import android.app.Activity;
import android.content.Intent;

import com.backfire.doudizhu.common.LoadingProgressDialog;
import com.backfire.doudizhu.exercise.ExerciseActivity;
import com.backfire.doudizhu.help.HelpActivity;
import com.backfire.doudizhu.lobby.LobbyActivity;
import com.backfire.doudizhu.luck.LuckActivity;
import com.backfire.doudizhu.ranklist.HighScoreActivity;
import com.backfire.doudizhu.shop.ShopActivity;
import com.backfire.doudizhu.task.TaskActivity;
import com.backfire.room.RoomActivity;
import com.backfire.utils.Log;


/**
 * @author WF
 *
 */
public class ActivityContainer{
	public enum ActivityType
	{
		LOBBY_ACTIVITY,
		REGISTER_ACTIVITY,
		EXERCISE_ACTIVITY,
		HELP_ACTIVITY,
		SHOP_ACTIVITY,
		ROOM_ACTIVITY,
		TASK_ACTIVITY,
		LUCK_ACTIVITY,
		HIGHTSCORE_ACTIVITY
	}
	
	//单例对象
	private static ActivityContainer activityNavigator = null;
	private Class<?> previousActivityClass;
	private ActivityBase currentActivity;
	
	private ActivityContainer()
	{
		//私有，防止外部调用
	}
	
	public static ActivityContainer getInstance()
	{
		if (activityNavigator == null) 
		{
			activityNavigator = new ActivityContainer();
		}
		return activityNavigator;
	}

	public void setCurrentActivity(ActivityBase currentActivity) {
		Log.i(this, "Current Activity is ", currentActivity);
		if (this.currentActivity!= null) {
			this.previousActivityClass = this.currentActivity.getClass();
		}
		this.currentActivity = currentActivity;
	}

	public ActivityBase getCurrentActivity() {
		return currentActivity;
	}
	
	public Class<?> getPreviousActivityClass() {
		return previousActivityClass;
	}
	
	public void PopActivity()
	{
		LoadingProgressDialog.dissmissDlg();
		if (this.previousActivityClass == null) {
			this.previousActivityClass = LobbyActivity.class;
		}
		Intent i = new Intent(this.currentActivity, this.previousActivityClass); 
		Log.d(this, this.currentActivity, this.previousActivityClass);
		//先关闭当前Activity，再切换到新Activity中，避免死锁
		this.currentActivity.startActivity(i);
		this.currentActivity.finish();
	}
	
	public void switchTo(Activity src, ActivityType target)
	{
		Class<?> tar = null;
		switch (target) {
			case LOBBY_ACTIVITY:
				tar = LobbyActivity.class;
				break;
			case EXERCISE_ACTIVITY:
				tar = ExerciseActivity.class;
				break;
			case HELP_ACTIVITY:
				tar = HelpActivity.class;
				break;
			case SHOP_ACTIVITY:
				tar = ShopActivity.class;
				break;
			case ROOM_ACTIVITY:
				tar = RoomActivity.class;
				break;
			case HIGHTSCORE_ACTIVITY:
				tar = HighScoreActivity.class;
				break;
			case TASK_ACTIVITY:
				tar = TaskActivity.class;
				break;
			case LUCK_ACTIVITY:
				tar = LuckActivity.class;
				break;
			default:
				break;
		}
		//&& !tar.isInstance(this.currentActivity) 特殊机型修改
		if(tar != null && src != null && !tar.isInstance(src) )
		{
	        LoadingProgressDialog.dissmissDlg();
			Intent i = new Intent(src, tar);
	        src.startActivity(i);
			src.finish();
		}
	}
}
