package com.box.androidsdk.share.fragments;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;

import com.box.androidsdk.share.R;

import java.util.Calendar;
import java.util.Date;


public class DatePickerFragment extends PositiveNegativeDialogFragment
        implements DatePickerDialog.OnDateSetListener, DialogInterface.OnDismissListener {

    DatePickerDialog mDialog;
    private DatePickerDialog.OnDateSetListener mOnDateSetListener;
    private DialogInterface.OnDismissListener onDismissListener;

    private static final String EXTRA_START_DATE = "extraStartDate";
    private final String EXTRA_KEY_YEAR = "extraYear";
    private final String EXTRA_KEY_MONTH = "extraMonth";
    private final String EXTRA_KEY_DAY = "extraDay";


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        setRetainInstance(true);

        mButtonClicked = false;

        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        if (getArguments() != null){
            Date date = (Date)getArguments().getSerializable(EXTRA_START_DATE);
            if (date != null) {
                c.setTime(date);
            }
        }

        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        if (savedInstanceState != null){
            year = savedInstanceState.getInt(EXTRA_KEY_YEAR);
            month = savedInstanceState.getInt(EXTRA_KEY_MONTH);
            day = savedInstanceState.getInt(EXTRA_KEY_DAY);
        }
        mDialog = new DatePickerDialog(getActivity(), R.style.ShareDialogTheme, this, year, month, day);
        // Create a new instance of DatePickerDialog and return it
        return mDialog;
    }

    public void setOnDateSetListener(DatePickerDialog.OnDateSetListener listener) {
        mOnDateSetListener = listener;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt(EXTRA_KEY_YEAR,  mDialog.getDatePicker().getYear());
        outState.putInt(EXTRA_KEY_MONTH,  mDialog.getDatePicker().getMonth());
        outState.putInt(EXTRA_KEY_DAY,  mDialog.getDatePicker().getDayOfMonth());
        super.onSaveInstanceState(outState);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        mButtonClicked = true;
        if (mOnDateSetListener != null){
            mOnDateSetListener.onDateSet(view, year, month, day);
        }
    }


    @Override
    public void onDestroyView()
    {
        Dialog dialog = getDialog();

        // Work around bug: http://code.google.com/p/android/issues/detail?id=17423
        if ((dialog != null) && getRetainInstance())
            dialog.setDismissMessage(null);

        super.onDestroyView();
    }

    public static final DatePickerFragment createFragment(final Date date, DatePickerDialog.OnDateSetListener dateSetListener, OnPositiveOrNegativeButtonClickedListener listener){
        DatePickerFragment fragment = new DatePickerFragment();
        Bundle b = new Bundle();
        b.putSerializable(EXTRA_START_DATE, date);
        fragment.setArguments(b);
        fragment.setOnDateSetListener(dateSetListener);
        fragment.setOnPositiveOrNegativeButtonClickedListener(listener);
        return fragment;
    }
}
@@ -42,37 +42,8 @@
 s;%0A%0A
-import android.app.Activity;%0A
 impo
@@ -67,32 +67,32 @@
 tePickerDialog;%0A
+
 import android.a
@@ -172,54 +172,8 @@
 le;%0A
-import android.support.v4.app.DialogFragment;%0A
 impo
@@ -997,16 +997,128 @@
 false;%0A%0A
+        final Calendar minDateCalendar = Calendar.getInstance();%0A        minDateCalendar.add(Calendar.DATE, 1);%0A
         
@@ -1218,32 +1218,33 @@
 .getInstance();%0A
+%0A
         if (getA
@@ -1429,25 +1429,230 @@
 %7D%0A        %7D%0A
+        // we cannot have a minimum date that is earlier than the set date.%0A        if (c.getTimeInMillis() %3C minDateCalendar.getTimeInMillis())%7B%0A            c.setTime(minDateCalendar.getTime());%0A        %7D
 %0A
-
         int 
@@ -2001,32 +2001,32 @@
 DAY);%0A        %7D%0A
-
         mDialog 
@@ -2106,32 +2106,111 @@
 r, month, day);%0A
+        mDialog.getDatePicker().setMinDate(minDateCalendar.getTimeInMillis());%0A
         // Creat

Add minimum date logic.