package cn.com.wasu.main;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.Toast;

import com.wasu.cs.module.InstallOtherAppModule;
import com.wasu.cs.ui.ActivityActSpecial;
import com.wasu.cs.ui.ActivityBrandArea;
import com.wasu.cs.ui.ActivityCNLTVCSModel;
import com.wasu.cs.ui.ActivityCartoon;
import com.wasu.cs.ui.ActivityCartoonStar;
import com.wasu.cs.ui.ActivityChannel;
import com.wasu.cs.ui.ActivityChildren;
import com.wasu.cs.ui.ActivityChildrenAllLike;
import com.wasu.cs.ui.ActivityChildrenDetail;
import com.wasu.cs.ui.ActivityChildrenFav;
import com.wasu.cs.ui.ActivityChildrenSong;
import com.wasu.cs.ui.ActivityColumn;
import com.wasu.cs.ui.ActivityColumnDetail;
import com.wasu.cs.ui.ActivityDetail;
import com.wasu.cs.ui.ActivityFavAndHistory;
import com.wasu.cs.ui.ActivityFeiHuCs;
import com.wasu.cs.ui.ActivityFilmSpecial;
import com.wasu.cs.ui.ActivityFilter;
import com.wasu.cs.ui.ActivityFourKSpecial;
import com.wasu.cs.ui.ActivityLoopPlayer;
import com.wasu.cs.ui.ActivityMain;
import com.wasu.cs.ui.ActivityNeteaseNews;
import com.wasu.cs.ui.ActivityNewFilmSpecial;
import com.wasu.cs.ui.ActivityOpenApp;
import com.wasu.cs.ui.ActivityPlayer;
import com.wasu.cs.ui.ActivitySearch;
import com.wasu.cs.ui.ActivityShortVideoSpecial;
import com.wasu.cs.ui.ActivityUserCenter;
import com.wasu.cs.ui.ActivityWeb;
import com.wasu.cs.widget.ActivityLiveVideoView;
import com.wasu.module.log.WLog;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import basic.taobaotv.view.ActivityTaobaoTv;

import static com.wasu.util.ApkUtils.checkApkExist;

public class IntentMap {
	private static final String tag = "IntentMap";
	private static IntentMap mInstance = new IntentMap();
	
	private Map<String, Class<?>> mClazzMap;
	
	public IntentMap(){
		mClazzMap = new HashMap<String, Class<?>>();

		mClazzMap.put("Home", 					ActivityMain.class);
		mClazzMap.put("Home_Vip", 				ActivityMain.class);
		mClazzMap.put("Detail_Subject", 		ActivityWeb.class);
		mClazzMap.put("Detail_Series", 			ActivityDetail.class);
		mClazzMap.put("Detail_Movie", 			ActivityDetail.class);
		mClazzMap.put("Detail_News", 			ActivityPlayer.class);
		mClazzMap.put("Player_Wasu", 			ActivityPlayer.class);
		mClazzMap.put("Link", 					ActivityWeb.class);
		mClazzMap.put("LINK_TAOBAO_TV", 		ActivityTaobaoTv.class);
		mClazzMap.put("Wasu_Favorite", 			ActivityFavAndHistory.class);
		mClazzMap.put("Wasu_History", 			ActivityFavAndHistory.class);
		mClazzMap.put("Wasu_UserCenter", 		ActivityUserCenter.class);
		mClazzMap.put("Wasu_Search", 			ActivitySearch.class);
		mClazzMap.put("Wasu_Filter", 			ActivityFilter.class);
		//打开第三方APK
		mClazzMap.put("OpenToApp", 				ActivityOpenApp.class);
		mClazzMap.put("Player_Wasu_Loop", 		ActivityLoopPlayer.class);
		mClazzMap.put("Movie_Subject_TP1",		ActivityFilmSpecial.class);
		mClazzMap.put("MOVIE_SUBJECT_TP2",		ActivityNewFilmSpecial.class);
		mClazzMap.put("News_Subject_TP1",		ActivityShortVideoSpecial.class);
		mClazzMap.put("Act_Subject_TP1", 		ActivityActSpecial.class);
        mClazzMap.put("Category_Movie", 		ActivityChannel.class);
        mClazzMap.put("Category_Series", 		ActivityChannel.class);
        mClazzMap.put("Category_News", 		    ActivityChannel.class);
        mClazzMap.put("Column_Detail_List",     ActivityColumnDetail.class);
        /**兼容新CMS从经纬导入的数据结构**/
        mClazzMap.put("Column_Detail_List1",    ActivityColumn.class);
        /**4K专区**/
        mClazzMap.put("4K_Subject_TP1",    ActivityFourKSpecial.class);
		/**中国蓝CS模板*/
		mClazzMap.put("CNLTV_Subject", ActivityCNLTVCSModel.class);
		//网易新闻专区
		mClazzMap.put("NeteaseNews_Subject", ActivityNeteaseNews.class);
		mClazzMap.put("Children_Channel",       ActivityChildren.class);            //少儿首页
		mClazzMap.put("Children_Cartoon",       ActivityCartoon.class);             //动画列表
		mClazzMap.put("Children_BrandArea",     ActivityBrandArea.class);           //品牌专区
		mClazzMap.put("Children_CartoonStar",   ActivityCartoonStar.class);         //动画明星
		//儿歌页
		mClazzMap.put("Children_Song", ActivityChildrenSong.class);
		//少儿详情
		mClazzMap.put("Children_Detail", ActivityChildrenDetail.class);
		//少儿收藏
		mClazzMap.put("Children_Fav", ActivityChildrenFav.class);
		//大家都爱看
		mClazzMap.put("Children_All_Like", ActivityChildrenAllLike.class);
		/**飞狐CS模板*/
		mClazzMap.put("Fei_Hu", ActivityFeiHuCs.class);
		mClazzMap.put("LiveActivity", ActivityLiveVideoView.class);
	}
	
