package com.view;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;

public class SearchConditions extends Activity {
	
	private EditText et_keywords;
	private EditText tv_sdt,tv_edt;
	private Button bt_sd,bt_st,bt_ed,bt_et;
	private Button bt_search;
	
	static final int START_DATE_DIALOG_ID=0;
	static final int START_TIME_DIALOG_ID=1;
	static final int END_DATE_DIALOG_ID=2;
	static final int END_TIME_DIALOG_ID=3;
	
	private int syear,smonth,sday,shr,smin;
	private int eyear,emonth,eday,ehr,emin;
	
	String edate="";
	String etime="";
	String sdate="";
	String stime="";
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.searchconditions);
		
		et_keywords=(EditText)findViewById(R.id.kwrds_in_scond);
		tv_sdt=(EditText)findViewById(R.id.sdt_in_scond);
		tv_sdt.setEnabled(false);
		tv_edt=(EditText)findViewById(R.id.edt_in_scond);
		tv_edt.setEnabled(false);
		bt_sd=(Button)findViewById(R.id.sdate_in_scond);
		bt_st=(Button)findViewById(R.id.stime_in_scond);
		bt_ed=(Button)findViewById(R.id.edate_in_scond);
		bt_et=(Button)findViewById(R.id.etime_in_scond);
		bt_search=(Button)findViewById(R.id.search_in_scond);
		
		final Calendar c=Calendar.getInstance();
		
		bt_sd.setOnClickListener(new OnClickListener()
		{
			public void onClick(View view)
			{
				showDialog(SearchConditions.START_DATE_DIALOG_ID);
			}
		});
		bt_st.setOnClickListener(new OnClickListener()
		{
			public void onClick(View view)
			{
				showDialog(SearchConditions.START_TIME_DIALOG_ID);
			}
		});
		bt_ed.setOnClickListener(new OnClickListener()
		{
			public void onClick(View view)
			{
				showDialog(SearchConditions.END_DATE_DIALOG_ID);
			}
		});
		bt_et.setOnClickListener(new OnClickListener()
		{
			public void onClick(View view)
			{
				showDialog(SearchConditions.END_TIME_DIALOG_ID);
			}
		});
	}
	
	protected Dialog onCreateDialog(int id)
	{
		switch(id)
		{
		case START_DATE_DIALOG_ID:
			return new DatePickerDialog(this,start_set,syear,smonth,sday);

		case END_DATE_DIALOG_ID:
			return new DatePickerDialog(this,end_set,eyear,emonth,eday);
		case START_TIME_DIALOG_ID:
			return new TimePickerDialog(this,start_tset,shr,smin,false);
		case END_TIME_DIALOG_ID:
			return new TimePickerDialog(this,end_tset,ehr,emin,false);
			
		}
		return null;
	}
	
	private DatePickerDialog.OnDateSetListener start_set=new DatePickerDialog.OnDateSetListener()
	{
		public void onDateSet(DatePicker view,int year,int month,int day)
		{
			syear=year;
			smonth=month;
			sday=day;
			sdate=syear+"-"+smonth+"-"+sday;
			updateDisplay(0);
		}
	};
	private DatePickerDialog.OnDateSetListener end_set=new DatePickerDialog.OnDateSetListener()
	{
		public void onDateSet(DatePicker view,int year,int month,int day)
		{
			eyear=year;
			emonth=month;
			eday=day;
			edate=eyear+"-"+emonth+"-"+eday;
			updateDisplay(1);
		}
	};
	
	private TimePickerDialog.OnTimeSetListener start_tset=new TimePickerDialog.OnTimeSetListener()
	{
		public void onTimeSet(TimePicker view,int hr,int min)
		{
			shr=hr;
			smin=min;
			stime=" "+shr+":"+smin;
			updateDisplay(0);
		}
	};
	
	private TimePickerDialog.OnTimeSetListener end_tset=new TimePickerDialog.OnTimeSetListener()
	{
		public void onTimeSet(TimePicker view,int hr,int min)
		{
			ehr=hr;
			emin=min;
			etime=" "+ehr+":"+emin;
			updateDisplay(1);
		}
	};

	protected void updateDisplay(int mode) {
		// TODO Auto-generated method stub
		
		if(mode==0)
		{
			tv_sdt.setText(sdate+stime);tv_sdt.invalidate();
		}
		else if(mode==1)
		{
			tv_edt.setText(edate+etime);tv_edt.invalidate();
		}
		
	}
	
	public String[] getKeywords()
	{
		String s=et_keywords.getText().toString();
		String[] re=s.split(" ");
		for(int i=0;i<re.length;i++)
			re[i].trim();
		return re;
	}

}
