package com.lianxin.pubqq.setup;

import java.io.File;
import java.util.Date;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lianxin.pubqq.R;
import com.lianxin.panqq.common.BaseHeadActivity;
import com.lianxin.panqq.common.DefineUser;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.common.Utils;
import com.lianxin.panqq.list.utils.AddressUtils;
import com.lianxin.pubqq.main.MyApp;
import com.lianxin.panqq.main.PathUtil;
import com.lianxin.panqq.picker.AddressPickerDialog;
import com.lianxin.panqq.picker.CommonPickerDialog;
import com.lianxin.panqq.client.callback.getCallBack;
//import com.lianxin.panqq.utils.ImageLoader;
import com.lianxin.panqq.client.getHeadClient;
import com.lianxin.panqq.client.getInfoClient;
import com.lianxin.panqq.client.setInfoClient;
import com.lianxin.panqq.common.bean.UserDetailInfo;
import com.lianxin.panqq.common.utils.SelectUtils;
import com.lianxin.panqq.common.utils.UserheadUtils;
import com.lianxin.panqq.edit.MyAlertDialog;
//注册
public class SetupExtendActivity extends BaseHeadActivity implements OnClickListener {
//	private TextView txt_title;
	private ImageView img_back;
	private Button btn_modify, btn_logout,btn_exit, btn_regoff;
//	private EditText et_usertel, et_password, et_code;
	private TextView txt_profess;
	private TextView txt_animal,txt_blood,txt_star;	
	private RelativeLayout lay_userpic,lay_profess;
	private RelativeLayout lay_animal,lay_blood,lay_star;	
	private EditText txt_realname, txt_school, txt_email, txt_info;	
	private ImageView img_userpic;	
	//private int nImageid=-1,nAddress=0,nAge,nSex;	

	private String strRealName = "";
	private String strSchool = "";	
	private String strAnimal = "";	
	private String strBlood = "";
	private String strProFess = "";
	private String strStar = "";
	private String strEmail = "";
	private String strSignaTure = "";  //个性签名	
	private int iAnimal = 0;	
	private int iBlood = 0;
	private int iProFess = 0;
	private int iStar = 0;	
	//private RegisterFor reginfo;
	/*private File cameraFile;//照相机拍照的图片
	private File outFile;//照相机拍照的图片
	private File headFile;	
	public static final int USERPIC_REQUEST_CODE_LOCAL = 101;
	public static final int USERPIC_REQUEST_CODE_LOCAL_19 = 101;
	public static final int USERPIC_REQUEST_CODE_CAMERA = 102;
	public static final int USERPIC_REQUEST_CODE_CUT = 103;	*/
	@Override
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.setup_extend);
		super.onCreate(savedInstanceState);
		
		initControl();
		initData();		
		initView();

		setListener();		
	}

