package com.lianxin.pubqq.activity;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.lianxin.pubqq.R;
import com.lianxin.pubqq.activity.adpter.MemberListAdapter;
import com.lianxin.pubqq.activity.adpter.MemberListAdapter.OnMyButClickListener;
import com.lianxin.panqq.edit.AddFriendDialog;
import com.lianxin.panqq.common.BaseActivity;
import com.lianxin.panqq.common.DefineChat;
import com.lianxin.panqq.common.Utils;
import com.lianxin.pubqq.chat.EMChatActivity;
import com.lianxin.panqq.common.DefineUser;
//import com.lianxin.panqq.chat.entity.EMMessage;
//import com.lianxin.panqq.chat.utils.MyLogger;
import com.lianxin.panqq.list.bean.Member;
import com.lianxin.panqq.client.callback.listCallBack;
import com.lianxin.panqq.common.bean.AskInfo;
import com.lianxin.panqq.list.ExMemberList;

//成员列表界面
public class MemberListActivity extends BaseActivity implements OnClickListener {
	private ImageView img_back, img_right;     
	private MemberListAdapter mAdapter;
	private List<Member> userList;//搜索用户列表
	private ExMemberList mListView;
	
	private int groupId = 10058;	
	private int groupType = DefineUser.TYPE_CROWD;		

    @Override
   	public void onCreate(Bundle savedInstanceState) {
      	 
   		super.onCreate(savedInstanceState);
   		setContentView(R.layout.activity_memberlist);
		img_back = (ImageView) findViewById(R.id.img_back); 		
		img_back.setOnClickListener(this);    		
		mListView = (ExMemberList)findViewById(R.id.user_xListView);
		mListView.setPullLoadEnable(true);

		groupType = getIntent().getIntExtra("TypeType", DefineUser.TYPE_CROWD);
		groupId = getIntent().getIntExtra("TypeID", 10001);	
		mListView.setTypeAndId(groupType,groupId);			
	//	initData();		 
		mAdapter =  new MemberListAdapter(this, userList);
		mListView.setAdapter(mAdapter);
		mListView.setDataAdapter(userList,mAdapter);		
		mListView.setStatusListener(new listCallBack(){
			@Override
			public void onSuccess(int statusCode, byte[] recvBuf) {
		
			}
			@Override
			public void onFailure(int errorCode, String errorMessage) {
				// TODO Auto-generated method stub
				
			}
		});		
		
		mListView.initData(); //初始化数据(视图初始化所需要的)		
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position,
					long id) {
				Member child = mAdapter.getItem(position-1);				
				Intent intent = new Intent(MemberListActivity.this, EMChatActivity.class);
				intent.putExtra("Chat_ID", child.getUserId());
				intent.putExtra("Chat_Type", DefineChat.ChatType_USER);		
				intent.putExtra("Chat_Name", child.getName());
				startActivity(intent);			}
			
		});

		mAdapter.setButListener(new OnMyButClickListener(){  
			@Override
			public void onButClicked(int position) {
				// TODO Auto-generated method stub
				onItemButtonClick(position);						
			}  
        });	
		mListView.resetData();	//刷新远程数据		
    }

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.img_back) {
			Utils.finish(MemberListActivity.this);
			//finish();
			//overridePendingTransition(R.anim.push_right_in,
			//		R.anim.push_right_out);
		} else if (id == R.id.img_right) {
		}
	}    
    
	public void onItemButtonClick(int position) {
		// TODO Auto-generated method stub
		Member userInfo = mAdapter.getItem(position);
		int nUserId = userInfo.userId;
		String strUserName = userInfo.getName();			
		int image = userInfo.imageid;		
		AskInfo uinfo = new AskInfo(nUserId,strUserName,image);			
		new AddFriendDialog(MemberListActivity.this,uinfo,
				new AddFriendDialog.OnSetListener() {
					@Override
					public void onSelect(AskInfo askInfo) {
				//		addFriend(askInfo);
					}
				}).show();
    }    
 
}
 