
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.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.LookGroupFor;
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.list.utils.CityUtils;
import com.lianxin.panqq.list.utils.CollegeUtils;
import com.lianxin.panqq.list.utils.DepartUtils;
import com.lianxin.panqq.common.utils.KeywordUtils;
import com.lianxin.panqq.picker.AddressPickerDialog;
import com.lianxin.panqq.picker.CityPickerDialog;
import com.lianxin.panqq.picker.DeptPickerDialog;
import com.lianxin.panqq.picker.GradePickerDialog;
import com.lianxin.panqq.picker.ImagePickerDialog;
import com.lianxin.panqq.picker.KeyPickerDialog;
import com.lianxin.panqq.common.DefineUser;
//搜索群组
public class GroupSearchActivity extends BaseActivity implements OnClickListener {
	private TextView txt_title;
	private ImageView img_back, img_right;
	private String Name, UserId;
	private TextView txt_keyword, txt_address;	
	private EditText txt_groupid, txt_groupname;
	private TextView cap_keyword, cap_address;	
	private TextView cap_groupid, cap_groupname;	
	private Button btn_startsearch;

	private RadioGroup rag_select1,rag_select2;
	private RadioButton rab_search_base,rab_search_super;	
	private RelativeLayout lay_keyword;
	private RelativeLayout lay_address; 
	int groupType = DefineUser.TYPE_CROWD;		
	int searchMode = 2;	

	private int  nDestId = 10058;	
	private  String strName = "abcd";	
	int nKeyword = 0x100;
	int nAddress = 2;
	int nCollege = 24;
	int nEnyear = 2002;
	int nDept = 10;
	int nCity = 2;	
	int RequestFrom = 0;	
	private LookGroupFor	lookGroupFor;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_groupsearch);		
		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);
		if(RequestFrom == 0)
			groupType = getIntent().getIntExtra("SEARCH_TYPE",DefineUser.TYPE_CROWD);
		if(RequestFrom == 1)
			groupType = getIntent().getIntExtra("SEARCH_TYPE",DefineUser.TYPE_CROWD);
		
		txt_groupid = (EditText) findViewById(R.id.tv_groupid);			
		txt_groupname = (EditText) findViewById(R.id.tv_groupname);
		txt_keyword = (TextView) findViewById(R.id.tv_keyword);			
		txt_address = (TextView) findViewById(R.id.tv_address);	
		cap_groupid = (TextView) findViewById(R.id.tv_groupid_cap);			
		cap_groupname = (TextView) findViewById(R.id.tv_groupname_cap);
		cap_keyword = (TextView) findViewById(R.id.tv_keyword_cap);			
		cap_address = (TextView) findViewById(R.id.tv_address_cap);			
		lay_keyword = (RelativeLayout) findViewById(R.id.rl_keyword);
		lay_address = (RelativeLayout) findViewById(R.id.rl_address);	

		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);	
			
	}

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

