package com.backfire.popupwindow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.yeeyo.doudizhu.feiyu.R;
import android.content.Context;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.backfire.doudizhu.common.DdzApplication;
import com.backfire.doudizhu.userinfo.UserInfo;
import com.backfire.doudizhu.userinfo.UserMessageCenter;
import com.backfire.room.RoomActivity;
import com.backfire.room.chat.ExpressionConstants;
import com.backfire.room.data.RoomInfo;

public class ChatPopupWindow extends BasePopupWindow {
	private ListView listView; // 列表（显示常用语和聊天记录）
	private EditText contentText; // 发送聊天内容框
	private Button completeButton; // 完成按钮
	private String commons[]; // 常用语数据
	private List<Map<String, String>> commons_list;
	private static String oldString;
	private boolean softKeyShow;
	private View view;
	private int width;
	private int height;
	private ChatMessageAdapter commonAdapter;
	/**
	 * @param activity
	 */
	public ChatPopupWindow(RoomActivity activity) {
		super(activity);
		this.initChatPopupWindow(activity);
	}

	@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

	}
	private void initChatPopupWindow(RoomActivity activit){

		try {
			LayoutInflater mLayoutInflater = (LayoutInflater) mActivity
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			int layoutRourse = R.layout.chat_pop;
			view = mLayoutInflater.inflate(layoutRourse, null);

			view.setFocusableInTouchMode(true);
			contentText = (EditText) view.findViewById(R.id.content);
			// 设置contentText内容为以前输入但没send的字符串
			contentText.setText(oldString);
			commons = ExpressionConstants.commonLanguages;
			commons_list = new ArrayList<Map<String, String>>();
			for (int i = 0; i < commons.length; i++) {
				Map<String, String> commons_map = new HashMap<String, String>();
				commons_map.put("content", commons[i]);
				commons_list.add(commons_map);
			}
			listView = (ListView) view.findViewById(R.id.useage);
			completeButton = (Button) view.findViewById(R.id.complete);
			((Button)view.findViewById(R.id.complete)).getPaint().setFakeBoldText(true);

			commonAdapter = new ChatMessageAdapter(DdzApplication.getApplication());
			listView.setAdapter(commonAdapter);

			this.setFocusable(true);
			this.setConsumeKeyEvent(true);

			ImageView popupview = (ImageView) view.findViewById(R.id.chatimage_id);
			width = popupview.getLayoutParams().width;
			height = popupview.getLayoutParams().height;
			this.createPopupWindow(view, width, height);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 为各组件设置监听器
	 */
	public void setListens(final RoomActivity activity) {
		contentText.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (KeyEvent.ACTION_DOWN == event.getAction()
						&& keyCode == KeyEvent.KEYCODE_BACK) {
					softKeyShow = !softKeyShow;
					if (!softKeyShow) {
						oldString = contentText.getText().toString();
						CloseWindow();
					}
				}
				return false;
			}
		});
		contentText.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				softKeyShow = true;
				return false;
			}
		});

		view.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				oldString = contentText.getText().toString();
				CloseWindow();
				return false;
			}
		});
		view.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				float x = event.getX();
				float y = event.getY();

				if (x < 0 || x > width || y < 0 || y > height) {
					oldString = contentText.getText().toString();
					CloseWindow();
				}
				return true;
			}
		});
		// 监听列表的点击事件
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				String content = (String) listView.getItemAtPosition(arg2);
				if (content == null || content.equals("")) {
					CloseWindow();
				} else {
					UserInfo self = UserMessageCenter.getInstance().getUserInfo();
					String userName = self.getNick();
					if (userName != null) {

						addMyChatToToast(content);
						activity.addToast(RoomInfo.getInstance().getSeatId(),content);
						activity.getRoom().getSocketSender().chatReq(content);
					}
					CloseWindow();
				}
			}
		});

		// 监听点击完成按钮
		completeButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String content = contentText.getText().toString();
				if (content == null || content.equals("")) {
					// 发送之后将以前的输入字符清除
					oldString = "";
					CloseWindow();
				} else {
					UserInfo self = UserMessageCenter.getInstance().getUserInfo();
					String userName = self.getNick();
					if (userName != null) {

						addMyChatToToast(content);
						activity.addToast(RoomInfo.getInstance().getSeatId(),content);
						activity.getRoom().getSocketSender().chatReq(content);
					}
					oldString = "";
					CloseWindow();
				}
				
				mActivity.playBtnPressMusic();
			}
		});

		contentText.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				String content = contentText.getText().toString();
				if (content == null || content.equals("")) {
					contentText.setText(oldString);
					if (oldString != null) {
						contentText.setSelection(oldString.length());
					}
				} else {
					contentText.setSelection(content.length());
				}
			}
		});

	}

	/**
	 * 添加玩家发的信息到显示框
	 */
	private void addMyChatToToast(String content) {

	}
	public class ChatMessageAdapter extends BaseAdapter {
		Context ctx;

		public ChatMessageAdapter(Context context) {
			this.ctx = context;
		}

		@Override
		public int getCount() {
			return commons.length;
		}

		@Override
		public Object getItem(int position) {
			return commons[position];
		}

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

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			try {
				TextView holder = null;
				if( convertView == null)
				{
					holder = new TextView(mActivity);
					convertView = mActivity.getLayoutInflater().inflate(R.layout.chat_list_item, null);

					holder = (TextView) convertView.findViewById(R.id.chat_text);
					convertView.setTag(holder);
				}
				else
				{
					holder = (TextView) convertView.getTag();
				}
				String text = commons[position];
				holder.setText(text);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return convertView;
		}
	}
}

