package com.backfire.popupwindow;

import net.yeeyo.doudizhu.feiyu.R;
import android.content.Context;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.backfire.doudizhu.ActivityBase;
import com.backfire.doudizhu.lobby.LobbyActivity;
import com.backfire.room.chat.ExpressionConstants;
import com.backfire.utils.Log;

public class IconSelectPopupWindow extends BasePopupWindow{
	private View view;
	private int width;
	private int height;
	
	private GridView gridView; // 网格，显示表情
	private ImageView icon_select_close;
	/**
	 * @param activity
	 */
	public IconSelectPopupWindow(ActivityBase activity) {
		super(activity);
		this.initSetting();
	}
	private void initSetting(){
		LayoutInflater mLayoutInflater = (LayoutInflater) mActivity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		int layoutRourse = R.layout.icon_select_pop;
		view = mLayoutInflater.inflate(layoutRourse, null);
		
		view.setFocusableInTouchMode(true);
		 
		gridView = (GridView) view.findViewById(R.id.gridview);
		icon_select_close = (ImageView) view.findViewById(R.id.icon_select_close);
		gridView.setAdapter(new ImageAdapter(mActivity));
		
		this.setFocusable(true);
		this.setConsumeKeyEvent(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 LobbyActivity context) {

		icon_select_close.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				CloseWindow();
				
				mActivity.playBtnPressMusic();
			}
			
		});
		view.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				CloseWindow();
				return false;
			}
		});
		gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				int gid = (int) arg3+1;
				Log.v(this, "icon:"+gid);
				if(gid>0){
					CloseWindow();
					ModifyUserInfoPop modify = new ModifyUserInfoPop(context,gid+"");
					modify.setListens(context);
					modify.ShowWindowInCenter();
				}
				mActivity.playBtnPressMusic();
			}
		});

		
	}
	public class ImageAdapter extends BaseAdapter {
		public ImageAdapter(Context c) {
			mContext = c;
		}

		public int getCount() {
			return ExpressionConstants.icons.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView = null;
			if (convertView == null) {
				imageView = new ImageView(mContext);
				imageView.setAdjustViewBounds(true);
				imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
				imageView.setPadding(1, 1, 1, 1);
			} else {
				imageView = (ImageView) convertView;
			}

			imageView.setImageResource(ExpressionConstants.icons[position]);
			return imageView;
		}

		private Context mContext;

	}
	@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
		
	}
}



