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

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

import com.lianxin.pubqq.R;
import com.lianxin.pubqq.activity.GroupSearchActivity;
import com.lianxin.pubqq.ulive.adpter.LiveSearchAdapter;
//import com.lianxin.panqq.download.LiveSearchAdapter;
import com.lianxin.panqq.common.BaseActivity;
import com.lianxin.panqq.common.DefineUser;
import com.lianxin.panqq.common.Utils;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.list.utils.FriendUtils;
import com.lianxin.panqq.client.AskClient;
import com.lianxin.panqq.client.callback.askCallBack;
import com.lianxin.panqq.client.callback.listCallBack;
import com.lianxin.panqq.common.bean.AskInfo;
import com.lianxin.panqq.list.ExLivesList;
import com.lianxin.panqq.main.bean.LivePerson;

/**
 * 搜索分类列表
 *
 */
public class LivesListActivity extends BaseActivity implements OnClickListener {
	private ImageView img_back, img_right;    
/*	private GroupListAdapter mAdapter;
	private List<GroupNode> groupList;//搜索用户列表
	private ExGroupList mListView;	*/
	private LiveSearchAdapter mAdapter;
	private List<LivePerson> livesList;//搜索用户列表
	private ExLivesList mListView;	
		
	private int groupType = DefineUser.TYPE_CROWD;		

    @Override
   	public void onCreate(Bundle savedInstanceState) {
      	 
   		super.onCreate(savedInstanceState);
   		setContentView(R.layout.activity_livelist);
		img_back = (ImageView) findViewById(R.id.img_back);
		img_right = (ImageView) findViewById(R.id.img_right);   		
		img_back.setOnClickListener(this);
		img_right.setOnClickListener(this);  		
		mListView = (ExLivesList)findViewById(R.id.live_xListView);
		mListView.setPullLoadEnable(true);
		groupType = getIntent().getIntExtra("Info_GroupType", 3);
		mListView.setType(groupType);	
		//initData();
		 
		mAdapter =  new LiveSearchAdapter(this, livesList);
		mListView.setAdapter(mAdapter);
		mListView.setDataAdapter(livesList,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.setXListViewListener(this);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v, int position,
					long id) {
	            Toast.makeText(context, "I am ", Toast.LENGTH_SHORT).show(); 				
				Intent intent = new Intent(LivesListActivity.this, LiveWatchActivity.class);
				LivePerson groupInfo = mAdapter.getItem(position-1);					
				
				startActivity(intent);			}
			
		});

		mListView.resetData();	//刷新远程数据	
    }
	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.img_back) {
			Utils.finish(LivesListActivity.this);
			//finish();
			//finish();
			//overridePendingTransition(R.anim.push_right_in,
			//		R.anim.push_right_out);
		} else if (id == R.id.img_right) {
			Intent intent = new Intent();
			//intent.setClass(this, UserSearchActivity.class);
			//intent.putExtra("REQUEST_FROM", 1);
			//startActivityForResult(intent, 20);
			intent.setClass(this, GroupSearchActivity.class);
			intent.putExtra("REQUEST_FROM", 1);
			intent.putExtra("SEARCH_TYPE", groupType);
			startActivityForResult(intent, 20);
		}
	} 
	
/*	public void onItemButtonClick(int position) {
		// TODO Auto-generated method stub
		GroupNode groupInfo = mAdapter.getItem(position);
		int nGroupId = groupInfo.getClassId();
		String strGroupName = groupInfo.getName();			
		int image = groupInfo.imageid;		
		AskInfo uinfo = new AskInfo(nGroupId,strGroupName,image);			
		new AskJoinDialog(LivesListActivity.this,uinfo,
				new AskJoinDialog.OnSetListener() {
					@Override
					public void onSelect(AskInfo askInfo) {
						joinGroup(askInfo);
					}
				}).show();
    }	*/
	

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == RESULT_OK) { // 清空消息
			//Intent intent = new Intent();			
			switch (requestCode) {
			case 20:
				mListView.onRefresh();
				break;			
			default:
				break;
			}			
		}
	}	
 
	public void joinGroup(AskInfo uinfo){
		int nDestid = uinfo.destid;
		if(nDestid == GloableParams.m_szUserId){//判断是否是自己
		//	startActivity(new Intent(this, AlertDialog.class).putExtra("msg", "不能添加自己"));
		//	return;
		}	
		if(FriendUtils.getFriendList(nDestid)>0){//判断是否已经是好友
		//	startActivity(new Intent(this, AlertDialog.class).putExtra("msg", "此用户已是你的好友"));
		//	return;
		}
		if(TextUtils.isEmpty(GloableParams.m_szServerIp)){//判断是否已经登录服务器
			Utils.showLongToast(LivesListActivity.this, "你还没有登录服务器!");
			return;
		}		
		getLoadingDialog("正在发送请求...").show();		
		AskClient.AskJoinGroup(groupType,uinfo,new askCallBack(){	
			@Override
			public void onSuccess(int statusCode, String message) {
				getLoadingDialog("正在发送请求...").dismiss();	
				final String str = message;
				runOnUiThread(new Runnable() {
					public void run() {
						Utils.showLongToast(LivesListActivity.this, str);
					}
				});				
			}
			@Override
			public void onFailure(int statusCode,  String message) {
				getLoadingDialog("正在发送请求...").dismiss();
				final String str = message;
				runOnUiThread(new Runnable() {
					public void run() {
						Utils.showLongToast(LivesListActivity.this, str);
					}
				});	
			}
		});	 
	}		
	
}
 