package com.lianxin.pubqq.nearby;

import java.io.File;

import android.graphics.Bitmap;
import android.os.Build;
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.panqq.client.getHeadClient;
import com.lianxin.panqq.client.callback.getCallBack;
import com.lianxin.panqq.common.BaseHeadActivity;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.common.Utils;
import com.lianxin.panqq.common.utils.UserheadUtils;
import com.lianxin.panqq.edit.MyAlertDialog;
//import com.lianxin.panqq.file.NearbyFileUtil;
import com.lianxin.pubqq.main.MyApp;
import com.lianxin.pubqq.setup.SetupExtendActivity;
import com.lianxin.panqq.list.utils.NearbyUtil;
import com.lianxin.panqq.main.PathUtil;
import com.lianxin.panqq.picker.ImagePickerDialog;
//import com.lianxin.panqq.thread.NearPeopleThread;
//import com.lianxin.panqq.utils.ImageLoader;
import com.lianxin.panqq.utils.ImagesUtils;
import com.lianxin.panqq.utils.PreferenceUtils;

//注册
public class NearbySetupActivity extends BaseHeadActivity implements OnClickListener {
	private TextView txt_title;
	private ImageView img_back;
	private Button btn_save, btn_notice;
	private RelativeLayout lay_image,lay_head;	
	private EditText txt_username;	
	private EditText txt_qianming;
	private ImageView img_user,img_head;	
	private int nImageid=-1,nAddress=0,nAge,nSex;	
	private int nIdentity = 9;	
	private String strNickname = "";
	private String strQianming = "有缘千里来相会";	
    RadioGroup rag_identity;
    RadioButton rab_b1,rab_b2,rab_b3,rab_b4,rab_b5;
    RadioButton rab_b6,rab_b7,rab_b8,rab_b9,rab_b10;    
    RadioGroup rag_qianming;
    RadioButton rab_c1,rab_c2,rab_c3,rab_c4,rab_c5;
    private boolean isValidated = true;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.setup_nearby);
		super.onCreate(savedInstanceState);
		
		initControl();
	//	initView();
		initData();
		setListener();		
	}

	protected void initControl() {

		img_back = (ImageView) findViewById(R.id.img_back);		
		txt_username = (EditText) findViewById(R.id.tv_user_username);
		img_user = (ImageView)findViewById(R.id.iv_user_image); 		
		img_head = (ImageView)findViewById(R.id.iv_user_head);
		txt_qianming = (EditText) findViewById(R.id.tv_qianming);		
		lay_image = (RelativeLayout) findViewById(R.id.rl_user_image);
		lay_head = (RelativeLayout) findViewById(R.id.rl_user_head);		
		btn_save = (Button)findViewById(R.id.btn_save);
		btn_notice = (Button)findViewById(R.id.btn_notice);		
		rag_identity=(RadioGroup)findViewById(R.id.rg_identity);
		rab_b1=(RadioButton)findViewById(R.id.rb_a1);
		rab_b2=(RadioButton)findViewById(R.id.rb_a2);
		rab_b3=(RadioButton)findViewById(R.id.rb_a3);
		rab_b4=(RadioButton)findViewById(R.id.rb_a4);
		rab_b5=(RadioButton)findViewById(R.id.rb_a5);	
		rab_b6=(RadioButton)findViewById(R.id.rb_a6);
		rab_b7=(RadioButton)findViewById(R.id.rb_a7);
		rab_b8=(RadioButton)findViewById(R.id.rb_a8);
		rab_b9=(RadioButton)findViewById(R.id.rb_a9);
		rab_b10=(RadioButton)findViewById(R.id.rb_a10);		
		rag_qianming=(RadioGroup)findViewById(R.id.rg_qianming);
		rab_c1=(RadioButton)findViewById(R.id.rb_c1);
		rab_c2=(RadioButton)findViewById(R.id.rb_c2);
		rab_c3=(RadioButton)findViewById(R.id.rb_c3);
		rab_c4=(RadioButton)findViewById(R.id.rb_c4);
		rab_c5=(RadioButton)findViewById(R.id.rb_c5);
        
		rag_identity.setOnCheckedChangeListener(new OnCheckedChangeListener(){       	 
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId==rab_b1.getId()){
                	nIdentity = 1;
                }else if(checkedId==rab_b2.getId()){
                	nIdentity = 2;
                }else if(checkedId==rab_b3.getId()){
                	nIdentity = 3;           	
                }else if(checkedId==rab_b4.getId()){
                	nIdentity = 4;	
                }else if(checkedId==rab_b5.getId()){
                	nIdentity = 5;
                }else if(checkedId==rab_b6.getId()){
                	nIdentity = 6;		
                }else if(checkedId==rab_b7.getId()){
                	nIdentity = 7;
                }else if(checkedId==rab_b8.getId()){
                	nIdentity = 8;
                }else if(checkedId==rab_b9.getId()){
                	nIdentity = 9;           	
                }else if(checkedId==rab_b10.getId()){
                	nIdentity = 10;	
                }else{
                	nIdentity = 9;
                }         
            } 
        });	
		rag_qianming.setOnCheckedChangeListener(new OnCheckedChangeListener(){       	 
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId==rab_c1.getId()){
                	strQianming = "相逢何必曾相识";
                }else if(checkedId==rab_c2.getId()){
                	strQianming = "有缘千里来相会";
                }else if(checkedId==rab_c3.getId()){
                	strQianming = "酒逢知己千杯少";          	
                }else if(checkedId==rab_c4.getId()){
                	strQianming = "心有灵犀一点通";	
                }else if(checkedId==rab_c5.getId()){
            		String str = txt_qianming.getText().toString().trim();               	
                	strQianming = str;
                }else{
                	strQianming = "有缘千里来相会";
        		}             
            } 
        });		
	}

	protected void initView() {

	}

	protected void initData() {
		//获取用户呢称
		String NickName = PreferenceUtils.getInstance(this).getNearbyNickName();		
		if(!TextUtils.isEmpty(NickName))
			txt_username.setText(NickName);	
		else{			
			NickName = Build.BRAND;	
		    txt_username.setText(NickName);	
	    }
		//获取用户头像
		
		String UserImage = PreferenceUtils.getInstance(MyApp.getInstance()).getNearbyUserImage();		
		if(!TextUtils.isEmpty(UserImage)){
			try{nImageid = Integer.parseInt(UserImage);
			} catch(Exception e){nImageid = 6;}	
			img_user.setImageResource(ImagesUtils.images[nImageid]); 
		}
		PathUtil.curPlayId = 1000;	
		String UserPic = PathUtil.getInstance().getHeadPath()+ "/1000.jpg";	
		if(UserPic != null&&new File(UserPic).exists())
			UserheadUtils.displayImage(UserPic, img_head);	
		
		//获取用户身份
		String str = PreferenceUtils.getInstance(MyApp.getInstance()).getNearbyUserSex();		
		if(!TextUtils.isEmpty(str)){
			try{nIdentity = Integer.parseInt(str);
			} catch(Exception e){nIdentity = 9;}			
		}else
			nIdentity = 9;
		if(nIdentity==9)
			rag_identity.check(rab_b9.getId());
		else if(nIdentity==1)
			rag_identity.check(rab_b1.getId());
		else if(nIdentity==2)
			rag_identity.check(rab_b2.getId());
		else if(nIdentity==3)
			rag_identity.check(rab_b3.getId());
		else if(nIdentity==4)
			rag_identity.check(rab_b4.getId());
		else if(nIdentity==5)
			rag_identity.check(rab_b5.getId());  
		else if(nIdentity==6)
			rag_identity.check(rab_b6.getId());
		else if(nIdentity==7)
			rag_identity.check(rab_b7.getId());
		else if(nIdentity==8)
			rag_identity.check(rab_b8.getId());
		else if(nIdentity==10)
			rag_identity.check(rab_b10.getId());
		else
			rag_identity.check(rab_b9.getId()); 		
		//获取用户签名
		str = PreferenceUtils.getInstance(MyApp.getInstance()).getNearbySignal();		
		if(!TextUtils.isEmpty(str))
			this.strQianming = str;
		else
			this.strQianming = "有缘千里来相会";			
		if(strQianming.equals("相逢何必曾相识"))
			rag_qianming.check(rab_c1.getId());
		else if(strQianming.equals("有缘千里来相会"))
			rag_qianming.check(rab_c2.getId());
		else if(strQianming.equals("酒逢知己千杯少"))
			rag_qianming.check(rab_c3.getId());
		else if(strQianming.equals("心有灵犀一点通"))
			rag_qianming.check(rab_c4.getId()); 
		else{
			rag_qianming.check(rab_c5.getId());
			txt_qianming.setText(strQianming);			
		}
	}

	protected void setListener() {

		img_back.setOnClickListener(this);
		btn_save.setOnClickListener(this);	
		btn_notice.setOnClickListener(this);			
		lay_image.setOnClickListener(this);		
		lay_head.setOnClickListener(this);		
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.img_back) {
			Utils.finish(NearbySetupActivity.this);
		} else if (id == R.id.btn_save) {
			isValidatedof();
			if (!isValidated)
				saveData();
		} else if (id == R.id.btn_notice) {
			isValidatedof();
			if (!isValidated) {
				saveData();
				//NearPeopleThread.loadNearbyMe();
				NearbyUtil.loadNearbyMe();
			}
		} else if (id == R.id.rl_user_image) {//修改头像
			change_image();
		} else if (id == R.id.rl_user_head) {//修改头像
			change_Userhead(0);
		}
	}

	private void isValidatedof() {
		isValidated = true;
		String userName = txt_username.getText().toString().trim();		

		if (TextUtils.isEmpty(userName)){
			Utils.showShortToast(NearbySetupActivity.this, "请填写用户名！");	
			txt_username.requestFocus();			
            return;
	    }
		if (userName.length() < 3){
			Utils.showShortToast(NearbySetupActivity.this, "用户名至少要3个英文字符或者两个汉字！");	
			txt_username.requestFocus();			
            return;
	    }

		if (nImageid < 0 || nImageid > 80 ){
			Utils.showShortToast(NearbySetupActivity.this, "请选择你的默认头像！");			
            return;
	    }
		strNickname = userName;		
		int checkedRadioButtonId = rag_qianming.getCheckedRadioButtonId();
		if (checkedRadioButtonId == R.id.rb_c1) {
			strQianming = "相逢何必曾相识";
		} else if (checkedRadioButtonId == R.id.rb_c2) {
			strQianming = "有缘千里来相会";
		} else if (checkedRadioButtonId == R.id.rb_c3) {
			strQianming = "酒逢知己千杯少";
		} else if (checkedRadioButtonId == R.id.rb_c4) {
			strQianming = "心有灵犀一点通";
		} else if (checkedRadioButtonId == R.id.rb_c5) {
			String str = txt_qianming.getText().toString().trim();
			strQianming = str;
		} else {
			strQianming = "有缘千里来相会";
		}	
		isValidated = false;
	}

	private void saveData() {
		//PreferenceUtils.getInstance(this).setNearbyNickName(strNickname);		
		//PreferenceUtils.getInstance(this).setNearbyUserImage(""+nImageid);
		//PreferenceUtils.getInstance(this).setNearbyUserSex(""+nIdentity);
		//PreferenceUtils.getInstance(this).setNearbySignal(strQianming);	
		GloableParams.NearByme.setUserName(strNickname);
		GloableParams.NearByme.setImagePos(nImageid);	
		GloableParams.NearByme.setSex(nIdentity);
		GloableParams.NearByme.setSignal(strQianming);		
		NearbyUtil.saveNearbyMe(null,GloableParams.NearByme);
		
		/*NearPeopleThread.NearByme.setUserName(strNickname);
		NearPeopleThread.NearByme.setImagePos(nImageid);	
		NearPeopleThread.NearByme.setSex(nIdentity);
		NearPeopleThread.NearByme.setSignal(strQianming);		
		NearbyUtil.saveNearbyMe(null,NearPeopleThread.NearByme);*/		
	}

	/**
	 * 更改头像
	 */
	public void change_image(){
 
		new ImagePickerDialog(this,
				ImagePickerDialog.THEME_HOLO_LIGHT,
				new ImagePickerDialog.OnImageSetListener() {
					@Override
					public void onImageSelect(int imageid) {
						nImageid = imageid;
						img_user.setImageResource(ImagesUtils.images[imageid]); 						
					}
				}).show();	
	}	

	@Override
	protected void draw_UserHead(Bitmap bitmap) {
		img_head.setImageBitmap(bitmap);
	}
	
	int isPublish = 0;
	@Override	
	protected void post_UserHead( String filepath) {

		isPublish = 0;
		new MyAlertDialog(NearbySetupActivity.this, "发布头像？",
				"你确定要在附近的人中公开这个自定义头像？", new MyAlertDialog.OnSetListener() {
					@Override
					public void onSelect(int comm) {
						isPublish = 1;
					}
				}).show();
		int nDestid = 10000;
		if (isPublish == 0) {// 判断是否是自己
			return;
		}
		getLoadingDialog("正在发布中...").show();
		getHeadClient.PostUserHead(1, nDestid, new getCallBack() {
			@Override
			public void onSuccess(int statusCode, byte[] recvBuf) {
				runOnUiThread(new Runnable() {
					public void run() {
						getLoadingDialog("正在发布中...").dismiss();

						Utils.showLongToast(NearbySetupActivity.this, "str");

					}
				});
			}

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