//	@Override
	protected void initData() {

	}
	
	protected void setData() {
		lookGroupFor = GloableParams.lookGroupFor;	
		if(lookGroupFor == null )
		    lookGroupFor = new LookGroupFor();		

		nDestId = lookGroupFor.getDestId(groupType);
		strName = lookGroupFor.getDestName(groupType);	
		nKeyword = lookGroupFor.getKeyWord(groupType);
		nAddress = lookGroupFor.getAddRess(groupType);	
		txt_groupid.setText(""+nDestId);		
		txt_groupname.setText(strName);		
		switch (groupType) {
		case DefineUser.TYPE_FRIEND:
			txt_keyword.setText(KeywordUtils.getKeyword(nKeyword));
			txt_address.setText(AddressUtils.getAddress(nAddress));	
			break;		
		case DefineUser.TYPE_CROWD:
			txt_keyword.setText(KeywordUtils.getKeyword(nKeyword));
			txt_address.setText(AddressUtils.getAddress(nAddress));			
			break;
		case DefineUser.TYPE_MYCLASS:	
			nCollege = nKeyword;
			nEnyear = nAddress;	
			txt_keyword.setText(CollegeUtils.getCollege(nCollege));	
			txt_address.setText("" + nEnyear);				
			break;
		case DefineUser.TYPE_DEPART:
			nDept = nAddress;
			txt_keyword.setText(DepartUtils.getDepartName(nDept));						
			txt_address.setText(DepartUtils.getDepartTypeName(nDept));			
			break;		
		case DefineUser.TYPE_PARTY:
			nDept = nAddress;
			txt_keyword.setText(DepartUtils.getDepartName(nDept));						
			txt_address.setText(DepartUtils.getDepartTypeName(nDept));	
			break;
		case DefineUser.TYPE_GROUP:
			nCity = nAddress;
			txt_keyword.setText(CityUtils.getCityTypeName(nCity));						
			txt_address.setText(CityUtils.getCityName(nCity));	
			break;
		case DefineUser.TYPE_CITY:
			nCity = nAddress;
			txt_keyword.setText(CityUtils.getCityTypeName(nCity));						
			txt_address.setText(CityUtils.getCityName(nCity));
			break;		
	
		default:
			txt_keyword.setText(KeywordUtils.getKeyword(nKeyword));
			txt_address.setText(AddressUtils.getAddress(nAddress));			
			break;
		}  		
	}
	
	protected void setCaption() {
		switch (groupType) {
		case DefineUser.TYPE_FRIEND:
			txt_title.setText("查找群");
			cap_groupid.setText("输入群ID");
			cap_groupname.setText("输入群名");
			cap_keyword.setText("关  键  字");
			cap_address.setText("兴趣地域");	
			break;		
		case DefineUser.TYPE_CROWD:
			txt_title.setText("查找群");
			cap_groupid.setText("输入群ID");
			cap_groupname.setText("输入群名");
			cap_keyword.setText("关  键  字");
			cap_address.setText("兴趣地域");			
			break;
		case DefineUser.TYPE_MYCLASS:
			txt_title.setText("查找班");
			cap_groupid.setText("输入班ID");
			cap_groupname.setText("输入班名");
			cap_keyword.setText("所属院系");
			cap_address.setText("入学年份");	
			break;
		case DefineUser.TYPE_DEPART:
			txt_title.setText("查找部门");
			cap_groupid.setText("输入部门ID");
			cap_groupname.setText("输入部门名");
			cap_keyword.setText("所属父项");
			cap_address.setText("主管部门");	
			break;		
		case DefineUser.TYPE_PARTY:
			txt_title.setText("查找会议室");
			cap_groupid.setText("输入会议ID");
			cap_groupname.setText("输入会议名");
			cap_keyword.setText("所属父项");
			cap_address.setText("主管部门");	
			break;
		case DefineUser.TYPE_GROUP:
			txt_title.setText("查找俱乐部");
			cap_groupid.setText("输入部ID");
			cap_groupname.setText("输入部名");
			cap_keyword.setText("所属省份");
			cap_address.setText("所在城市");	
			break;
		case DefineUser.TYPE_CITY:
			txt_title.setText("查找地域");
			cap_groupid.setText("输入地域ID");
			cap_groupname.setText("输入地域名");
			cap_keyword.setText("所属省份");
			cap_address.setText("所在城市");	
			break;		
	
		default:
			txt_title.setText("查找群");
			cap_groupid.setText("输入群ID");
			cap_groupname.setText("输入群名");
			cap_keyword.setText("关  键  字");
			cap_address.setText("兴趣地域");				
			break;
		}			
	}		
	
	public void getData() {
	
	}	
	
