package com.lianxin.pubqq.extend;
import java.io.File;

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.TextView;

import com.lianxin.pubqq.R;
import com.lianxin.pubqq.activity.UserDetailInfoActivity;
import com.lianxin.pubqq.chat.EMChatActivity;
import com.lianxin.panqq.common.DefineChat;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.common.Utils;
import com.lianxin.panqq.common.BaseActivity;
import com.lianxin.panqq.common.utils.UserheadUtils;
import com.lianxin.panqq.list.bean.UserNode;
import com.lianxin.panqq.list.utils.FriendUtils;
import com.lianxin.panqq.list.utils.NearbyUtil;
import com.lianxin.panqq.list.utils.UserUtil;
import com.lianxin.panqq.main.PathUtil;
import com.lianxin.panqq.main.bean.NearPeople;
import com.lianxin.panqq.utils.ImagesUtils;
import com.lianxin.panqq.edit.ModeTransUserDialog;
import com.lianxin.panqq.edit.SetupSightDialog;
//import com.lianxin.panqq.file.UserFileUtil;
//好友详情
public class UserDetailActivity extends BaseActivity implements OnClickListener {
	private TextView txt_title, tv_name, tv_accout;
	private TextView tv_address, tv_signal;	
	private ImageView img_back, img_right;
	private String Name, UserId;
	private Button btn_sendmsg;
	private Button btn_transmode;	
	private int userId;
	private int userType;
	private int nImage,nSex,nAge;	
	private String userName;// 用户昵称
	private String strAddress = "";
	private String strSignaTure = "";  //个性签名
	private ImageView img_userpic,img_sex;	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_friendmsg);	
		super.onCreate(savedInstanceState);
		
		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);
		btn_sendmsg = (Button) findViewById(R.id.btn_sendmsg);
		btn_sendmsg.setTag("1");
		btn_transmode = (Button)findViewById(R.id.btn_mode_trans);		
		tv_name = (TextView) findViewById(R.id.tv_name);
		tv_accout = (TextView) findViewById(R.id.tv_accout);
		tv_address = (TextView) findViewById(R.id.tv_address);
		tv_signal = (TextView) findViewById(R.id.tv_signal);	
		img_userpic = (ImageView)findViewById(R.id.iv_avatar);
		img_sex = (ImageView)findViewById(R.id.iv_sex);		
	}

	protected void initView() {

	}

	protected void initData() {
		// 获取传过来的groupid		
		userId = getIntent().getIntExtra("USER_ID",10002);
		userType = getIntent().getIntExtra("USER_Type", 1);
		userName = getIntent().getStringExtra("USER_Name");	

		tv_name.setText(userName);
		tv_accout.setText("杳信号：" + userId);		
		PathUtil.curPlayId = userId;	
		String UserPic = PathUtil.getInstance().getHeadPath()+ "/"+userId+".jpg";	
		//String UserPic = PathUtil.getInstance().getHeadPath()+ "/"+GloableParams.m_szUserId+".jpg";	
		if(UserPic != null&&new File(UserPic).exists())
			UserheadUtils.displayImage(UserPic, img_userpic);	
		//如果是本地数据，则读取本地文件
		if( userId < 10000 ){		
			NearPeople person = NearbyUtil.getNearPeople(userId);
			//nAge = person.age;
			strAddress = "附近的人";	
			strSignaTure = person.getSignal();		
			nImage = person.getImagePos();
			int id = person.getSex();
			if(id==9)
				nSex = 1;
			else if(id==10)
				nSex = 2;
			else if(id<=4)
				nSex = 1;
			else
				nSex = 2;			
		}else if(TextUtils.isEmpty(GloableParams.m_szServerIp)){		
		    //UserNode thisUser = UserFileUtil.readOneUser(UserDetailActivity.this, userId);	
		    UserNode thisUser = UserUtil.readOneUser(UserDetailActivity.this, userId);			    
			//strUserName = thisUser.getName();
			nAge = thisUser.age;
			strAddress = thisUser.straddress;
			strSignaTure = thisUser.getName();			
			nImage = thisUser.imageid;
			String strSex = thisUser.sex;			
			if(strSex.equals("男"))
				nSex = 1;
			else
				nSex = 2;				     			     
		}	
		if(nSex==2)
			img_sex.setImageResource(R.drawable.ic_sex_female);	
		else
			img_sex.setImageResource(R.drawable.ic_sex_male);	
		tv_address.setText(strAddress);
		tv_signal.setText(strSignaTure);		
	}

	protected void setListener() {
		img_back.setOnClickListener(this);
	//	img_right.setOnClickListener(this);
		btn_sendmsg.setOnClickListener(this);
		btn_transmode.setOnClickListener(this);		
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.img_back) {
			Utils.finish(UserDetailActivity.this);
			/*	case R.id.img_right:

			break;*/
		} else if (id == R.id.btn_sendmsg) {
			if ("1".equals(v.getTag().toString())) {
				Intent intent = new Intent(this, EMChatActivity.class);
				intent.putExtra("Chat_ID", userId);
				intent.putExtra("Chat_Type", userType);
				intent.putExtra("Chat_Name", userName);
				//startActivity(intent);
				Utils.start_Activity(context, intent);
			} else {
				// TODO 添加好友
			}
		} else if (id == R.id.btn_mode_trans) {
			int mode = DefineChat.ModeType_MYFRIEND;
			if (userId < 10000)
				mode = DefineChat.ModeType_NEARBY;
			else if (FriendUtils.getFriendList(userId) > 0) {
				mode = DefineChat.ModeType_MYFRIEND;
			} else
				mode = DefineChat.ModeType_STRANG;

			new ModeTransUserDialog(UserDetailActivity.this, userId,
					new ModeTransUserDialog.OnSetListener() {
						@Override
						public void onSelect(int option) {
							//	 setSight( option );
							//txt_power.setText(""+admin);
						}
					}).show();
		}
	}

}
