
package com.lianxin.pubqq.activity;
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.CheckBox;
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.entity.LookUserFor;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.common.Utils;
import com.lianxin.panqq.common.BaseActivity;
import com.lianxin.panqq.list.utils.AddressUtils;
import com.lianxin.panqq.picker.AddressPickerDialog;
import com.lianxin.panqq.picker.AgePickerDialog;
import com.lianxin.panqq.picker.DeptPickerDialog;
//用户搜索界面
public class UserSearchActivity extends BaseActivity implements OnClickListener {
	private TextView txt_title;
	private ImageView img_back, img_right;
	//private String Name, UserId;
	private TextView txt_address, txt_age;	
	private EditText txt_userid, txt_username, et_code;	
	private Button btn_startsearch;
	private RadioGroup rag_sex;
	private RadioButton rab_sex_noselect;
	private RadioButton rab_sex_male;   
	private RadioButton rab_sex_female;	
	private RadioGroup rag_select1,rag_select2;
	private RadioButton rab_search_base,rab_search_super;	
	private CheckBox chk_online;	
	private RelativeLayout lay_nianling;
	private RelativeLayout lay_chengshi;  
	int searchMode = 2;	
	//int sex = 2;
	int ageMin = 18;
	int ageMax = 24;
	int nSex = 2;
	int nAddress = 2;
	int nDept = 2;	
	int RequestFrom = 0;	
	private LookUserFor	lookUserFor;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_usersearch);		
		super.onCreate(savedInstanceState);
		
		initControl();
		initView();
		initData();
		setListener();		
	}

//	@Override
	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_startsearch = (Button) findViewById(R.id.btn_startsearch);
		btn_startsearch.setTag("1");
		RequestFrom = getIntent().getIntExtra("REQUEST_FROM",0);
		
		txt_userid = (EditText) findViewById(R.id.tv_userid);			
		txt_username = (EditText) findViewById(R.id.tv_username);
		txt_address = (TextView) findViewById(R.id.tv_address);			
		txt_age = (TextView) findViewById(R.id.tv_ageandage);		
		lay_nianling = (RelativeLayout) findViewById(R.id.rl_nianling);
		lay_chengshi = (RelativeLayout) findViewById(R.id.rl_chengshi);	
		rag_sex=(RadioGroup)findViewById(R.id.rg_sex);
		rab_sex_noselect=(RadioButton)findViewById(R.id.rb_sex_noselect);		
		rab_sex_male=(RadioButton)findViewById(R.id.rb_sex_male);
		rab_sex_female=(RadioButton)findViewById(R.id.rb_sex_female);	
		rag_select1=(RadioGroup)findViewById(R.id.rg_select1);
		rag_select2=(RadioGroup)findViewById(R.id.rg_select2);	
		rab_search_base=(RadioButton)findViewById(R.id.rb_search_base);
		rab_search_super=(RadioButton)findViewById(R.id.rb_search_super);	
		chk_online = (CheckBox)findViewById(R.id.check_online);			
	}

//	@Override
	protected void initView() {
		setData();
	}

//	@Override
	protected void initData() {

	}
	
	protected void setData() {
		lookUserFor = GloableParams.lookUserFor;	
		if(lookUserFor == null )
		    lookUserFor = new LookUserFor();		
		txt_userid.setText(""+lookUserFor.getDestId());		
		txt_username.setText(lookUserFor.getDestName());
		nAddress = lookUserFor.getAddRess();
		txt_address.setText(AddressUtils.getAddress(nAddress));
		ageMin = lookUserFor.getAgeMin();
		ageMax = lookUserFor.getAgeMax();		
		txt_age.setText(""+ageMin+"-"+ageMax);		
/*		if(sex==1)
			rab_sex_male.setSelected(true);
		else if(sex==2)
			rab_sex_female.setSelected(true);
		else
			rab_sex_noselect.setSelected(true);	*/
		nSex = lookUserFor.getiSex();			
		if(nSex==1)
			rag_sex.check(rab_sex_male.getId());
		else if(nSex==2)
			rag_sex.check(rab_sex_female.getId());
		else
			rag_sex.check(rab_sex_noselect.getId());

		if(lookUserFor.isOnline)
			chk_online.setChecked(true);
		else
			chk_online.setChecked(false);		
	}

	public void getData() {
	
	}	
	