//	@Override
	protected void initControl() {

		img_back = (ImageView) findViewById(R.id.img_back);
//		img_back.setVisibility(View.VISIBLE);
		btn_modify = (Button) findViewById(R.id.btn_modify);
		btn_logout = (Button) findViewById(R.id.btn_logout);
		btn_exit = (Button) findViewById(R.id.btn_exit);
		btn_regoff = (Button) findViewById(R.id.btn_regoff);
		img_userpic = (ImageView)findViewById(R.id.iv_user_pic); 		
		txt_realname = (EditText) findViewById(R.id.tv_user_realname);
		txt_school = (EditText) findViewById(R.id.tv_user_school);
		txt_email = (EditText) findViewById(R.id.tv_user_email);
		txt_info = (EditText) findViewById(R.id.tv_user_info);	
		txt_profess = (TextView) findViewById(R.id.tv_user_profess);
		txt_animal = (TextView) findViewById(R.id.tv_user_animal);
		txt_blood = (TextView) findViewById(R.id.tv_user_blood);
		txt_star = (TextView) findViewById(R.id.tv_user_star);		
		lay_userpic = (RelativeLayout) findViewById(R.id.rl_user_pic);
		lay_profess = (RelativeLayout) findViewById(R.id.rl_user_profess);
		lay_animal = (RelativeLayout) findViewById(R.id.rl_user_animal);
		lay_blood = (RelativeLayout) findViewById(R.id.rl_user_blood);	
		lay_star = (RelativeLayout) findViewById(R.id.rl_user_star);
		
	}

	protected void initView() {
		txt_realname.setText(strRealName);
		txt_school.setText(strSchool);
		txt_email.setText(strEmail);	
		txt_profess.setText(strProFess);
		txt_animal.setText(strAnimal);		
		txt_blood.setText(strBlood);
		txt_star.setText(strStar);
		txt_info.setText(strSignaTure);		
	}

	protected void initData() {
		//获取用户信息
		//String UserPic = PreferenceUtils.getInstance(this).getSettingUserPic();
		PathUtil.curPlayId = GloableParams.m_szUserId;		
		String UserPic = PathUtil.getInstance().getHeadPath()+ "/"+GloableParams.m_szUserId+".jpg";	
		if(UserPic != null&&new File(UserPic).exists())
			UserheadUtils.displayImage(UserPic, img_userpic);	

		strRealName = GloableParams.myUserInfo.m_cRealName;
		strSchool = GloableParams.myUserInfo.m_strCollEge;	
		strAnimal = GloableParams.myUserInfo.m_cAnimal;	
		strBlood = GloableParams.myUserInfo.m_cBlood;
		strStar = GloableParams.myUserInfo.m_cStar;		
		strProFess = GloableParams.myUserInfo.m_strProFess;
		strEmail = GloableParams.myUserInfo.m_strMail;		
		iAnimal = GloableParams.myUserInfo.m_iAnimal;	
		iBlood = GloableParams.myUserInfo.m_iBlood;
		iStar = GloableParams.myUserInfo.m_iStar;		
		iProFess = GloableParams.myUserInfo.m_iSort;	
		strSignaTure = GloableParams.myUserInfo.m_strSignaTure;  //个性签名		
	}

	protected void setListener() {

		img_back.setOnClickListener(this);
		btn_modify.setOnClickListener(this);
		btn_logout.setOnClickListener(this);
		btn_exit.setOnClickListener(this);
		btn_regoff.setOnClickListener(this);		
		lay_userpic.setOnClickListener(this);		
		lay_profess.setOnClickListener(this); 
		lay_animal.setOnClickListener(this);
		lay_blood.setOnClickListener(this); 
		lay_star.setOnClickListener(this);		
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.img_back) {
			Utils.finish(SetupExtendActivity.this);
		} else if (id == R.id.btn_modify) {
			sendModify();
		} else if (id == R.id.btn_logout) {
			MyApp.getInstance().userLogOffline(0);
		} else if (id == R.id.btn_exit) {
			Utils.finish(SetupExtendActivity.this);
			MyApp.getInstance().exit();
		} else if (id == R.id.btn_regoff) {//MyApp.getInstance().exit();
			Intent intent2 = new Intent(this, SetupRegoutActivity.class);
			startActivity(intent2);
		} else if (id == R.id.rl_user_pic) {//修改头像
			change_Userhead(1);
		} else if (id == R.id.rl_user_profess) {//修改职业
			picker_address(SelectUtils.TYPE_PROFESS);
		} else if (id == R.id.rl_user_animal) {//修改属相
			picker_address(SelectUtils.TYPE_ANIMAL);
		} else if (id == R.id.rl_user_blood) {//修改血型
			picker_address(SelectUtils.TYPE_BLOOD);
		} else if (id == R.id.rl_user_star) {//修改星座
			picker_address(SelectUtils.TYPE_STAR);
		}
	}

	private void getRemoteInfo() {

		if (TextUtils.isEmpty(GloableParams.m_szServerIp)) {// 判断是否已经登录服务器
			return;
		}
		int nUserId = GloableParams.m_szUserId;
		int nOldId = GloableParams.myUserInfo.m_iUserId;
		int nowtime = (int) new Date().getTime() / 1000;
		if (nUserId != nOldId) {// 判断是否是自己

		} else if ((nowtime - 10000) >= 20) {

		}
		getLoadingDialog("正在刷新数据...").show();
		getInfoClient.GetUserInfo(1000, nUserId, 1, new getCallBack() {
			@Override
			public void onSuccess(int statusCode, byte[] recvBuf) {
				UserDetailInfo userExInfo = new UserDetailInfo(recvBuf);
				GloableParams.myUserInfo = userExInfo;
				runOnUiThread(new Runnable() {
					public void run() {
						getLoadingDialog("正在刷新数据...").dismiss();
						initData();
						initView();
					}
				});
			}

			@Override
			public void onFailure(int statusCode, String message) {
				// getLoadingDialog("正在刷新数据...").dismiss();
				final String str = message;
				runOnUiThread(new Runnable() {
					public void run() {
						getLoadingDialog("正在刷新数据...").dismiss();
						// Utils.showLongToast(MemberInfoActivity.this, str);
					}
				});
			}
		});

	}

	/* 修改扩展资料 */
	private void sendModify() {
		strRealName = txt_realname.getText().toString().trim();
		strSchool = txt_school.getText().toString().trim();
		strProFess = txt_profess.getText().toString().trim();
		strEmail = txt_email.getText().toString().trim();
		strSignaTure = txt_info.getText().toString().trim();
		// 真实姓名,毕业学校,分类,属相,血型,星座,电子信箱,职业,个性签名
		String info = "" + strRealName + "," + strSchool + "," + iProFess + ","
				+ iAnimal + "," + iBlood + "," + iStar + "," + strEmail + ","
				+ strProFess + "," + strSignaTure;
		getLoadingDialog("正在修改个人信息...").show();
		setInfoClient.SetExtendInfo(DefineUser.REGT_MODIFY_EXTEND, info,
				new getCallBack() {
					@Override
					public void onSuccess(int statusCode, byte[] recvBuf) {
						// UserDetailInfo userExInfo = new
						// UserDetailInfo(recvBuf);
						runOnUiThread(new Runnable() {
							public void run() {
								getLoadingDialog("正在修改个人信息...").dismiss();
								Utils.showLongToast(SetupExtendActivity.this,
										"修改成功");
							}
						});
					}

					@Override
					public void onFailure(int statusCode, String message) {
						// getLoadingDialog("正在刷新数据...").dismiss();
						final String str = message;
						runOnUiThread(new Runnable() {
							public void run() {
								getLoadingDialog("正在修改个人信息...").dismiss();
								Utils.showLongToast(SetupExtendActivity.this,
										str);
							}
						});
					}
				});
	}

	/**
	 * 更改地址
	 */
	public void change_address() {

		new AddressPickerDialog(this, AddressPickerDialog.THEME_HOLO_LIGHT,
				new AddressPickerDialog.OnAddressSetListener() {
					@Override
					public void onAddressSelect(int address) {
						// nAddress = address;
						txt_email.setText(AddressUtils.getAddress(address));
						// String textString = "" + address;
						// Utils.showLongToast(UserSearchActivity.this,
						// textString);
					}
				}).show();
	}

	public void picker_address(int mode) {
		final int nMode = mode;
		new CommonPickerDialog(this, mode,
				new CommonPickerDialog.OnSetListener() {
					@Override
					public void onSelect(int id, String option) {
						switch (nMode) {
						case SelectUtils.TYPE_ANIMAL: // 属相
							iAnimal = id;
							strAnimal = option;
							txt_animal.setText(option);
							break;
						case SelectUtils.TYPE_STAR: // 星座
							iStar = id;
							strStar = option;
							txt_star.setText(option);
							break;
						case SelectUtils.TYPE_BLOOD: // 血型
							iBlood = id;
							strBlood = option;
							txt_blood.setText(option);
							break;
						case SelectUtils.TYPE_PROFESS: // 职业
							iProFess = id;
							strProFess = option;
							txt_profess.setText(option);
							break;
						default:
							break;
						}
					}
				}).show();
	}

	/**
	 * 更改头像
	 */
	@Override
	protected void draw_UserHead(Bitmap bitmap) {
		img_userpic.setImageBitmap(bitmap);
	}

	int isUpload = 0;
	//private void PostUserHead(final String filepath, int destid) {
	@Override	
	protected void post_UserHead( String filepath) {

		isUpload = 0;
		new MyAlertDialog(SetupExtendActivity.this, "上传头像？",
				"你确定要向服务器上传这个自定义头像？", new MyAlertDialog.OnSetListener() {
					@Override
					public void onSelect(int comm) {
						isUpload = 1;
					}
				}).show();
		if (isUpload == 0) {// 判断是否是自己
			return;
		}
		int nDestid = 10001;		
		if (TextUtils.isEmpty(GloableParams.m_szServerIp)) {// 判断是否已经登录服务器
			Utils.showLongToast(SetupExtendActivity.this, "你还没有登录服务器!");
			return;
		}
		getLoadingDialog("正在上传中...").show();
		getHeadClient.PostUserHead(0, nDestid, new getCallBack() {
			@Override
			public void onSuccess(int statusCode, byte[] recvBuf) {
				runOnUiThread(new Runnable() {
					public void run() {
						getLoadingDialog("正在上传中...").dismiss();

						Utils.showLongToast(SetupExtendActivity.this, "上传成功");

					}
				});
			}

			@Override
			public void onFailure(int statusCode, String message) {
				// getLoadingDialog("正在刷新数据...").dismiss();
				final String str = message;
				runOnUiThread(new Runnable() {
					public void run() {
						getLoadingDialog("正在上传中...").dismiss();
						Utils.showLongToast(SetupExtendActivity.this, str);
					}
				});
			}
		});
	}
}
