package com.lianxin.pubqq.activity;
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 com.lianxin.pubqq.R;
import com.lianxin.pubqq.activity.adpter.MemberAdminListAdapter;
import com.lianxin.pubqq.activity.adpter.MemberAdminListAdapter.OnMyButClickListener;
import com.lianxin.panqq.edit.MarkUserDialog;
import com.lianxin.panqq.common.BaseActivity;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.common.Utils;
import com.lianxin.panqq.common.DefineUser;
import com.lianxin.panqq.list.bean.UserAdminNode;
import com.lianxin.panqq.list.utils.FriendUtils;
import com.lianxin.panqq.client.markClient;
import com.lianxin.panqq.client.callback.getCallBack;
import com.lianxin.panqq.client.callback.listCallBack;
import com.lianxin.panqq.list.ExMemberAdminList;

//成员高级管理列表
public class MemberAdminListActivity extends BaseActivity implements OnClickListener {
	private ImageView img_back, img_right;     
	private MemberAdminListAdapter mAdapter;
	private List<UserAdminNode> userList;//搜索用户列表
	private ExMemberAdminList mListView;
	
	private int groupId = 10058;	
	private int groupType = DefineUser.TYPE_CROWD;		

    @Override
   	public void onCreate(Bundle savedInstanceState) {
      	 
   		super.onCreate(savedInstanceState);
   		setContentView(R.layout.admin_memberlist);
		img_back = (ImageView) findViewById(R.id.img_back); 		
		img_back.setOnClickListener(this);    		
		mListView = (ExMemberAdminList)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 MemberAdminListAdapter(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) {
				UserAdminNode child = mAdapter.getItem(position-1);				
	
				Intent intent = new Intent(MemberAdminListActivity.this, UserSuperInfoActivity.class);				
				intent.putExtra("Info_UserId", child.getUserId());
				intent.putExtra("Info_UserName", child.getName());
				intent.putExtra("Info_Age", child.age);
				intent.putExtra("Info_Address", child.address);
				intent.putExtra("Info_strAddress", child.straddress);				
				intent.putExtra("Info_Image", child.imageid);
				intent.putExtra("Info_Sex", child.sex);					
				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(MemberAdminListActivity.this);
			//finish();
		} else if (id == R.id.img_right) {
		}
	}    
    
	public void onItemButtonClick(int position) {
		// TODO Auto-generated method stub
		UserAdminNode userInfo = mAdapter.getItem(position);
		final int nUserId = userInfo.userId;
		String strUserName = userInfo.getName();			

		new MarkUserDialog(MemberAdminListActivity.this,nUserId,strUserName,
				new MarkUserDialog.OnSetListener() {
					@Override
					public void onSelect(int option) {
						 MarkUser( nUserId, option );
						// txt_power.setText(""+admin);							
					}
					@Override
					public void onSelect(int count,int max) {
						String str = "投票成功。\n你今天已经投票"+count+"次，";
						int sub = max - count;
						if(count<max)
							str += "\n还可以投票"+sub +"次\n";
						else
							str += "\n每天的投票最大次数为20+金钻等级。\n超过的每次投票花费0.2个PQ币。\n";						
						Utils.showLongToast(MemberAdminListActivity.this, str);						
					}						
				}).show();		
    }    

	private void MarkUser( int nUserId, int option ){
		int nDestid = nUserId;
		if(nDestid == GloableParams.m_szUserId){//判断是否是自己
		//	return;
		}	
		if(FriendUtils.getFriendList(nDestid)>0){//判断是否已经是好友
		//	return;
		}
		if(TextUtils.isEmpty(GloableParams.m_szServerIp)){//判断是否已经登录服务器
			Utils.showLongToast(MemberAdminListActivity.this, "你还没有登录服务器!");
			return;
		}		
		getLoadingDialog("正在投票中...").show();		
		markClient.putMarkVote(DefineUser.TYPE_FRIEND,nUserId,option,new getCallBack(){	
			@Override
			public void onSuccess(int statusCode, byte[] recvBuf) {
				//getLoadingDialog("正在刷新数据...").dismiss();	
				final int count = recvBuf[4];	
				final int max = recvBuf[8];				
				runOnUiThread(new Runnable() {
					public void run() {
						getLoadingDialog("正在投票中...").dismiss();
						String str = "投票成功。\n你今天已经投票"+count+"次，";
						int sub = max - count;
						if(count<max)
							str += "\n还可以投票"+sub +"次\n";
						else
							str += "\n每天的投票最大次数为20+金钻等级。\n超过的每次投票花费0.2个PQ币。\n";						
						Utils.showLongToast(MemberAdminListActivity.this, str);
						
					}
				});				
			}
			@Override
			public void onFailure(int statusCode,  String message) {
				//getLoadingDialog("正在刷新数据...").dismiss();
				final String str = message;
				runOnUiThread(new Runnable() {
					public void run() {
						getLoadingDialog("正在正在投票中...").dismiss();							
						Utils.showLongToast(MemberAdminListActivity.this, str);
					}
				});	
			}
		});	 
	}	
	
}
 