//	@Override
	protected void setListener() {
		img_back.setOnClickListener(this);
	//	img_right.setOnClickListener(this);
		btn_startsearch.setOnClickListener(this);
		
		lay_nianling.setOnClickListener(this); 
		lay_chengshi.setOnClickListener(this); 	
		rag_sex.setOnCheckedChangeListener(new OnCheckedChangeListener(){       	 
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                if(checkedId==rab_sex_noselect.getId()){
                	nSex = 0;
           //     	rag_sex.check(-1);                	
                }
                else if(checkedId==rab_sex_male.getId()){
                	nSex = 1;
                }                
                else if(checkedId==rab_sex_female.getId()){
                	nSex = 2;
                }                
            } 
        });	
		
		rag_select1.setOnCheckedChangeListener(new OnCheckedChangeListener(){       	 
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId==rab_search_base.getId()){
                	searchMode = 1;
             //   	rg_select2.check(-1);   
                	rag_select2.clearCheck (); 
                	rag_select1.check(rab_search_base.getId());                	
                }              
            }       
        });	
		rag_select2.setOnCheckedChangeListener(new OnCheckedChangeListener(){       	 
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId==rab_search_super.getId()){
                	searchMode = 2;
               // 	rg_select1.check(-1); 
                	rag_select1.clearCheck ();  
                	rag_select2.check(rab_search_super.getId());                 	
                }              
            } 
        });		
	}

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

			break;*/
		} else if (id == R.id.btn_startsearch) {
			onStartSearch();
		} else if (id == R.id.rl_chengshi) {//修改城市
			change_address();
			//change_keyword();
		} else if (id == R.id.rl_nianling) {//年龄范围
			change_age();
		}
	}

	public void change_address() {

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

	public void change_depart() {
		new DeptPickerDialog(UserSearchActivity.this,
				DeptPickerDialog.THEME_HOLO_LIGHT,nDept,true,
				new DeptPickerDialog.OnDeptSetListener() {
					@Override
					public void onDeptSelect(int address) {
						String textString = "" + address;
						Utils.showLongToast(UserSearchActivity.this, textString);
					}
				}).show();
	}
	
	public void change_age() {
		new AgePickerDialog(UserSearchActivity.this,
				AgePickerDialog.THEME_HOLO_LIGHT,ageMin,ageMax,
				new AgePickerDialog.OnAgeSetListener() {
					@Override
					public void onAgeSelect(int minage,int maxage) {
						ageMin = minage;
						ageMax = maxage;
						if(ageMin<0){
							ageMin = 0;
							ageMax = 150;						
						}
						String textString = "" + minage+"-" + maxage;
						txt_age.setText(""+ageMin+"-"+ageMax);						
						Utils.showLongToast(UserSearchActivity.this, textString);
					}
				}).show();
	}

	private void onStartSearch() {
		
		if (searchMode != 1&&searchMode != 2){
			Utils.showShortToast(this, "请选择精确查找或者条件查找！");	
			//txt_servip.requestFocus();			
            return;
	    }
		int userId = 0;
		String userName = "";		
		if (searchMode == 1){
			String strId = txt_userid.getText().toString().trim();
			userName = txt_username.getText().toString().trim();
			if (TextUtils.isEmpty(strId)){
				Utils.showShortToast(this, "用户ID不能为空！\n如果要按照用户名查找,请置用户ID为0");	
				txt_userid.requestFocus();			
	            return;
		    }			
	        userId = Integer.parseInt(txt_userid.getText().toString().trim()); // 获取年龄				
			if (userId == 0 && userName.length() == 0 )
			{
				Utils.showShortToast(this, "请输入用户ID！");				
				return;
			}
			else if (userId == 0 && userName.length() < 3 )
			{
				Utils.showShortToast(this, "不是有效的用户名！");				
				return;
			}
			else if (userId != 0 && userId<= 10000 )
			{
				Utils.showShortToast(this, "不是有效的用户ID！");					
				return;
			}
	    }
		lookUserFor = new LookUserFor();
		if (searchMode == 1){
			lookUserFor.LookForUser(userId,userName);	
	    }		
		else{
			boolean isOnline = false;	
			if(chk_online.isChecked())
				isOnline = true;				
			//lookUserFor.LookForUser( nAddress,ageMin,ageMax,nSex);	
			lookUserFor.LookForUser( nAddress,ageMin,ageMax,nSex,isOnline);			
	    }
		
		GloableParams.lookUserFor = lookUserFor;
		if(RequestFrom == 0){
		    Intent intent = new Intent(this, UserListActivity.class);
		    startActivity(intent);
		}else
	 	    setResult(RESULT_OK);		
		overridePendingTransition(R.anim.push_left_in,
				R.anim.push_left_out);
		finish();		
	}
}
