package com.example.bb;

import java.util.Calendar;

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

public class CheckinDialogFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        Reservation res = (Reservation) getActivity();
        Calendar checkinCalendar = res.getCheckinCalendar();

        Dialog dialog = new DatePickerDialog(res,
                new CheckinListener(),
                checkinCalendar.get(Calendar.YEAR),
                checkinCalendar.get(Calendar.MONTH),
                checkinCalendar.get(Calendar.DAY_OF_MONTH));
        return dialog;
    }

    class CheckinListener implements
            DatePickerDialog.OnDateSetListener {
        @Override
        public void onDateSet(DatePicker view, int year,
                int monthOfYear, int dayOfMonth) {

            Reservation res = (Reservation) getActivity();
            Calendar checkinCalendar = res.getCheckinCalendar();

            // update the calendar field to the check in date
            // to maintain the check in value.
            checkinCalendar.set(Calendar.YEAR, year);
            checkinCalendar.set(Calendar.MONTH, monthOfYear);
            checkinCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

            // modify the TextView to display the checkin date
            TextView tv = (TextView) res
                    .findViewById(R.id.labelCheckin);
            tv.setText(R.string.checkin);
            tv.append(" " + (monthOfYear + 1) + "/" + dayOfMonth + "/"
                    + year);
        }
    }
}