 
package com.lianxin.pubqq.nearby;

import java.util.List;

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 com.lianxin.panqq.list.utils.NearbyUtil;
import com.lianxin.panqq.main.bean.NearPeople;
import com.lianxin.pubqq.R;
import com.lianxin.panqq.edit.MyAlertDialog;
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.common.utils.NearbyUtils;
import com.lianxin.pubqq.chat.EMChatActivity;
import com.lianxin.panqq.utils.ImagesUtils;
//import com.lianxin.panqq.chat.entity.EMMessage;
//import com.lianxin.panqq.thread.NearPeopleThread;
//import com.lianxin.panqq.file.NearbyFileUtil;
/**
 * 用户信息界面
 */
public class NearbyInfoActivity extends BaseActivity implements OnClickListener {
    private List<NearPeople> nearbys = GloableParams.NearPeoples;
    private NearPeople thisUser = null;
    
	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_sex;  //用户性别
	private TextView txt_destip;  //用户年龄
	private TextView txt_qianming;   //用户地区
	private TextView txt_oursid;  //用户性别
	private TextView txt_nickname;  //用户年龄
	private TextView txt_newid;   //用户地区

	private Button btn_satrtchat;
	private Button btn_savefriend;
	private Button btn_editfriend;
	private Button btn_delefriend;

