package com.lianxin.pubqq.setup;

import java.util.Date;

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.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.lianxin.pubqq.R;
import com.lianxin.pubqq.main.MyApp;
import com.lianxin.panqq.client.getInfoClient;
import com.lianxin.panqq.client.setInfoClient;
import com.lianxin.panqq.client.callback.getCallBack;
import com.lianxin.panqq.common.BaseActivity;
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.panqq.common.bean.UserDetailInfo;
import com.lianxin.panqq.picker.AddressPickerDialog;
import com.lianxin.panqq.picker.ImagePickerDialog;
import com.lianxin.panqq.utils.ImagesUtils;
//注册
public class SetupBasicActivity extends BaseActivity implements OnClickListener {
	private ImageView img_back;
	private Button btn_modify, btn_logout,btn_exit, btn_regoff;
	private RelativeLayout lay_image,lay_address;	
	private EditText txt_username;	
	private EditText txt_age,  txt_sex, txt_image;
	private TextView txt_address;	
	private ImageView img_user;	
    RadioGroup rag_sex;
    RadioButton rab_sex_male;
    RadioButton rab_sex_female;	
    RadioGroup rag_allow;
    RadioButton rab_allow_public;
    RadioButton rab_allow_check;
    RadioButton rab_allow_private;    
	private int nUserId = 10000;//用户唯一id	
	private String strUserName = "情已停机";
	private int iImage = 10;
	private int iSex = 1;
	private String strSex = "男";	
	private int iAge = 20;
	private int iAddress = 10; 
	private int iAllow = 1; 	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.setup_basic);
		super.onCreate(savedInstanceState);
		
		initControl();
		initData();	
		initView();
		setListener();	
		
		//getRemoteInfo();
		int nOldId = GloableParams.myUserInfo.m_iUserId;
		int nowtime = (int) new Date().getTime()/1000;	
		if(GloableParams.m_szUserId != nOldId){//判断是否是自己
			getRemoteInfo();
		}else if((nowtime -10000 ) >= 20){
			getRemoteInfo();		
		}		
	}

	protected void initControl() {

		img_back = (ImageView) findViewById(R.id.img_back);
		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);			
		txt_username = (EditText) findViewById(R.id.tv_user_username);
		img_user = (ImageView)findViewById(R.id.iv_user_image); 		
		txt_address = (TextView) findViewById(R.id.tv_user_address);		
		lay_image = (RelativeLayout) findViewById(R.id.rl_user_image);
		lay_address = (RelativeLayout) findViewById(R.id.rl_user_address);		
		txt_age = (EditText) findViewById(R.id.tv_user_age);		
		rag_sex=(RadioGroup)findViewById(R.id.rg_sex);
		rab_sex_male=(RadioButton)findViewById(R.id.rb_sex_male);
		rab_sex_female=(RadioButton)findViewById(R.id.rb_sex_female);
        
		rag_sex.setOnCheckedChangeListener(new OnCheckedChangeListener(){       	 
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                if(checkedId==rab_sex_male.getId()){
       //         	sex = "1";
      //              Toast.makeText(RegisterActivity.this,"男", Toast.LENGTH_LONG).show();
                }
                if(checkedId==rab_sex_female.getId()){
        //        	sex = "2";
      //              Toast.makeText(RegisterActivity.this,"女", Toast.LENGTH_LONG).show();
                }                
            } 
        });	
		rag_allow=(RadioGroup)findViewById(R.id.rg_allow);
		rab_allow_public=(RadioButton)findViewById(R.id.rb_allow_Public);
		rab_allow_check=(RadioButton)findViewById(R.id.rb_allow_Friend);
		rab_allow_private=(RadioButton)findViewById(R.id.rb_allow_Private);		
	}

	protected void initView() {
		txt_username.setText(strUserName);
		txt_age.setText(""+iAge);
		txt_address.setText(AddressUtils.getAddress(iAddress));	
		img_user.setImageResource(ImagesUtils.images[iImage]);	
		if(iSex==2)
		    rag_sex.check(R.id.rb_sex_female);
		else
		    rag_sex.check(R.id.rb_sex_male);				
		if(iAllow == 2)
			rag_allow.check(R.id.rb_allow_Public);
		else if(iAllow == 1)
			rag_allow.check(R.id.rb_allow_Friend);
		else if(iAllow == 0)
			rag_allow.check(R.id.rb_allow_Private);		
	}

	protected void initData() {
		nUserId = GloableParams.myUserInfo.m_iUserId;//用户唯一id	
		strUserName = GloableParams.myUserInfo.m_cUserName;
		iImage = GloableParams.myUserInfo.m_imyIcon;
		iSex = GloableParams.myUserInfo.m_iSex;
		strSex = GloableParams.myUserInfo.m_cSex;	
		iAge = GloableParams.myUserInfo.m_iAge;
		iAddress = GloableParams.myUserInfo.m_iCity;
		iAllow = GloableParams.myUserInfo.m_iAllow;		
	}

	protected void setListener() {

		img_back.setOnClickListener(this);
		btn_modify.setOnClickListener(this);
		btn_logout.setOnClickListener(this);
		btn_exit.setOnClickListener(this);
		btn_regoff.setOnClickListener(this);		
		lay_image.setOnClickListener(this);		
		lay_address.setOnClickListener(this); 
	//	txt_address.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.img_back) {
			Utils.finish(SetupBasicActivity.this);
			//	finish(RegisterActivity.this);
			//    super.onBackPressed();
			//finish();
			//overridePendingTransition(R.anim.push_right_in,
			//		R.anim.push_right_out);
		} else if (id == R.id.btn_modify) {
			sendModify();
		} else if (id == R.id.btn_logout) {//MyApp.getInstance().logout();
			MyApp.getInstance().userLogOffline(0);
			//Intent intent1 = new Intent(this, SetupLogoffActivity.class);
			//startActivity(intent1);
		} else if (id == R.id.btn_exit) {
			Utils.finish(SetupBasicActivity.this);
			MyApp.getInstance().exit();
			//finish();
			//overridePendingTransition(R.anim.push_left_in,
			//		R.anim.push_left_out);
		} 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_image) {//修改头像
			change_image();
		} else if (id == R.id.rl_user_address) {//修改城市
			change_address();
		} else if (id == R.id.tv_user_address) {//修改城市
			change_address();
		}
	}

	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() {
		String userName = txt_username.getText().toString().trim();		
		if (TextUtils.isEmpty(userName)){
			Utils.showShortToast(SetupBasicActivity.this, "请填写用户名！");	
			txt_username.requestFocus();			
            return;
	    }
		if (userName.length() < 3){
			Utils.showShortToast(SetupBasicActivity.this, "用户名至少要3个英文字符或者两个汉字！");	
			txt_username.requestFocus();			
            return;
	    }		
        iAge = Integer.parseInt(txt_age.getText().toString().trim()); // 获取年龄
		if (iAge < 0 || iAge > 120 ){
			Utils.showShortToast(SetupBasicActivity.this, "请输入正确的年龄！");	
			txt_age.requestFocus();			
            return;
	    }
		if (iImage < 0 || iImage > 80 ){
			Utils.showShortToast(SetupBasicActivity.this, "请选择你的默认头像！");	
		//	txt_age.requestFocus();			
            return;
	    }
		if (iAddress <= 0 || iAddress > 720 ){
			Utils.showShortToast(SetupBasicActivity.this, "请选择你的所在地区！");	
		//	txt_age.requestFocus();			
            return;
	    }		
		int checkedRadioButtonId = rag_sex.getCheckedRadioButtonId();//			Utils.showShortToast(RegisterActivity.this, "请选择性别！");
		if (checkedRadioButtonId == R.id.rb_sex_female) {
			strSex = "女";
			iSex = 2;
		} else if (checkedRadioButtonId == R.id.rb_sex_male) {
			strSex = "男";
			iSex = 1;
		}
		int radioButtonId = rag_allow.getCheckedRadioButtonId();//			Utils.showShortToast(RegisterActivity.this, "请选择性别！");
		if (radioButtonId == R.id.rb_allow_Public) {
			iAllow = 2;
		} else if (radioButtonId == R.id.rb_allow_Friend) {
			iAllow = 1;
		} else if (radioButtonId == R.id.rb_allow_Private) {
			iAllow = 0;
		}
		//if(3>2)
		//	return;
		int iProv = AddressUtils.getAddressType(iAddress);
        //用户呢称,默认头像,用户年龄,省份,城市,用户性别,开放级别		
		String info = ""+userName+","+iImage+","+iAge+","+iProv+","+iAddress+","+iSex+","+iAllow;
		getLoadingDialog("正在修改个人信息...").show();	
		setInfoClient.SetExtendInfo(DefineUser.REGT_MODIFY_BASIC,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(SetupBasicActivity.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(SetupBasicActivity.this, str);
					}
				});	
			}
		});	 
	}
	/**
	 * 更改头像
	 */
	public void change_image(){
 
		new ImagePickerDialog(this,
				ImagePickerDialog.THEME_HOLO_LIGHT,
				new ImagePickerDialog.OnImageSetListener() {
					@Override
					public void onImageSelect(int imageid) {
						//String textString = KeywordUtils.getKeyword(keyword);
						iImage = imageid;
						img_user.setImageResource(ImagesUtils.images[iImage]); 						
					//	txt_keyword.setText(KeywordUtils.getKeyword(nKeyword));						
					//	Utils.showLongToast(GroupSearchActivity.this, textString);
					}
				}).show();	
	}	
	/**
	 * 更改地址
	 */
	public void change_address(){
 
		new AddressPickerDialog(this,
				AddressPickerDialog.THEME_HOLO_LIGHT,iAddress,false,
				new AddressPickerDialog.OnAddressSetListener() {
					@Override
					public void onAddressSelect(int address) {
						iAddress = address;
						txt_address.setText(AddressUtils.getAddress(address));						
					//	String textString = "" + address;
					//	Utils.showLongToast(UserSearchActivity.this, textString);
					}
				}).show();	       
	}

}
