package com.example.db01;

import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.*;

public class AddAlarm extends Activity
{
	
	private EditText msgEdit;
	private CheckBox soundCb;
	private DatePicker datePicker;
	private TimePicker timePicker;
	private TextView fromdateText;
	private TextView todateText;
	private TextView attimeText;
	
	private ViewSwitcher vs;
	private RadioGroup rg;
	private RelativeLayout rl3;
	private RelativeLayout rl4;
	
	private Spinner spinner1;
	private Spinner spinner2;
	private Spinner spinner3;
	
	private EditText minsEdit;
	private EditText hoursEdit;
	private EditText daysEdit;
	private EditText monthsEdit;
	private EditText yearsEdit;
	
	private DbAdapter dbHelper;
	
	private static final SimpleDateFormat sdf = new SimpleDateFormat();

	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
        setTitle("New Reminder");
        setContentView(R.layout.add);
        findViews();
        
        dbHelper = new DbAdapter(this);
	 	dbHelper.open();
        
	}
	
	private void findViews()
	{
		msgEdit = (EditText) findViewById(R.id.msg_et);
		soundCb = (CheckBox) findViewById(R.id.sound_cb);
		datePicker = (DatePicker) findViewById(R.id.datePicker);
		timePicker = (TimePicker) findViewById(R.id.timePicker);
		fromdateText = (TextView) findViewById(R.id.fromdate_tv);
		todateText = (TextView) findViewById(R.id.todate_tv);
		attimeText = (TextView) findViewById(R.id.attime_tv);
        vs = (ViewSwitcher) findViewById(R.id.view_switcher);
        rg = (RadioGroup) findViewById(R.id.radioGroup);
        rl3 = (RelativeLayout) findViewById(R.id.relativeLayout3);
        rl4 = (RelativeLayout) findViewById(R.id.relativeLayout4);
        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner2 = (Spinner) findViewById(R.id.spinner2);
        spinner3 = (Spinner) findViewById(R.id.spinner3);
        
        minsEdit = (EditText) findViewById(R.id.mins_et);
        hoursEdit = (EditText) findViewById(R.id.hours_et);
        daysEdit = (EditText) findViewById(R.id.days_et);
        monthsEdit = (EditText) findViewById(R.id.months_et);
        yearsEdit = (EditText) findViewById(R.id.years_et);
	}
	
	private boolean validate()
	{
		if (TextUtils.isEmpty(msgEdit.getText()))
		{
			msgEdit.requestFocus();
			Toast.makeText(this, "Enter a message", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (vs.getDisplayedChild() == 1) 
		{
				if (TextUtils.isEmpty(fromdateText.getText()))
				{
					Toast.makeText(this, "Specify from date", Toast.LENGTH_SHORT).show();
					return false;
				}
				if (TextUtils.isEmpty(todateText.getText()))
				{
					Toast.makeText(this, "Specify to date", Toast.LENGTH_SHORT).show();
					return false;
				}
			
				try
				{
					if (sdf.parse(fromdateText.getText().toString()).after(sdf.parse(todateText.getText().toString())))
					{
					Toast.makeText(this, "From date is after To date!", Toast.LENGTH_SHORT).show();
					return false;
					}
				} 
				
				catch (ParseException e) {}			
				if (TextUtils.isEmpty(attimeText.getText())) 
				{
					Toast.makeText(this, "Specify time", Toast.LENGTH_SHORT).show();
					return false;
				}			
		}
		return true;
	}
	
	
	public void create(View v)
	{
		if (!validate()) return;
		
		Alarm alarm = new Alarm();
		AlarmTime alarmTime = new AlarmTime();
		long alarmId = 0;
		
		switch(vs.getDisplayedChild()) 
		{
			case 0: //one time
				long now = System.currentTimeMillis();
			 	alarm.setName( msgEdit.getText().toString() );
			 	alarm.setFromDate( Utils.getDateStr(datePicker.getYear(), datePicker.getMonth()+1, datePicker.getDayOfMonth()) );
			 	alarm.setToDate("Null");
			 	alarm.setRule("Null");
			 	alarm.setInterval("Null");
			 	alarm.setSound(soundCb.isChecked());
			 	alarmId = dbHelper.addAlarm(alarm);
				alarmTime.setAt(Utils.getTimeStr(timePicker.getCurrentHour(), timePicker.getCurrentMinute()));
				alarmTime.setAlarmId(alarmId);
				dbHelper.addAlarmTime(alarmTime);
				
				break;
				
			case 1: //repeating
				String pipo = "000";	
				break;				
		}
		
		Intent service = new Intent(this, AlarmService.class);
		service.putExtra(AlarmMsg.COL_ALARMID, String.valueOf(alarmId));
		service.setAction(AlarmService.POPULATE);
		startService(service);

		finish();
	}
	

	
	

}