	private int nUserId = 3010;//用户唯一id
	private int nPosition = 10;//用户唯一id	
	private String strUserName = "情已停机";
	private int image = 10;
	private String strNickName = "赵八方";
	private int iSex = 1;
	private String Sex = "男";
	private String strIp = "";	

	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_nearbyinfo);
		
		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_destip = (TextView)findViewById(R.id.tv_user_destip);
		txt_qianming = (TextView)findViewById(R.id.tv_user_qianming);
		txt_oursid = (TextView)findViewById(R.id.tv_user_oursid);
		txt_nickname = (TextView)findViewById(R.id.tv_user_nickname);
		txt_newid = (TextView)findViewById(R.id.tv_user_newid);
		
		btn_satrtchat = (Button)findViewById(R.id.btn_startchat);
		btn_savefriend = (Button)findViewById(R.id.btn_savefriend);
		btn_editfriend = (Button)findViewById(R.id.btn_editfriend);
		btn_delefriend = (Button)findViewById(R.id.btn_delefriend);		
	//	btn_toblack = (Button)findViewById(R.id.btn_toblack);
	//	btn_jubao = (Button)findViewById(R.id.btn_jubao);
		
		lay_right = (LinearLayout)findViewById(R.id.layout_right);
		txt_right = (TextView)findViewById(R.id.tv_right);
	}

	protected void initView() {
		nUserId = getIntent().getIntExtra("Info_UserId", 3001);
		nPosition = getIntent().getIntExtra("Info_Position", 10);	
		strUserName = getIntent().getStringExtra("Info_NickName");	
		strNickName	= getIntent().getStringExtra("Info_NickName");
		image = getIntent().getIntExtra("Info_Image", 23);		
		//判断是否是自己
		NearPeople user = nearbys.get(nPosition);	
		if(user!=null&&nUserId == user.getRecvId()){
			thisUser = user;
		}else{
			for (NearPeople user1 : GloableParams.NearPeoples) {
				int userid = user1.getSendId();
				if (userid == nUserId) { 
					thisUser = user1;					
					break;
				}
			}			
		}
	//	if(userId.equals(MyApp.getInstance().getUser())){				
		if(nUserId > 5000){
			txt_right.setText("我的故事");
			txt_right.setVisibility(View.GONE);			
			btn_satrtchat.setVisibility(View.GONE);
			btn_savefriend.setVisibility(View.GONE);
			btn_editfriend.setVisibility(View.GONE);
			btn_delefriend.setVisibility(View.GONE);
		}else if(nUserId > 3000){
			txt_right.setText("我的故事");
			txt_right.setVisibility(View.GONE);	
		//	btn_savefriend.setVisibility(View.GONE);
			btn_editfriend.setVisibility(View.GONE);
			btn_delefriend.setVisibility(View.GONE);
		}else{
			txt_right.setText("我的故事");
			txt_right.setVisibility(View.GONE);	
		//	btn_satrtchat.setVisibility(View.GONE);
			btn_savefriend.setVisibility(View.GONE);
		//	btn_editfriend.setVisibility(View.GONE);
		//	btn_delefriend.setVisibility(View.GONE);
		}

	}
	
	protected void initData() {

		nUserId = getIntent().getIntExtra("Info_UserId", 10010);		
	    if(thisUser!=null){
		strUserName = thisUser.getUserName();	
		image = thisUser.getImagePos();
		//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]);			
		iSex = thisUser.getSex();
		Sex = NearbyUtils.getIdentityText(iSex);
		txt_sex.setText(Sex);		
		strIp = thisUser.getIp();     
        txt_destip.setText(strIp);
		txt_oursid.setText(""+thisUser.recvid);         
		txt_qianming.setText(thisUser.getSignal()); 
		strNickName = thisUser.getNickName();
		//txt_nickname.setText(strNickName);	
	    }
		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]);			
	}	

	protected void setListener() {
		img_back.setOnClickListener(this);		
		lay_right.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
		//0314		startActivity(new Intent(Userinfo.this , User_gushi_Activity.class).putExtra("userId", userId));
			}
		});
		btn_satrtchat.setOnClickListener(this);
		btn_savefriend.setOnClickListener(this); 
		btn_editfriend.setOnClickListener(this); 
		btn_delefriend.setOnClickListener(this); 		
	}	
	

	
	@Override
	public void onClick(View arg0) {
		int id = arg0.getId();
		if (id == R.id.img_back) {
			Utils.finish(NearbyInfoActivity.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_NEARBY);
			intent.putExtra("Chat_Name", strUserName);
			startActivity(intent);
		} else if (id == R.id.btn_savefriend) {
			NearbyUtil.editSaveToIniFile(thisUser);
			Utils.showShortToast(NearbyInfoActivity.this, "正在与对方协商保存");
		} else if (id == R.id.btn_editfriend) {
			new MyAlertDialog(NearbyInfoActivity.this, "修改对方信息？",
					"你确定要修改对方信息吗？",
					new MyAlertDialog.OnSetListener() {
						@Override
						public void onSelect(int comm) {
							editFriend();
						}
					}).show();
		} else if (id == R.id.btn_delefriend) {
			new MyAlertDialog(NearbyInfoActivity.this, "删除该成员？",
					"你确定要从本地文件中移除该成员吗？",
					new MyAlertDialog.OnSetListener() {
						@Override
						public void onSelect(int comm) {
							NearbyUtil.deleNearbyPeople(NearbyInfoActivity.this, thisUser);
							//	deleteMember(uinfo);
						}
					}).show();
		}
	} 

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

	protected void editFriend(){
		String strnewid = txt_newid.getText().toString().trim();
		String nickname = txt_nickname.getText().toString().trim();
		int newid = 0;		
		if(!TextUtils.isEmpty(strnewid)){//判断是否已经登录服务器
	        newid = Integer.parseInt(strnewid);
	        if(newid<1100||newid>=3000){
				Utils.showShortToast(NearbyInfoActivity.this, "修改ID只能在1100至2999之间!");
				return;	        	
	        }else if(newid!=nUserId){
	        	if(NearbyUtil.isUseridExist(newid)!=0){
				Utils.showShortToast(NearbyInfoActivity.this, "该新ID已经被占用!");
				return;	
	        	}
	        }
	        newid = 0;	        
		}else if(TextUtils.isEmpty(nickname)){//判断是否已经登录服务器
			Utils.showShortToast(NearbyInfoActivity.this, "你还没有修改备注名!");
			return;
		}
	    if(thisUser!=null){
	    	return;
	    }
		if(!TextUtils.isEmpty(nickname)){//判断是否已经登录服务器
			thisUser.setNickName(nickname);
			NearbyUtil.editNearbyPeople(thisUser,nickname);			
		}
		if(newid != 0){//判断是否已经登录服务器
			NearbyUtil.modiSaveToIniFile(thisUser,newid);
			Utils.showShortToast(NearbyInfoActivity.this, "正在与对方协商保存");			
		}else
			Utils.showShortToast(NearbyInfoActivity.this, "已经保存");	
	}			


	

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