/*
 * WR_UserCenter.java
 * classes : cn.com.wasu.main.jsobject.WR_UserCenter
 * @author Administrator
 * V 1.0.0
 * Create at 2014年12月13日 下午1:16:21
 */
package com.wasu.cs.jsobject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.webkit.JavascriptInterface;

import com.wasu.authsdk.AuthListener;
import com.wasu.authsdk.AuthSDK;
import com.wasu.authsdk.IAuthInterface;
import com.wasu.common.WError;
import com.wasu.cs.ui.ActivityMain;
import com.wasu.module.log.WLog;
import com.wasu.statistics.StatisticsConstant;
import com.wasu.statistics.WasuStatistics;
import com.wasu.util.ConfigUtils;

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

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

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

/**
 * cn.com.wasu.main.jsobject.WR_UserCenter
 * 
 * @author junz@wasu.com <br/>
 *         create at 2014年12月13日 下午1:16:21
 */
public class WR_UserCenter {
	
	public static final String CFG_FILE = "usercenter";
	
	public static final String HEADURL = "headUrl";
	public static final String LOGINTYPE = "loginType";
	public static final String EXPAND = "expand";
	
	private static final String TAG = "WR_UserCenter";
	
	private Context mContext;

	public WR_UserCenter(Context context) {
		this.mContext = context;
	}

//	@JavascriptInterface
//	public void closePage() {
//		((UserCenterActivity) mContext).finish();
//	}
	
	/**
	 * 获取设备相关信息
	 * 
	 * @return
	 */
	@JavascriptInterface
	public String getDeviceInfo() {
		WLog.i(TAG, "getDeviceInfo");
		Map<String, String> map = new HashMap<String, String>();
		WLog.i(TAG, "IAuthInterface.KEY_TVID");
		map.put("tvId", AuthSDK.getInstance().getValue(IAuthInterface.KEY_TVID));
		WLog.i(TAG, "IAuthInterface.KEY_DEVICEID");
		map.put("deviceId", AuthSDK.getInstance().getValue(IAuthInterface.KEY_DEVICEID));
		WLog.i(TAG, "IAuthInterface.KEY_PUBLICKEY");
		map.put("publicKey", AuthSDK.getInstance().getValue(IAuthInterface.KEY_PUBLICKEY));
		WLog.i(TAG, "IAuthInterface.KEY_ENCRYPTV");
		map.put("encryptV", AuthSDK.getInstance().getValue(IAuthInterface.KEY_ENCRYPTV));
		WLog.i(TAG, "NetUtils.getMacAddress(mContext) ");
		/*if(StringUtils.isEmpty(Common.mac)){
			Common.mac=NetUtils.getMacAddress(mContext);
			map.put("mac", Common.mac);
			//map.put("mac", NetUtils.getMacAddress(mContext));
		}else{
			map.put("mac", Common.mac);
		}*/
		map.put("mac", AuthSDK.getInstance().getValue(IAuthInterface.KEY_MAC));
		
		map.put("siteId", BuildType.SITE_ID);
		map.put("sdkVersion", Common.SDK_VERSION);

		String jsonStr = "{";
		Set<?> keySet = map.keySet();
		for (Object key : keySet) {
			jsonStr += "\"" + key + "\":\"" + map.get(key) + "\",";
		}
		jsonStr = jsonStr.substring(0, jsonStr.length() - 1);
		jsonStr += "}";
		WLog.i(TAG, "getDeviceInfo end");
		return jsonStr;
	}

