/*
 * WR_Term.java
 * classes : cn.com.wasu.main.jsobject.WR_Term
 * @author Administrator
 * V 1.0.0
 * Create at 2014年10月16日 下午5:10:47
 */
package com.wasu.cs.jsobject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.webkit.JavascriptInterface;
import cn.com.wasu.main.IntentMap;

import com.wasu.authsdk.AuthSDK;
import com.wasu.authsdk.IAuthInterface;
import com.wasu.cs.ui.ActivityMain;

/**
 * cn.com.wasu.main.jsobject.WR_Term
 * @author zhangrm@wasu.com <br/>
 * create at 2014年10月16日 下午5:10:47
 */
public class WR_Term {
	private static final String TAG = "WR_Term";

	private Context mContext;

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

	@JavascriptInterface
	public String getTVID() {
		return AuthSDK.getInstance().getValue(IAuthInterface.KEY_TVID);
	}

	@JavascriptInterface
	public String getSoftAppVer() {
		try {
			return String.valueOf(mContext.getPackageManager().getPackageInfo(
					mContext.getPackageName(), 0).versionName);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

	@JavascriptInterface
	public String getSDKVersion() {
		return String.valueOf(Build.VERSION.SDK_INT);
	}

	@JavascriptInterface
	public void onQRResult(boolean paid) {

	}

	@JavascriptInterface
	public void callback_alipay(boolean isSuccess) {

	}

	/**
	 * 注意，这个方法可能导致两个页面循环
	 */
//	@JavascriptInterface
//	public void goBack() {
//		if (mContext instanceof ActivityWelcome) {
//			ActivityWelcome act = (ActivityWelcome) mContext;
//			act.goBack();
//		}
//	}

	@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);
		}
	}
	
	/**
	 * 返回主界面
	 */
	@JavascriptInterface
	public void closePage() {
		if (mContext instanceof Activity) {
			((Activity) mContext).finish();
		}
	}

	//@JavascriptInterface
//	public boolean isOpenedByAction() {
//		if (mContext instanceof ActivityWelcome) {
//			ActivityWelcome act = (ActivityWelcome) mContext;
//			return act.isOpenByAction();
//		}
//		return false;
//	}

}
