package com.backfire.popupwindow;

import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;

import com.backfire.doudizhu.ActivityBase;
import com.backfire.utils.Log;

/**
 * Backfire的PopupWindow基类，提供了默认构造方法。
 * 通常在构造方法里只储存当前的Activity，
 * 在ShowWindowAtLocation(int x, int y)中载入界面资源，并显示在指定位置。
 * 
 * @author wf
 *
 */
public abstract class BasePopupWindow {

	//activity
	protected ActivityBase mActivity;
	private PopupWindow mPopupWindow;
	private boolean isConsumeKeyEvent = false; //默认不相应返回按键
	private boolean isFocusable = false; //默认为非模态
	
	
	protected OnKeyListener keyListener = new OnKeyListener() {
		
		@Override
		public boolean onKey(View v, int keyCode, KeyEvent event) {
			if (isConsumeKeyEvent) {
				if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {;
					CloseWindow();
					return true;
				}
			}
			return false;
		}
	};
	
	public BasePopupWindow(ActivityBase activity)
	{
		this.mActivity = activity;
	}
	
	protected final void createPopupWindow(View v, int width, int height) {
		this.mPopupWindow = new PopupWindow(v, width, height, false);
		this.mPopupWindow.setOnDismissListener(new OnDismissListener() {
			@Override
			public void onDismiss() {
				popupWindowWillDissmiss();
			}
		});
	}
	
	protected abstract boolean popupWindowWillShow();
	protected abstract void popupWindowDidShow();
	protected abstract void popupWindowWillDissmiss();
	
	protected final void ShowWindowAtLocation(int gravity, int x, int y){
		if (this.popupWindowWillShow()) {
			mPopupWindow.setFocusable(this.isFocusable());
			//这里取的是左上角
			mPopupWindow.showAtLocation(mActivity.getWindow().getDecorView(), gravity, x, y);
			mPopupWindow.update();
			if (this.isConsumeKeyEvent()) {
				this.mPopupWindow.getContentView().setFocusable(true);
				this.mPopupWindow.getContentView().setFocusableInTouchMode(true);
				this.mPopupWindow.getContentView().setOnKeyListener(keyListener);
			}
			this.popupWindowDidShow();
		}
	}
	
	public final void ShowWindowAtLocation(int x, int y)
	{
		if (!this.mActivity.isFinishing() && this.mPopupWindow != null) {
			this.ShowWindowAtLocation(Gravity.NO_GRAVITY, x, y);
		}
		else{
			Log.e(this, "Error: ", this.mActivity.isFinishing(), this.mPopupWindow);
		}
	}
	
	public final void ShowWindowInCenter()
	{
		if (!this.mActivity.isFinishing() && this.mPopupWindow != null) {
			this.ShowWindowAtLocation(Gravity.CENTER, 0, 0);
		}
		else{
			Log.e(this, "Error: ", this.mActivity.isFinishing(), this.mPopupWindow);
		}
	}

	public void CloseWindow()
	{
		if(null != mPopupWindow && mPopupWindow.isShowing())
		{
			mPopupWindow.dismiss();
		}
	}
	
	public boolean isShowing()
	{
		boolean result = false;
		if (null != mPopupWindow) {
			result = mPopupWindow.isShowing();
		}
		return result;
	}
	
	public ActivityBase getmActivity() {
		return mActivity;
	}

	public void setmActivity(ActivityBase mActivity) {
		this.mActivity = mActivity;
	}
	
	public final boolean isFocusable() {
		return isFocusable;
	}

	public final void setFocusable(boolean isFocusable) {
		this.isFocusable = isFocusable;
	}

	protected final boolean isConsumeKeyEvent() {
		return isConsumeKeyEvent;
	}

	protected final void setConsumeKeyEvent(boolean isConsumeKeyEvent) {
		this.isConsumeKeyEvent = isConsumeKeyEvent;
	}

}