	/**
	 * 获取用户相关信息
	 * 
	 * @return
	 */
	@JavascriptInterface
	public String getUserInfo() {
		WLog.i(TAG, "getUserInfo");
		Map<String, String> map = new HashMap<String, String>();
		map.put("phone", AuthSDK.getInstance().getValue(IAuthInterface.KEY_PHONE));
		map.put("token", AuthSDK.getInstance().getValue(IAuthInterface.KEY_TOKEN));
		map.put("userKey", AuthSDK.getInstance().getValue(IAuthInterface.KEY_USERKEY));
		map.put("headUrl", ConfigUtils.getString(mContext, CFG_FILE, HEADURL));
		map.put("loginType", ConfigUtils.getString(mContext, CFG_FILE, LOGINTYPE));
		map.put("expand", ConfigUtils.getString(mContext, CFG_FILE, EXPAND));

		String jsonStr = "{";
		Set<?> keySet = map.keySet();
		for (Object key : keySet) {
			jsonStr += "\"" + key + "\":\"" + map.get(key) + "\",";
		}
		jsonStr = jsonStr.substring(0, jsonStr.length() - 1);
		jsonStr += "}";
		WLog.i(TAG, "getUserInfo end");
		return jsonStr;
	}

	/**
	 * 保存用户相关信息
	 * 
	 * @return
	 */
	@JavascriptInterface
	public void setUserInfo(String jsonData) {
		WLog.i(TAG, "setUserInfo jsonData="+jsonData);
		String phone = "";
		String token = "";
		String userKey = "";
		String headUrl = "";
		String loginType = "";
		String expand = "";
		/**用户是否为VIP**/
		String vipState="";
		/**用户VIP到期时间**/
		String vipExpireTime = "";

		try {
			JSONObject json = new JSONObject(jsonData);
			phone = json.optString("phone", "");
			token = json.getString("token");
			userKey = json.getString("userKey");
			headUrl = json.optString("headUrl", "");
			loginType = json.optString("loginType", "");
			expand = json.optString("expand", "");
			vipState=json.optString("vipState","0");
			vipExpireTime = json.optString("vipExpireTime","");


			AuthSDK.getInstance().saveValue(IAuthInterface.KEY_PHONE, phone);
			AuthSDK.getInstance().saveValue(IAuthInterface.KEY_TOKEN, token);
			AuthSDK.getInstance().saveValue(IAuthInterface.KEY_USERKEY, userKey);
			AuthSDK.getInstance().saveValue("vipState", vipState);
			AuthSDK.getInstance().saveValue("vipExpireTime", vipExpireTime);

			AuthSDK.getInstance().saveValue("headUrl", headUrl);
			AuthSDK.getInstance().saveValue("loginType", loginType);
			AuthSDK.getInstance().saveValue("expand", expand);

			WasuStatistics.getInstance().setValue(StatisticsConstant.USERKEY, userKey);

			//ADModule.getInstance().setAdFree(false);
			
			ConfigUtils.saveData(mContext, CFG_FILE, HEADURL, headUrl);
			ConfigUtils.saveData(mContext, CFG_FILE, LOGINTYPE, loginType);
			ConfigUtils.saveData(mContext, CFG_FILE, EXPAND, expand);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		WLog.i(TAG, "setUserInfo end");
	}

	/**
	 * 页面出现加密串等失效后通知客户端刷新
	 * 
	 * @param errorCode
	 * @param extra
	 * @return
	 */
	@JavascriptInterface
	public boolean OnError(int errorCode, String extra) {
		WLog.i(TAG, "OnError");
		switch (errorCode) {
		case 1:  //加密串过期
			AuthSDK.getInstance().deviceLogin(new AuthListener() {

				@Override
				public void result(int ret, String extra, Object retData) {
					if (ret == WError.ERROR_NONE) {
						Log.i(TAG, "login success!!!!!");
					}
				}
				
			});
			return true;
		case 2:  //token失效
		case 3:  //userKey无效
			return false;
		default:
			return false;
		}
	}

	/**
	 * danxingxi
	 * 为了在营业厅的任何页面都可以直接返回到首页
	 */
	@JavascriptInterface
	public void goToHome() {
		if (mContext instanceof Activity) {
			Activity web = (Activity)mContext;
			web.finish();

			Intent i = new Intent();
			i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			IntentMap.startIntent(web, i, null, null, ActivityMain.class);
		}
	}

	/**
	 * 关闭加载webview的activity
	 */
	@JavascriptInterface
	public void closePage() {
		if (mContext instanceof Activity) {
			((Activity) mContext).finish();
		}
	}
}
