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.GloableParams;
import com.lianxin.panqq.list.bean.Address;

import com.lianxin.panqq.common.bean.InviteMessage;
import com.lianxin.panqq.common.bean.InviteMessage.InviteMesageStatus;
import com.lianxin.panqq.list.adpter.ExListAdapter;
import com.lianxin.panqq.utils.ImagesUtils;

public class InviteListAdapter extends ExListAdapter{

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

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

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

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

	 @Override
	public long getItemId(int position) {
	
		return position;		
	}
	 
		public void refresh() {
			notifyDataSetChanged();
		}
		
	@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_invitelist, null);
			holder.avator = (ImageView) convertView.findViewById(R.id.avatar);
			holder.reason = (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.groupname = (TextView) convertView.findViewById(R.id.tv_groupName);
		    holder.time = (TextView) convertView.findViewById(R.id.time);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
	//	if(position<80){
	//	final InviteMessage bean = getItem(position);
		
		final InviteMessage msg = getItem(position);
		if (msg != null) {
			//if(msg.getGroupId() != null){ // 显示群聊提示			
			if(msg.chattype >= 5){ // 显示群聊提示
				holder.groupContainer.setVisibility(View.VISIBLE);
				holder.groupname.setText(msg.getGroupName());
			} else{
				holder.groupContainer.setVisibility(View.GONE);
			}
			
			holder.reason.setText(msg.getReason());
	//		holder.name.setText(user.getNick());
			holder.name.setText(msg.getFrom());	
	        int image = msg.getHeadImg();
	        if(image>80) image = image%80;
	        holder.avator.setImageResource(ImagesUtils.images[image]); 			
			// holder.time.setText(DateUtils.getTimestampString(new
			// Date(msg.getTime())));
		//	if(msg.getTime()>0)
		//		holder.time.setText("TimeUtil");				
			//	holder.time.setText(TimeUtil.getMsgTime(msg.getTime()));
		//	else
		//		holder.time.setText(msg.getstrTime());			
			if (msg.getStatus() == InviteMesageStatus.BEAGREED) {
				holder.status.setVisibility(View.INVISIBLE);
				holder.reason.setText("已同意你的请求");
			} else if (msg.getStatus() == InviteMesageStatus.BEINVITEED || msg.getStatus() == InviteMesageStatus.BEAPPLYED) {
				holder.status.setVisibility(View.VISIBLE);
				holder.status.setText("同意");
				if(msg.getStatus() == InviteMesageStatus.BEINVITEED){
					if (msg.getReason() == null) {
						// 如果没写理由
						holder.reason.setText("请求加你为好友");
					}
				}else{ //入群申请
					if (TextUtils.isEmpty(msg.getReason())) {
						holder.reason.setText("申请加入群：" + msg.getGroupName());
					}
				}
				// 设置点击事件
				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);                          	
	                    }                     
					}
				});		
			} else if (msg.getStatus() == InviteMesageStatus.AGREED) {
				holder.status.setText("已同意");
				holder.status.setBackgroundDrawable(null);
				holder.status.setEnabled(false);
			} else if(msg.getStatus() == InviteMesageStatus.REFUSED){
				holder.status.setText("已拒绝");
				holder.status.setBackgroundDrawable(null);
				holder.status.setEnabled(false);
			} else if(msg.getStatus() == InviteMesageStatus.BEREFUSED){
				holder.status.setText("已拒绝你的请求");
				holder.status.setBackgroundDrawable(null);
				holder.status.setEnabled(false);				
			} else {
				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 reason;
		Button status;
		LinearLayout groupContainer;
		TextView groupname;
	    TextView time;
	}

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

    public void setButListener(OnMyButClickListener onButClickListener) {  
        this.ButClickListener = onButClickListener;  
    } 	
	
	private String getAddress(int address){
	    List<Address> AddressList = GloableParams.AddressList;		
		String strprovince="";
		String strcity="";	
		int type = -1;
		for (int i = 0; i < AddressList.size(); i++) {
			if(AddressList.get(i).getId()==address)
			{
				strcity = AddressList.get(i).getName();
				type = AddressList.get(i).gettype();
			}
		}
		if(type<0) return "";
		if(type==0) return strcity;	
		for (int i = 0; i < AddressList.size(); i++) {
			if(AddressList.get(i).getId()==type)
			{
				strprovince = AddressList.get(i).getName();
				type = AddressList.get(i).gettype();
			}
		}	
		return strprovince+strcity;			
	}

}
