package com.example.countdown;

import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.widget.DatePicker;

public class EventDateDialogFragment extends DialogFragment {
    private EventDateListener listener;
    
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            listener = (EventDateListener) activity;
        }
        catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement EventDateListener");
        }
    }
    
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Calendar calendar = Calendar.getInstance();
        Dialog dialog = new DatePickerDialog(getActivity(),
                new DateListener(),
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH));
        return dialog;
    }

    class DateListener implements DatePickerDialog.OnDateSetListener {
        public void onDateSet(DatePicker view, int year,
                int monthOfYear, int dayOfMonth) {
            Calendar calendar = new GregorianCalendar(year, monthOfYear,
                    dayOfMonth);
            listener.onEventDateChosen(calendar);
        }
    }
}