 
package com.lianxin.pubqq.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lianxin.pubqq.R;
import com.lianxin.panqq.edit.AddFriendDialog;
import com.lianxin.panqq.edit.MyAlertDialog;
import com.lianxin.panqq.client.callback.askCallBack;
import com.lianxin.panqq.client.AskClient;
import com.lianxin.panqq.common.BaseActivity;
import com.lianxin.panqq.common.DefineChat;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.common.Utils;
import com.lianxin.panqq.list.bean.UserNode;
import com.lianxin.panqq.list.utils.AddressUtils;
import com.lianxin.panqq.list.utils.FriendUtils;
import com.lianxin.pubqq.chat.EMChatActivity;
import com.lianxin.panqq.utils.ImagesUtils;
import com.lianxin.panqq.main.EMContactManager;
//import com.lianxin.panqq.chat.entity.EMMessage;
import com.lianxin.panqq.common.bean.AskInfo;
//用户信息界面
public class UserInfoActivity extends BaseActivity implements OnClickListener {
	//private UserNode userInfo;//搜索用户列表	
	
	private TextView txt_title,txt_right;
	private ImageView img_back, img_right; 
	private ImageView img_photo;  //用户头像
	private TextView txt_userid; //用户id
	private TextView txt_username;  //用户名	
//	private TextView txt_nickname; //用户昵称
	private TextView txt_sex;  //用户性别
	private TextView txt_age;  //用户年龄
	private TextView txt_address;   //用户地区

	private Button btn_satrtchat;
	private Button btn_addfriend;
	private Button btn_toblack;
	private Button btn_jubao;
	private Button btn_setsight;
	private Button btn_setguard;
	private Button btn_detail;
	private Button btn_admin;	

	private int nUserId = 10100;//用户唯一id
	private String strUserName = "情已停机";
	private int image = 10;
	private long userHead = 0;	
	private String strNickName = "赵八方";
	private int iSex = 1;
	private String Sex = "男";	
	private int iAge = 20;
	private int iAddress = 10;  //本地读取的是地址代码
	private String strAddress = "";//远程传送的是具体的地址字符串
	
	public static final int REQUEST_CODE_ADD_TO_CONTACT = 226;
	public static final int REQUEST_CODE_ADD_TO_BLACKLIST = 225;
//	private ProgressDialog progressDialog;
	
