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.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
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 BuyTipPopupWindow 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 BuyTipPopupWindow(ActivityBase activity,String msg) {
		super(activity);
		this.initSetting(msg,0);
	}
	public BuyTipPopupWindow(ActivityBase activity,String msg,int flag) {
		super(activity);
		this.initSetting(msg,flag);
	}
	private void initSetting(String msg,int flag){
		LayoutInflater mLayoutInflater = (LayoutInflater) mActivity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		int layoutRourse = R.layout.buy_tip_pop;
		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()<12){
				notify_message.setTextSize(TypedValue.COMPLEX_UNIT_PX,DeviceUtil.AdaptScreenWidth(mActivity, 20));
				notify_message.setGravity(Gravity.CENTER);
			}else if(msg.length()<40){
				notify_message.setTextSize(TypedValue.COMPLEX_UNIT_PX,DeviceUtil.AdaptScreenWidth(mActivity, 16));
				notify_message.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
			}else{
				notify_message.setTextSize(TypedValue.COMPLEX_UNIT_PX,DeviceUtil.AdaptScreenWidth(mActivity, 12));
				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;
			default :select_submit.setText("确定");break;
		}
		this.setFocusable(true);
		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();
			}
			
		});
		
	}
	
	@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
		
	}

}

