package com.lianxin.pubqq.setup;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.content.Context;
//import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
//import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lianxin.pubqq.R;

import com.lianxin.panqq.setup.bean.ReplyMessage;
import com.lianxin.panqq.utils.ImagesUtils;
import com.lianxin.panqq.utils.TimeUtil;



public class ReplyListAdapter extends BaseAdapter{

	private List<ReplyMessage> allList;
	private List<ReplyMessage> filterList;
	private List<ReplyMessage> myList;	
	private LayoutInflater mInflater;
	private Context mContext;	

	public ReplyListAdapter(Context context,List<ReplyMessage> replyList) {
		mInflater = LayoutInflater.from(context);
		mContext=context;
		this.allList=replyList;
		filterList = new ArrayList<ReplyMessage>();
		myList = new ArrayList<ReplyMessage>();
        myList.addAll(replyList); 		
	}

	@Override
	public int getCount() {
		return myList==null?0:myList.size();
	}

	@Override
	public ReplyMessage getItem(int position) {
		return myList.get(position);
	}

 
	public String getUser(int position) {
		return myList.get(position).getName();
	}
	
	 @Override
	public long getItemId(int position) {

		return position;		
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
	    final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_replylist, null);
			holder.avator=(ImageView)convertView.findViewById(R.id.iv_avator);
			holder.name=(TextView)convertView.findViewById(R.id.tv_name);
			holder.time=(TextView)convertView.findViewById(R.id.tv_time);			
			holder.content = (TextView) convertView.findViewById(R.id.tv_content);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final ReplyMessage bean = getItem(position);
		
		int imageid = bean.imageid%80;
		holder.avator.setImageResource(ImagesUtils.images[imageid]);	
		String strname = bean.getName()+"("+bean.getUserid()+")";
	//	String strContent = ""+(position+1)+","+PowerUtils.getPowerText(DefineUser.TYPE_CROWD,1)+","+2;	
		String strContent = bean.getReply();
		/*if(position<=5){
			if(position==0) strContent = "到岗:ok";
			if(position==1) strContent = "有事:下午上课";
			if(position==2) strContent = "到岗:好的";
			if(position==3) strContent = "到岗:ok";
			if(position==4) strContent = "到岗:准时到";
			if(position==5) strContent = "有事:下午上课";			
		}*/
		/*if(position<=5){
		    if(position==0) strContent = "支持";
		    if(position==1) strContent = "反对";
		    if(position==2) strContent = "支持";
		    if(position==3) strContent = "弃权";
		    if(position==4) strContent = "支持";
		    if(position==5) strContent = "反对";			
	    }	*/
		holder.name.setText(strname);
		holder.content.setText(strContent);
		holder.time.setText(TimeUtil.getMsgTime(bean.getTime()));
		//判断当前故事是否是自己的
		final int pposition = position;

		return convertView;
	}

	private static class ViewHolder {

		public TextView name;
		public ImageView avator;
		TextView content;
		TextView time;
//		TextView zan;
	//	NoScrollGridView gridView;
//		int position;
	}

	// 定义自己的过滤器
	private MyFilter myfilter;

	public MyFilter getFilter() {
		if (myfilter == null) {
			myfilter = new MyFilter();
		}
		return myfilter;
	}

	class MyFilter extends Filter {

		@Override
		protected FilterResults performFiltering(CharSequence charSequence) {
			// TODO Auto-generated method stub
			FilterResults results = new FilterResults();
			if (TextUtils.isEmpty(charSequence)) {
				filterList.clear();
				results.values = allList;
				return results;
			} else {
				filterList.clear();
				for (Iterator<ReplyMessage> iterator = allList.iterator(); iterator
						.hasNext();) {
					ReplyMessage name = iterator.next();
					if (name.getName().contains(charSequence)) {
						filterList.add(name);
					}
				}
				results.values = filterList;
				return results;
			}
		}

		// 筛选结果
		@SuppressWarnings("unchecked")
		@Override
		protected void publishResults(CharSequence arg0, FilterResults arg1) {
			// TODO Auto-generated method stub
			myList = (List<ReplyMessage>) arg1.values;
			if (arg1.count > 0) {
				notifyDataSetChanged();
			} else {
				notifyDataSetInvalidated();
			}
		}

	}

/*	public Filter getFilter() {
		// TODO Auto-generated method stub
		return null;
	}*/

}

