package com.lianxin.pubqq.activity.adpter;

import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lianxin.pubqq.R;
import com.lianxin.panqq.common.bean.InformMessage;
import com.lianxin.panqq.common.bean.InformMessage.InformMesageStatus;
import com.lianxin.panqq.list.adpter.ExListAdapter;
import com.lianxin.panqq.utils.ImagesUtils;
import com.lianxin.panqq.utils.TimeUtil;

public class InformListAdapter extends ExListAdapter{

	private List<InformMessage> mList;

	private OnMyButClickListener ButClickListener; 	 
	public InformListAdapter(Context context,List<InformMessage> userList) {
	    super(context);
		this.mList=userList;

	}

	@Override
	public void setData(Object object) {
		this.mList = (List<InformMessage>)object; 
	} 	

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

	@Override
	public InformMessage getItem(int position) {
		return mList.get(position);
	}

	 @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_informlist, null);
			holder.avator = (ImageView) convertView.findViewById(R.id.avatar);
			holder.message = (TextView) convertView.findViewById(R.id.message);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.status = (Button) convertView.findViewById(R.id.user_state);
			holder.groupContainer = (LinearLayout) convertView.findViewById(R.id.ll_group);
			holder.type = (TextView) convertView.findViewById(R.id.tv_oatype);
			holder.title = (TextView) convertView.findViewById(R.id.tv_oatitle);			
		    holder.time = (TextView) convertView.findViewById(R.id.time);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final InformMessage msg = getItem(position);
		if (msg != null) {			
		/*	if(msg.chatType >= 5){ // 显示群聊提示
				holder.groupContainer.setVisibility(View.VISIBLE);
				holder.groupname.setText(msg.getGroupName());
			} else{
				holder.groupContainer.setVisibility(View.GONE);
			}*/
			holder.title.setText(msg.getTitle());
			holder.message.setText(msg.getReason());
	//		holder.name.setText(user.getNick());
			holder.type.setText(msg.getType());	
			holder.name.setText(msg.getFrom());			
	        int image = msg.getHeadImg();
	        if(image>80) image = image%80;
	        holder.avator.setImageResource(ImagesUtils.images[image]); 			
	        holder.time.setText(TimeUtil.getMsgTime(msg.getTime()));	        
			if (msg.getStatus() == InformMesageStatus.BEAGREED) {
				holder.status.setVisibility(View.INVISIBLE);
				holder.message.setText("已同意你的好友请求");
			} else if (msg.getStatus() == InformMesageStatus.BEINVITEED || msg.getStatus() == InformMesageStatus.BEAPPLYED) {
				holder.status.setVisibility(View.VISIBLE);
				holder.status.setText("处理");
				if(msg.getStatus() == InformMesageStatus.BEINVITEED){
					if (msg.getReason() == null) {
						// 如果没写理由
						holder.message.setText("请求加你为好友");
					}
				}else{ //入群申请
					if (TextUtils.isEmpty(msg.getReason())) {
						holder.message.setText("申请加入群：" + msg.getGroupName());
					}
				}
			} else if (msg.getStatus() == InformMesageStatus.AGREED) {
				holder.status.setText("已同意");
				holder.status.setBackgroundDrawable(null);
				holder.status.setEnabled(false);
			} else if(msg.getStatus() == InformMesageStatus.REFUSED){
				holder.status.setText("已拒绝");
				holder.status.setBackgroundDrawable(null);
				holder.status.setEnabled(false);
			}
		    if(position>2){
			    holder.status.setText("已处理");
			    holder.status.setBackgroundDrawable(null);
			    //holder.status.setEnabled(false);
		    }			
			// 设置点击事件
			final int pos = position;			
			holder.status.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// 同意别人发的好友请求
                    //Toast.makeText(mContext, "您点击了-" , Toast.LENGTH_LONG).show();
	                if(ButClickListener != null){ 
                	     ButClickListener.onButClicked(pos);                          	
                    }                     
				}
			});
			// 设置用户头像 
	//		ImageLoader.getInstance().displayImage(user.getHeaderurl(), holder.avator, ImageOptions.get_Options());

		}
		return convertView;
	}

	private void imageBrower(int position, String[] urls) {
	/*	Intent intent = new Intent(mContext, ImagePagerActivity.class);
		// 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls);
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
		mContext.startActivity(intent);*/
	}

	private static class ViewHolder {
		ImageView avator;
		TextView name;
		TextView message;
		Button status;
		LinearLayout groupContainer;
		TextView type;
		TextView title;		
	    TextView time;
	}

    public static interface OnMyButClickListener{  
        public void onButClicked( int position );        
    }	

    public void setButListener(OnMyButClickListener onButClickListener) {  
        this.ButClickListener = onButClickListener;  
    }    
    

}
