package com.wasu.cs.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.wasu.authsdk.AuthSDK;
import com.wasu.authsdk.IAuthInterface;
import com.wasu.module.log.WLog;
import com.wasu.statistics.StatisticsConstant;
import com.wasu.statistics.WasuStatistics;

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

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import basic.BuilderTypeManager.BuildType;
import cn.com.wasu.main.BuildConfig;
import cn.com.wasu.main.ChannelFlavor;
import cn.com.wasu.main.Common;
import cn.com.wasu.main.IntentConstant;
import cn.com.wasu.main.IntentMap;

/**
 * 程序入口，包括外调，程序启动后默认调用此界面
 * 
 * @author Administrator
 * 
 */
public class ActivityDispatch extends Activity {

	private String TAG = "ActivityDispatch";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		WLog.i(TAG,"onCreate()");

		setTheme(android.R.style.Theme_NoDisplay);

		parseIntent();

		//每个页面都添加TVID属性
		Map<String, String> map=new HashMap<String, String>();
		map.put(StatisticsConstant.TVID, AuthSDK.getInstance().getValue(IAuthInterface.KEY_TVID));
		WasuStatistics.getInstance().addPageElem(map);
		finish();
	}

	/**
	 * 解析intent
	 */
	private void parseIntent() {
		Intent i = getIntent();
		if (i == null) {
			i = new Intent();
		}

		String layoutCode = i.getStringExtra("layoutCode");
		String jsonUrl = i.getStringExtra("url");
		String extra = i.getStringExtra("extra");
		String value = i.getStringExtra("value");
//		layoutCode = "Detail_Movie";
//		jsonUrl = "https://120.26.137.228/?s=2002&p=sntAssetDetail&k=2&v=2&catId=221792&assetId=483275";

		Class<?> defautClass = null;

		/** 支持 **/
		if (!TextUtils.isEmpty(layoutCode) && !TextUtils.isEmpty(jsonUrl)){
			
			/**第三方widget接口调用以下布局，数据域名转换为4.0默认域名/v/key**/
			if(layoutCode == "Detail_Series"||layoutCode == "Detail_Movie"||layoutCode == "Detail_News"||layoutCode == "Player_Wasu"){
				URL url = null;
				String KEY = "k";
				String V = "v";
				
				try {
					url = new URL(jsonUrl);
				} catch (MalformedURLException e) {
					e.printStackTrace();
				}					
				if(!(BuildType.HTTP_DOMAIN).contains(url.getHost())){
					jsonUrl = BuildType.HTTP_DOMAIN + url.getFile();
					jsonUrl = jsonUrl.replaceAll("(" + KEY +"=[^&]*)", KEY + "=" + Common.URL_KEY);
					jsonUrl = jsonUrl.replaceAll("(" + V +"=[^&]*)", V + "=" + Common.URL_V);
				}

			}
			Intent intent = new Intent();
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			intent.putExtra(IntentConstant.BACK_MAIN.value(), true);
			IntentMap.startIntent(this, null, layoutCode, jsonUrl, defautClass);
			return;
		}else if (!TextUtils.isEmpty(layoutCode) && !TextUtils.isEmpty(extra)){
			String catId =  "";
			String assetId = "";
			try {
				JSONObject json = new JSONObject(extra);
				catId = json.optString("catId");
				assetId = json.optString("assetId");
			} catch (JSONException e) {
				e.printStackTrace();
			}

			if(!TextUtils.isEmpty(catId)) {
				StringBuilder url = new StringBuilder(BuildType.HTTP_DOMAIN + "?s=" + BuildType.SITE_ID );
				url.append("&p=" + (TextUtils.isEmpty(assetId) ? "sntCat" : "sntAssetDetail"));
				url.append("&k=" + Common.URL_KEY);
				url.append("&v=" + Common.URL_V);
				url.append("&catId=" + catId);
				if(!TextUtils.isEmpty(assetId)) {
					url.append("&assetId=" + assetId);
				}

				Intent intent = new Intent();
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

				//Add by Frewen.Wong 渡鸦科技返回直接退出应用，不需要返回到首页
				if (BuildConfig.FLAVOR.equalsIgnoreCase(ChannelFlavor.DUYA_TECH)) {
					intent.putExtra(IntentConstant.BACK_MAIN.value(), false);
				} else {
					intent.putExtra(IntentConstant.BACK_MAIN.value(), true);
				}

				IntentMap.startIntent(this, null, layoutCode, url.toString(), defautClass);
				return;
			}

		} else if (!TextUtils.isEmpty(layoutCode)) {
			IntentMap.startIntent(this, null, layoutCode, null, defautClass);
			return;
		}
// 		else if (TextUtils.isEmpty(layoutCode) && !TextUtils.isEmpty(value)) {  // 乐视专用，这里不需要
//			String catId = "";
//			String assetId = "";
//			int Type = 0;
//			boolean Isvideo = false;
//
//			try {
//				JSONObject json = new JSONObject(value);
//				Type = json.optInt("Type");
//				catId = json.optString("Categoryid");
//				assetId = json.optString("Id");
//				Isvideo = json.optBoolean("Isvideo");
//
//			} catch (JSONException e) {
//				e.printStackTrace();
//			}
//
//			switch (Type) {
//				case 1:
//					layoutCode = LayoutCodeMap.DETAIL_MOVIE;
//					break;
//				case 2:
//					layoutCode = LayoutCodeMap.DETAIL_NEWS;
//					break;
//				case 3:
//					layoutCode = LayoutCodeMap.Home_Vip;
//					break;
//				case 4:
//					layoutCode = LayoutCodeMap.Wasu_Favorite;
//					break;
//				case 5:
//					layoutCode = LayoutCodeMap.Wasu_History;
//					break;
//			}
//
//			if (!TextUtils.isEmpty(catId)) {
//				StringBuilder url = new StringBuilder(BuildType.HTTP_DOMAIN + "?s=" + BuildType.SITE_ID);
//				url.append("&p=" + (TextUtils.isEmpty(assetId) ? "sntCat" : "sntAssetDetail"));
//				url.append("&k=" + Common.URL_KEY);
//				url.append("&v=" + Common.URL_V);
//				url.append("&catId=" + catId);
//				if (!TextUtils.isEmpty(assetId)) {
//					url.append("&assetId=" + assetId);
//				}
//
//				Intent intent = new Intent();
//				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//				intent.putExtra(IntentConstant.BACK_MAIN.value(), false);
//
//				if (!TextUtils.isEmpty(layoutCode)) {
//					IntentMap.startIntent(this, intent, layoutCode, url.toString(), defautClass);
//				}
//				return;
//			}
//		}

		if (!AuthSDK.getInstance().isDeviceRegister() && !BuildType.HIDE_USERCENTER) {
			defautClass = ActivityRegister.class;
		} else {
			defautClass = ActivityWelcome.class;
		}

		IntentMap.startIntent(this, null, layoutCode, jsonUrl, defautClass);
	}
	

}
