package com.wasu.cs.widget.viplogin;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.Window;

import com.wasu.authsdk.AuthSDK;
import com.wasu.authsdk.IAuthInterface;
import com.wasu.comp.wasuwebview.WR;
import com.wasu.comp.wasuwebview.WR_Favorites;
import com.wasu.comp.wasuwebview.WR_History;
import com.wasu.comp.wasuwebview.WR_Term;
import com.wasu.comp.wasuwebview.WR_UserCenter;
import com.wasu.comp.wasuwebview.tv;
import com.wasu.util.SystemUtils;

public class VipDialogLogin extends Dialog {
	private static final String tag = "DialogLogin";

	private LoginStatusListener mLoginStatusListener;

	private WasuVipWebView mWebView;

	private String mLoginUrl;

	public interface LoginStatusListener {

		/**
		 * 登录状态回调
		 * 
		 * @param success
		 *            : 是否成功
		 */
		public void onLogStatus(boolean success);
	}
//
//	private LoginStateListener loginState = new LoginStateListener() {
//		@Override
//		public void onLogStatus(boolean success) {
//			if (mLoginStatusListener == null) {
//				return;
//			}
//			if (TextUtils.isEmpty(AuthSDK.getInstance().getValue(IAuthInterface.KEY_USERKEY))) {
//				mLoginStatusListener.onLogStatus(success);
//			} else {
//				mLoginStatusListener.onLogStatus(success);
//			}
//
//			mLoginStatusListener = null;
//			dismiss();
//		}
//	};

	public VipDialogLogin(Context context, WasuVipWebView webview, String loginUrl) {
		super(context);
		this.mWebView = webview;
		this.mLoginUrl = loginUrl;
		init(context);
	}

	public void setLoginStatusListener(LoginStatusListener l) {
		this.mLoginStatusListener = l;
	}

	@SuppressLint("SetJavaScriptEnabled")
	private void init(Context context) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setBackgroundDrawableResource(android.R.color.transparent);

		setContentView(mWebView);

		Rect screenRect = SystemUtils.getScreenSize(getContext());
		getWindow().setLayout(screenRect.width(), screenRect.height());

		mWebView.addJavascriptInterface(new WR(getContext()), "WR");
		mWebView.addJavascriptInterface(new WR_Term(getContext(), mWebView, this), "WR_Term");
		mWebView.addJavascriptInterface(new WR_History(getContext()), "WR_History");
		mWebView.addJavascriptInterface(new WR_Favorites(getContext()), "WR_Favorites");
		mWebView.addJavascriptInterface(new tv(getContext()), "tv");
		mWebView.addJavascriptInterface(new WR_UserCenter(getContext(),this), "WR_UserCenter");

		setCancelable(true);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Dialog#onStart()
	 */
	@Override
	protected void onStart() {
		super.onStart();
		openPayPage();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Dialog#onStop()
	 */
	@Override
	protected void onStop() {
		super.onStop();
		if (mLoginStatusListener != null) {
			if (TextUtils.isEmpty(AuthSDK.getInstance().getValue(IAuthInterface.KEY_USERKEY))) {
				mLoginStatusListener.onLogStatus(false);
			} else {
				mLoginStatusListener.onLogStatus(true);
			}
			mLoginStatusListener = null;
		}
		mWebView.loadUrl("about:blank");
	}

	private void openPayPage() {
		mWebView.loadUrl(mLoginUrl);
	}

	public WasuVipWebView getWebView() {
		return mWebView;
	}
}
