package com.lianxin.pubqq.main.adpter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lianxin.pubqq.R;
import com.lianxin.panqq.common.DefineUser;
import com.lianxin.panqq.common.utils.UserheadUtils;
import com.lianxin.panqq.main.bean.NearPeople;
import com.lianxin.panqq.utils.ImagesUtils;

public class NearbyAdapter extends BaseAdapter {
	private Context mContext;
	private List<NearPeople> UserInfos;// 好友信息
    private float curTouchPosx; 
    private OnMyClickListener childListener; 
    
	public NearbyAdapter(Context mContext, List<NearPeople> UserInfos) {
		this.mContext = mContext;
		this.UserInfos = UserInfos;
	}

	@Override
	public int getCount() {
		return UserInfos.size();	
	}

	@Override
	public Object getItem(int position) {
		return UserInfos.get(position);
	}

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

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
	//	User user = UserInfos.get(position);
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.layout_item_nearby, null);
			    //    R.layout.contact_item, null); 
		}
		ImageView img=(ImageView)convertView.findViewById(R.id.contactIcon);   		
		TextView name=(TextView)convertView.findViewById(R.id.name);
		TextView idtext=(TextView)convertView.findViewById(R.id.userid);		
		TextView description=(TextView)convertView.findViewById(R.id.description);
      //  img.setImageResource(R.drawable.head);
        int image = UserInfos.get(position).getImagePos();
        if(image>80) image = image%80;
        img.setImageResource(ImagesUtils.images[image]); 		
	    int userid = UserInfos.get(position).getSendId(); 
        	UserheadUtils.showUserHead(userid,img); 
        name.setText(UserInfos.get(position).getName());        
  
  	    int recvid = UserInfos.get(position).getRecvId();  
 	    long sendip = UserInfos.get(position).getSendIp(); 	    
  	    idtext.setText("["+userid+"]"+"["+recvid+"]"+"["+sendip+"]");	    
  	    String strip = UserInfos.get(position).getIp();
  	    String sex = UserInfos.get(position).getSexText(); 
  	    String signal = UserInfos.get(position).getSignal(); 	    
  	    description.setText("["+sex+"]"+strip+"["+signal+"]");  	
  	    convertView.setOnTouchListener(new OnTouchListener() {
 			@Override
 			public boolean onTouch(View v, MotionEvent event) {
 			//	Toast.makeText();
 				//获取点击事件相对控件左边的x轴坐标，即点击事件距离控件左边的距离
 				curTouchPosx = event.getX();//获得触摸x坐标
 			//	curTouchPosy = event.getY();//获得触摸y坐标				
 				return false;
 			}
 		});
        final View childView = convertView;
        final int position1 = position;         
  	    convertView.setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                if(childListener != null){  
                	if(curTouchPosx<300)                        	
                        childListener.onMyClicked1(childView, position1); 
                	else                        	
                        childListener.onMyClicked(childView, position1);                         	
                }  
            }                     
        });   	    
		return convertView;
	}

    public static interface OnMyClickListener{  
        public void onMyClicked(View view, int position);  
        public void onMyClicked1(View view, int position);        
    }	
    
    public void setMyClickListener(OnMyClickListener childListener) {  
        this.childListener = childListener;  
    }    
}