	private LinearLayout lay_right;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_userinfo);
		
		initControl();
		initView();
		initData();
		setListener();			
	}

	protected void initControl() {
		txt_title = (TextView) findViewById(R.id.txt_title);
		//txt_title.setText("用户查找");
		img_back = (ImageView) findViewById(R.id.img_back);
		img_back.setVisibility(View.VISIBLE);	
		txt_userid = (TextView)findViewById(R.id.tv_user_userid);
		txt_username = (TextView)findViewById(R.id.tv_user_username);		
		img_photo = (ImageView)findViewById(R.id.iv_user_image);
//		txt_nickname = (TextView)findViewById(R.id.tv_user_nickname);
		txt_sex = (TextView)findViewById(R.id.tv_user_sex);
		txt_age = (TextView)findViewById(R.id.tv_user_age);
		txt_address = (TextView)findViewById(R.id.tv_user_address);

		btn_satrtchat = (Button)findViewById(R.id.btn_startchat);
		btn_addfriend = (Button)findViewById(R.id.btn_addfriend);
		btn_toblack = (Button)findViewById(R.id.btn_toblack);
		btn_jubao = (Button)findViewById(R.id.btn_jubao);
		btn_detail = (Button)findViewById(R.id.btn_detail);
		btn_admin = (Button)findViewById(R.id.btn_admin);
		
		lay_right = (LinearLayout)findViewById(R.id.layout_right);
		txt_right = (TextView)findViewById(R.id.tv_right);
	}

	protected void initView() {
		nUserId = getIntent().getIntExtra("Info_UserId", 10010);
		//判断是否是自己			
		if(nUserId == GloableParams.m_szUserId){
			txt_right.setText("我的故事");
			btn_satrtchat.setVisibility(View.GONE);
			btn_addfriend.setVisibility(View.GONE);
			btn_toblack.setVisibility(View.GONE);
			btn_jubao.setVisibility(View.GONE);
		}
		//判断是否是好友，如果是就把加好友隐藏		
		if(FriendUtils.getFriendList(nUserId)>0){
		//	btn_addfriend.setVisibility(View.GONE);
		}
	
	}
	
	protected void initData() {

		nUserId = getIntent().getIntExtra("Info_UserId", 10010);		
		strUserName = getIntent().getStringExtra("Info_UserName");
		iAge = getIntent().getIntExtra("Info_Age", 20);
		iAddress = getIntent().getIntExtra("Info_Address", 10);
		strAddress = getIntent().getStringExtra("Info_strAddress");		
		image = getIntent().getIntExtra("Info_Image", 23);
		Sex = getIntent().getStringExtra("Info_Sex");		
		txt_userid.setText(""+nUserId); 
		txt_username.setText(strUserName);	
//		txt_nickname.setText(strNickName);		
        if(image<0) image = -image;		
        if(image>80) image = image%80;
        img_photo.setImageResource(ImagesUtils.images[image]);	
        if(iSex==2)	txt_sex.setText("女");
        else	txt_sex.setText("男");
        txt_sex.setText(Sex);       
        txt_age.setText(""+iAge);
        if(iAddress>0)
		    txt_address.setText(AddressUtils.getAddress(iAddress));        
        else
		    txt_address.setText(strAddress); 
	}	

	protected void setListener() {
		img_back.setOnClickListener(this);		
		lay_right.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
			}
		});
		btn_satrtchat.setOnClickListener(this);
		btn_addfriend.setOnClickListener(this); 
		btn_toblack.setOnClickListener(this);
		btn_jubao.setOnClickListener(this);	
		btn_detail.setOnClickListener(this);
		btn_admin.setOnClickListener(this);			
	}	
	

	
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		int id = arg0.getId();
		if (id == R.id.img_back) {
			Utils.finish(UserInfoActivity.this);
			//finish();
			//finish(UserInfoActivity.this);
		} else if (id == R.id.btn_startchat) {
			Intent intent = new Intent(this, EMChatActivity.class);
			intent.putExtra("Chat_ID", nUserId);
			intent.putExtra("Chat_Type", DefineChat.ChatType_USER);
			intent.putExtra("Chat_Name", strNickName);
			startActivity(intent);
		} else if (id == R.id.btn_addfriend) {
			AskInfo uinfo = new AskInfo(nUserId, strUserName, image);
			new AddFriendDialog(UserInfoActivity.this, uinfo,
					new AddFriendDialog.OnSetListener() {
						@Override
						public void onSelect(AskInfo askInfo) {
							addFriend(askInfo);
						}
					}).show();
		} else if (id == R.id.btn_toblack) {
			new MyAlertDialog(UserInfoActivity.this, "移入到黑名单？",
					"你确定要将该成员移入到黑名单吗？",
					new MyAlertDialog.OnSetListener() {
						@Override
						public void onSelect(int comm) {
							//	UserInfo uinfo = new UserInfo(nUserId,nTypeId,strNickName,image,iPower,iIndex);
							//	deleteMember(uinfo);
						}
					}).show();
		} else if (id == R.id.btn_jubao) {
			AskInfo uinfo1 = new AskInfo(nUserId, strNickName, image);
			new AddFriendDialog(UserInfoActivity.this, uinfo1,
					new AddFriendDialog.OnSetListener() {
						@Override
						public void onSelect(AskInfo askInfo) {
							addFriend(askInfo);
						}
					}).show();
		} else if (id == R.id.btn_detail) {
			Intent intent3 = new Intent(UserInfoActivity.this, UserDetailInfoActivity.class);
			intent3.putExtra("Info_UserId", nUserId);
			intent3.putExtra("Info_UserName", strUserName);
			intent3.putExtra("Info_Age", iAge);
			intent3.putExtra("Info_Address", iAddress);
			intent3.putExtra("Info_Image", image);
			intent3.putExtra("Info_Sex", Sex);
			startActivity(intent3);
		} else if (id == R.id.btn_admin) {
			Intent intent4 = new Intent(UserInfoActivity.this, UserSuperInfoActivity.class);
			intent4.putExtra("Info_UserId", nUserId);
			intent4.putExtra("Info_UserName", strUserName);
			intent4.putExtra("Info_Age", iAge);
			intent4.putExtra("Info_Address", iAddress);
			intent4.putExtra("Info_Image", image);
			intent4.putExtra("Info_Sex", Sex);
			startActivity(intent4);
		}
	} 
	/**
	 * onActivityResult
	 */
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) { // 清空消息
			if (requestCode == REQUEST_CODE_ADD_TO_CONTACT) { // 加好友
				//EMMessage deleteMsg = (EMMessage) adapter.getItem(data.getIntExtra("position", -1)); 
				String sendstr = data.getStringExtra("edittext");

			}else if(requestCode == REQUEST_CODE_ADD_TO_BLACKLIST){

			}
		}
	}
	
	/**
	 *  添加contact
	 * @param view
	 */

	public void addFriend(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(UserInfoActivity.this, "你还没有登录服务器!");
			return;
		}		
		getLoadingDialog("正在发送请求...").show();		
		AskClient.AskAddFriend(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(UserInfoActivity.this, str);
					}
				});				
			}
			@Override
			public void onFailure(int statusCode,  String message) {
				getLoadingDialog("正在发送请求...").dismiss();
				final String str = message;
				runOnUiThread(new Runnable() {
					public void run() {
						Utils.showLongToast(UserInfoActivity.this, str);
					}
				});	
			}
		});	 
	}			

	/**
	 * 加入到黑名单
	 * 
	 * @param username
	 */
	private void addUserToBlacklist(String username) {
		try {
			EMContactManager.getInstance().addUserToBlackList(username, true);
			Toast.makeText(getApplicationContext(), "移入黑名单成功", 0).show();
		//} catch (EaseMobException e) {
		} catch (Exception e) {			
			e.printStackTrace();
			Toast.makeText(getApplicationContext(), "移入黑名单失败", 0).show();
		}

	}

	/**
	 * 返回
	 * 
	 * @param view
	 */
	public void back(View view) {
		//finish();
		Utils.finish(UserInfoActivity.this);		
	}
	

	
}