//	@Override
	protected void setListener() {
		img_back.setOnClickListener(this);
	//	img_right.setOnClickListener(this);
		btn_startsearch.setOnClickListener(this);
		
		lay_keyword.setOnClickListener(this); 
		lay_address.setOnClickListener(this); 	

		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(GroupSearchActivity.this);
			//finish();
			/*	case R.id.img_right:

			break;*/
		} else if (id == R.id.btn_startsearch) {
			onStartSearch();
		} else if (id == R.id.rl_address) {//修改城市
			Select_Address();
		} else if (id == R.id.rl_keyword) {//年龄范围
			Select_Keyword();
		}
	}

	protected void Select_Keyword() {
		switch (groupType) {
		case DefineUser.TYPE_FRIEND:
			change_keyword();
			break;		
		case DefineUser.TYPE_CROWD:
			change_keyword();		
			break;
		case DefineUser.TYPE_MYCLASS:
			change_grade();					
			break;
		case DefineUser.TYPE_DEPART:
			change_depart();
			break;		
		case DefineUser.TYPE_PARTY:
			change_depart();
			break;
		case DefineUser.TYPE_GROUP:
			change_city();	
			break;
		case DefineUser.TYPE_CITY:
			change_city();
			break;			
		default:
			change_keyword();		
			break;
		}  		
	}	

	protected void Select_Address() {
		switch (groupType) {
		case DefineUser.TYPE_FRIEND:
			change_address();
			break;		
		case DefineUser.TYPE_CROWD:
			change_address();		
			break;
		case DefineUser.TYPE_MYCLASS:
			change_grade();					
			break;
		case DefineUser.TYPE_DEPART:
			change_depart();
			break;		
		case DefineUser.TYPE_PARTY:
			change_depart();
			break;
		case DefineUser.TYPE_GROUP:
			change_city();	
			break;
		case DefineUser.TYPE_CITY:
			change_city();
			break;			
		default:
			change_address();		
			break;
		}  		
	}	
	
	public void change_address() {	
		new AddressPickerDialog(GroupSearchActivity.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(GroupSearchActivity.this, textString);
					}
				}).show();
	}

	public void change_city() {
		new CityPickerDialog(GroupSearchActivity.this,
				CityPickerDialog.THEME_HOLO_LIGHT,nCity,true,
				new CityPickerDialog.OnCitySetListener() {
					@Override
					//public void onCitySelect(int province,int city) {					
					public void onCitySelect(int city) {
						nCity = city;
						txt_keyword.setText(CityUtils.getCityTypeName(city));						
						txt_address.setText(CityUtils.getCityName(city));						
						String textString = "" + city;
						Utils.showLongToast(GroupSearchActivity.this, textString);
					}
				}).show();
	}	
	
	public void change_depart() {
		new DeptPickerDialog(GroupSearchActivity.this,
				DeptPickerDialog.THEME_HOLO_LIGHT,nDept,true,
				new DeptPickerDialog.OnDeptSetListener() {
					@Override
					public void onDeptSelect(int address) {
						nDept = address;
						txt_keyword.setText(DepartUtils.getDepartName(address));						
					//	nAddress = DepartUtils.getDepartType(nKeyword);
						txt_address.setText(DepartUtils.getDepartTypeName(address));						
						String textString = "" + address;
						Utils.showLongToast(GroupSearchActivity.this, textString);
					}
				}).show();
	}

	public void change_grade() {
		new GradePickerDialog(GroupSearchActivity.this,
				GradePickerDialog.THEME_HOLO_LIGHT,nCollege,nEnyear,
				new GradePickerDialog.OnGradeSetListener() {
					@Override
					public void onGradeSelect(int minage,int maxage) {
						nCollege = minage;
						nEnyear = maxage;	
						txt_keyword.setText(CollegeUtils.getCollege(nCollege));	
						txt_address.setText("" + nEnyear);							
						String textString = "" + minage+"-" + maxage;
				//		txt_age.setText(""+ageMin+"-"+ageMax);						
						Utils.showLongToast(GroupSearchActivity.this, textString);
					}
				}).show();
	}	
	
	public void change_keyword() {
		new KeyPickerDialog(GroupSearchActivity.this,
				KeyPickerDialog.THEME_HOLO_LIGHT,nKeyword,
				new KeyPickerDialog.OnKeySetListener() {
					@Override
					public void onKeySelect(int keyword) {
						String textString = KeywordUtils.getKeyword(keyword);
						nKeyword = keyword;
						txt_keyword.setText(KeywordUtils.getKeyword(nKeyword));						
					//	Utils.showLongToast(GroupSearchActivity.this, textString);
					}
				}).show();
	}	

	public void change_image() {
		new ImagePickerDialog(GroupSearchActivity.this,
				ImagePickerDialog.THEME_HOLO_LIGHT,
				new ImagePickerDialog.OnImageSetListener() {
					@Override
					public void onImageSelect(int keyword) {
						String textString = KeywordUtils.getKeyword(keyword);
						nKeyword = keyword;
						txt_keyword.setText(KeywordUtils.getKeyword(nKeyword));						
					//	Utils.showLongToast(GroupSearchActivity.this, textString);
					}
				}).show();
	}	
	
	private void onStartSearch() {
		
		if (searchMode != 1&&searchMode != 2){
			Utils.showShortToast(this, "请选择精确查找或者条件查找！");				
            return;
	    }
		
		if (searchMode == 1){
			String strId = txt_groupid.getText().toString().trim();
			String userName = txt_groupname.getText().toString().trim();			
			if (TextUtils.isEmpty(strId)){
				Utils.showShortToast(GroupSearchActivity.this, "群组ID不能为空！\n如果要按照群组名查找,请置此项为0");	
				txt_groupid.requestFocus();			
	            return;
		    }
	        int userId = Integer.parseInt(txt_groupid.getText().toString().trim()); // 获取年龄				
			if (checkIsVaild() == 0 )//如果ID无效和群名无效
			    return;
			lookGroupFor.saveSearchDate(groupType,userId,userName);
			lookGroupFor.LookForGroup(groupType,userId,userName);			
	    }else if(groupType==DefineUser.TYPE_CROWD){
			lookGroupFor.saveSearchDate( groupType,nKeyword,nAddress);
			lookGroupFor.LookForGroup( groupType,nKeyword,nAddress);
	    }else if(groupType==DefineUser.TYPE_MYCLASS){
			lookGroupFor.saveSearchDate( groupType,nCollege,nEnyear);
			lookGroupFor.LookForGroup( groupType,nCollege,nEnyear);
	    }else if(groupType==DefineUser.TYPE_PARTY){
			lookGroupFor.saveSearchDate( groupType,nDept,nDept);
			lookGroupFor.LookForGroup( groupType,nDept,nDept);
	    }else if(groupType==DefineUser.TYPE_DEPART){
			lookGroupFor.saveSearchDate( groupType,nDept,nDept);
			lookGroupFor.LookForGroup( groupType,nDept,nDept);	
	    }else if(groupType==DefineUser.TYPE_GROUP){
			lookGroupFor.saveSearchDate( groupType,nCity,nCity);
			lookGroupFor.LookForGroup( groupType,nCity,nCity);
	    }else if(groupType==DefineUser.TYPE_CITY){
			lookGroupFor.saveSearchDate( groupType,nCity,nCity);
			lookGroupFor.LookForGroup( groupType,nCity,nCity);			
	    }

		GloableParams.lookGroupFor = lookGroupFor;
		if(RequestFrom == 0){
			//if(groupType==DefineUser.TYPE_CROWD||groupType==DefineUser.TYPE_MYCLASS)
			//{
		        Intent intent = new Intent(this, GroupListActivity.class);
			    intent.putExtra("Info_GroupType", groupType);		    
		        startActivity(intent);
			/*}else
			{
		        Intent intent = new Intent(this, AdminGroupActivity.class);
			    intent.putExtra("Info_GroupType", groupType);		    
		        startActivity(intent);
			}*/			
		}else
	 	    setResult(RESULT_OK);		
		overridePendingTransition(R.anim.push_left_in,
				R.anim.push_left_out);
		finish();		
	}
	
    private int checkIsVaild() {
		String strId = txt_groupid.getText().toString().trim();
		String userName = txt_groupname.getText().toString().trim();	
		int userId = Integer.parseInt(txt_groupid.getText().toString().trim()); // 获取年龄
		if (userId == 0 && userName.length() == 0) {

			switch (groupType) {
			case DefineUser.TYPE_FRIEND:
				Utils.showShortToast(this, "请输入群ID！");
				break;
			case DefineUser.TYPE_CROWD:
				Utils.showShortToast(this, "请输入群ID！");
				break;
			case DefineUser.TYPE_MYCLASS:
				Utils.showShortToast(this, "请输入班ID！");
				break;
			case DefineUser.TYPE_DEPART:
				Utils.showShortToast(this, "请输入部门ID！");
				break;
			case DefineUser.TYPE_PARTY:
				Utils.showShortToast(this, "请输入会议室ID！");
				break;
			case DefineUser.TYPE_GROUP:
				Utils.showShortToast(this, "请输入俱乐部ID！");
				break;
			case DefineUser.TYPE_CITY:
				Utils.showShortToast(this, "请输入地域ID！");
				break;
			default:
				Utils.showShortToast(this, "请输入群ID！");
				break;
			}
			return 0;
		} else if (userId == 0 && userName.length() < 3) {
			switch (groupType) {
			case DefineUser.TYPE_FRIEND:
				Utils.showShortToast(this, "不是有效的群名！");
				break;
			case DefineUser.TYPE_CROWD:
				Utils.showShortToast(this, "不是有效的群名！");
				break;
			case DefineUser.TYPE_MYCLASS:
				Utils.showShortToast(this, "不是有效的班名！");
				break;
			case DefineUser.TYPE_DEPART:
				Utils.showShortToast(this, "不是有效的部门名！");
				break;
			case DefineUser.TYPE_PARTY:
				Utils.showShortToast(this, "不是有效的会议室名！");
				break;
			case DefineUser.TYPE_GROUP:
				Utils.showShortToast(this, "不是有效的俱乐部名！");
				break;
			case DefineUser.TYPE_CITY:
				Utils.showShortToast(this, "不是有效的地域名！");
				break;
			default:
				Utils.showShortToast(this, "不是有效的群名！");
				break;
			}
			return 0;
		}

		switch (groupType) {
		case DefineUser.TYPE_FRIEND:
			if (userId != 0 && userId <= 0) {
				Utils.showShortToast(this, "不是有效的群ID！");
				return 0;
			}
			break;
		case DefineUser.TYPE_CROWD:
			if (userId != 0 && userId <= 10000) {
				Utils.showShortToast(this, "不是有效的群ID！");
				return 0;
			}
			break;
		case DefineUser.TYPE_MYCLASS:
			if (userId != 0 && userId <= 1000) {
				Utils.showShortToast(this, "不是有效的班ID！");
				return 0;
			}
			break;
		case DefineUser.TYPE_DEPART:
			if (userId != 0 && userId <= 0) {
				Utils.showShortToast(this, "不是有效的部门ID！");
				return 0;
			}
			break;
		case DefineUser.TYPE_PARTY:
			if (userId != 0 && userId <= 10000) {
				Utils.showShortToast(this, "不是有效的会议室ID！");
				return 0;
			}
			break;
		case DefineUser.TYPE_GROUP:
			if (userId != 0 && userId <= 10000) {
				Utils.showShortToast(this, "不是有效的俱乐部ID！");
				return 0;
			}
			break;
		case DefineUser.TYPE_CITY:
			if (userId != 0 && userId <= 0) {
				Utils.showShortToast(this, "不是有效的地域ID！");
				return 0;
			}
			break;
		default:
			if (userId != 0 && userId <= 10000) {
				Utils.showShortToast(this, "不是有效的群ID！");
				return 0;
			}
			break;
		}
		return 1;
	} 	
}