	/**
	 * 对外提供的打开方式
	 * @param i					默认的intent，可以预先初始化一些基本参数
	 * @param layoutCode		对应的layoutCode
	 * @param jsonUrl			对应的code地址
	 * @param defautClass		如果找不到layoutcode，对应打开的class
	 * 		true标示成功，false标示失败
	 */
	public static boolean startIntent(Context c, Intent i, String layoutCode, String jsonUrl, Class<?> defautClass){
		return mInstance._startIntent(c, i, layoutCode, jsonUrl, defautClass);
	}
	
	/**
	 * 内部使用，参数同上
	 * @param i
	 * @param layoutCode
	 * @param jsonUrl
	 * @param defautClass
	 * @return
	 * 		true标示成功，false标示失败
	 */
	private boolean _startIntent(Context c, Intent i, String layoutCode, String jsonUrl, Class<?> defautClass){
		if (i == null){
			i = new Intent();
		}
		
		WLog.d(tag, "start intent, laycode=" + layoutCode + ";jsonUrl=" + jsonUrl);

		if(!TextUtils.isEmpty(layoutCode) && "OpenApp".equalsIgnoreCase(layoutCode) && !TextUtils.isEmpty(jsonUrl)){ // 第三方APK接入
			JSONObject jsonObj = null;
			try {
				jsonObj = new JSONObject(jsonUrl);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String actionName = jsonObj.optString("actionName");
			String packageName = jsonObj.optString("packageName");
			String download = jsonObj.optString("download");
			WLog.d(tag, "actionName  "+actionName);
			WLog.d(tag, "packageName  "+packageName);
			WLog.d(tag, "download  "+download);
			if (!checkApkExist(c, packageName)) {// 如果没有安装
				if (!TextUtils.isEmpty(download)) {
					Toast.makeText(c, "正在下载，请稍候", Toast.LENGTH_SHORT).show();
					InstallOtherAppModule.getInstance().init(c);
					InstallOtherAppModule.getInstance().dowanloadApk(download);
				} else {
					// 无下载信息
					Toast.makeText(c, "无下载信息", Toast.LENGTH_SHORT).show();
				}

			} else {// 如果已经安装直接打开
				Intent intent = new Intent();
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				//打开第三方APK的layout为OpenToApp，服务器下发的数据时layout字段为OpenApp
				IntentMap.startIntent(c, intent, "OpenToApp", jsonUrl, null);
				if (c instanceof Activity){
					Activity tmp = (Activity) c;
					tmp.overridePendingTransition(R.anim.activity_down_in, R.anim.activity_down_out);
				}
			}
			return true;
		}
		
		Class<?> realClazz = defautClass;

		if (!TextUtils.isEmpty(layoutCode)){
			//possible exception，trim()返回一个新的String
			 String mlayoutCode=layoutCode.trim();
			if (!TextUtils.isEmpty(mlayoutCode)){
				realClazz = mClazzMap.get(mlayoutCode);
				i.putExtra(IntentConstant.LAYOUT_CODE.value(), mlayoutCode);
			}

		}
		if (!TextUtils.isEmpty(jsonUrl)){
			//possible exception，trim()返回一个新的String
			String mjsonUrl=jsonUrl.trim();
			i.putExtra(IntentConstant.DATAURI.value(), mjsonUrl);
		}
		
		if (realClazz == null){
			realClazz = defautClass;
		}
		
		if (realClazz == null){
			return false;
		}
		
		i.setClass(c, realClazz);
		
		c.startActivity(i);
		
		if (c instanceof Activity){
			Activity tmp = (Activity) c;
			tmp.overridePendingTransition(R.anim.activity_down_in, R.anim.activity_down_out);
		}
		
		return true;
	}
}
