package com.backfire.popupwindow;

import net.yeeyo.doudizhu.feiyu.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

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

public class TableTroducePopupWindow extends BasePopupWindow{
	private View view;
	private int width;
	private int height;
	
	private TextView title;
	private TextView checkbox_introduce_txt;
	private TextView introduce_txt;
	private Button info_submit;
	private ImageView info_close;
	private CheckBox checkbox_introduce;
	private ImageView introduce_img;
	private String key;
	/**
	 * @param activity
	 */
	public TableTroducePopupWindow(ActivityBase activity,String msg,Bitmap bitmap,String key) {
		super(activity);
		this.initSetting(msg,bitmap,null,key);
	}
	public TableTroducePopupWindow(ActivityBase activity,String msg,String desc,String key) {
		super(activity);
		this.initSetting(msg,null,desc,key);
	}
	private void initSetting(String msg,Bitmap bitmap,String desc,String key){
		try {
			LayoutInflater mLayoutInflater = (LayoutInflater) mActivity
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			int layoutRourse = R.layout.table_troduce_pop;
			view = mLayoutInflater.inflate(layoutRourse, null);
			
			view.setFocusableInTouchMode(true);
			
			this.key = key;
			 
			title = (TextView) view.findViewById(R.id.title);
			checkbox_introduce_txt = (TextView) view.findViewById(R.id.checkbox_introduce_txt);
			introduce_txt = (TextView) view.findViewById(R.id.introduce_txt);
			info_submit = (Button) view.findViewById(R.id.info_submit);
			info_close = (ImageView) view.findViewById(R.id.info_close);
			checkbox_introduce = (CheckBox) view.findViewById(R.id.checkbox_introduce);
			introduce_img = (ImageView) view.findViewById(R.id.introduce_img);
			
			title.setText(msg);
			if(bitmap!=null){
				introduce_img.setBackgroundDrawable(new BitmapDrawable(bitmap));
				introduce_img.setVisibility(View.VISIBLE);
				introduce_txt.setVisibility(View.GONE);
			}else{
				introduce_img.setVisibility(View.GONE);
				introduce_txt.setVisibility(View.VISIBLE);
				introduce_txt.setText(desc);
			}
			
			this.setFocusable(true);
			this.setConsumeKeyEvent(true);
			ImageView popupview = (ImageView) view.findViewById(R.id.info_bg);
			width = popupview.getLayoutParams().width;
			height = popupview.getLayoutParams().height;
			this.createPopupWindow(view, width, height);
		} catch (Exception e) {
			Log.e(this, e);
		}
	}
	
	/**
	 * 为各组件设置监听器
	 */
	public void setListens() {
		info_submit.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				CloseWindow();
				
				mActivity.playBtnPressMusic();
			}
			
		});
		info_close.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				CloseWindow();
				
				mActivity.playBtnPressMusic();
			}
			
		});
		checkbox_introduce_txt.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				if(checkbox_introduce.isChecked()){
					checkbox_introduce.setChecked(false);
				}else{
					checkbox_introduce.setChecked(true);
				}
			}
			
		});
		checkbox_introduce.setOnCheckedChangeListener(new OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean ischecked) {
				if(checkbox_introduce.isChecked()){
					ShareData.getInstance().saveBooleanValue(key, true);
				}else{
					ShareData.getInstance().saveBooleanValue(key, 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
		
	}
}

