package com.backfire.popupwindow;

import net.yeeyo.doudizhu.feiyu.R;
import android.content.Context;
import android.text.Html;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.backfire.doudizhu.ActivityBase;
import com.backfire.doudizhu.common.DeviceUtil;

public class SelectPopupWindow extends BasePopupWindow{
	private View view;
	private int width;
	private int height;
	
	private TextView notify_message;
	private Button select_submit;
	private Button select_cancel;
	/**
	 * @param activity
	 */
	public SelectPopupWindow(ActivityBase activity,String msg) {
		super(activity);
		this.initSetting(msg,0,true);
	}
	public SelectPopupWindow(ActivityBase activity,String msg,int flag,boolean keyEvent) {
		super(activity);
		this.initSetting(msg,flag,keyEvent);
	}
	public SelectPopupWindow(ActivityBase activity,String msg,int flag) {
		super(activity);
		this.initSetting(msg,flag,true);
	}
	private void initSetting(String msg,int flag,boolean keyEvent){
		LayoutInflater mLayoutInflater = (LayoutInflater) mActivity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		int layoutRourse = R.layout.select_pop;
		if(length(msg)<50){
			layoutRourse = R.layout.select_pop_1;
		}
		view = mLayoutInflater.inflate(layoutRourse, null);
		
		view.setFocusableInTouchMode(true);
		 
		notify_message = (TextView) view.findViewById(R.id.notify_message);
		select_submit = (Button) view.findViewById(R.id.select_submit);
		select_cancel = (Button) view.findViewById(R.id.select_cancel);
		((Button)view.findViewById(R.id.select_submit)).getPaint().setFakeBoldText(true);
		((Button)view.findViewById(R.id.select_cancel)).getPaint().setFakeBoldText(true);
		
		if(msg!=null){
			if(msg.length()<22){
				notify_message.setTextSize(TypedValue.COMPLEX_UNIT_PX,DeviceUtil.AdaptScreenWidth(mActivity, 20));
				notify_message.setGravity(Gravity.CENTER);
			}else{
				notify_message.setTextSize(TypedValue.COMPLEX_UNIT_PX,DeviceUtil.AdaptScreenWidth(mActivity, 16));
				notify_message.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
			}
			notify_message.setText(Html.fromHtml(msg));
		}
		switch(flag){
			case 1:
				select_submit.setText("重试");
				break;
			case 2:
				select_submit.setText("充值");
				break;
			case 3:
				select_submit.setText("升级");
				break;
			case 4:
				select_submit.setText("退出游戏");
				select_cancel.setText("继续游戏");
				break;
			case 5:
				select_submit.setText("退出游戏");
				select_cancel.setText("抽奖");
				break;
			default :select_submit.setText("确定");break;
		}
		this.setFocusable(true);
		this.setConsumeKeyEvent(keyEvent);
		ImageView popupview = (ImageView) view.findViewById(R.id.select_bg);
		width = popupview.getLayoutParams().width;
		height = popupview.getLayoutParams().height;
		this.createPopupWindow(view, width, height);
	}
	/**
	 * 为各组件设置监听器
	 */
	public void setListens(final OnClickListener submit,final OnClickListener close) {
		select_submit.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				CloseWindow();
				if(submit!=null)submit.onClick(arg0);
				
				mActivity.playBtnPressMusic();
			}
			
		});
		select_cancel.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				CloseWindow();
				if(close!=null)close.onClick(arg0);
				
				mActivity.playBtnPressMusic();
			}
			
		});
		view.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if(isConsumeKeyEvent())CloseWindow();
				return false;
			}
		});
		
	}
	
	@Override
	protected boolean popupWindowWillShow() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	protected void popupWindowDidShow() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void popupWindowWillDissmiss() {
		// TODO Auto-generated method stub
		
	}
	 public boolean isLetter(char c) {
	        int k = 0x80;
	        return c / k == 0 ? true : false;
	    }

	  /**
	     * 得到一个字符串的长度,显示的长度,一个汉字或日韩文长度为2,英文字符长度为1
	     * 
	     * @param String
	     *            s ,需要得到长度的字符串
	     * @return int, 得到的字符串长度
	     */
	    public int length(String s) {
	        if (s == null)
	            return 0;
	        char[] c = s.toCharArray();
	        int len = 0;
	        for (int i = 0; i < c.length; i++) {
	            len++;
	            if (!isLetter(c[i])) {
	                len++;
	            }
	        }
	        return len;
	    }


}
