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 CheckoutDialogFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

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

        if (!checkinCalendar.before(checkoutCalendar)) {
            // update the checkout date to one more than the
            // checkin date if the checkin is not before the
            // checkout
            checkoutCalendar = (Calendar) checkinCalendar.clone();
            checkoutCalendar.add(Calendar.DAY_OF_MONTH, 1);
            System.out.println(checkoutCalendar);
        }

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

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

            Reservation res = (Reservation) getActivity();
            Calendar checkoutCalendar = res.getCheckoutCalendar();

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

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