package com.alexstyl.specialdates.addevent;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;

import com.alexstyl.specialdates.R;
import com.alexstyl.specialdates.addevent.ui.BirthdayDatePicker;
import com.alexstyl.specialdates.contact.Birthday;
import com.alexstyl.specialdates.date.DateParseException;
import com.alexstyl.specialdates.date.DayDate;
import com.alexstyl.specialdates.ui.base.MementoDialog;
import com.alexstyl.specialdates.util.DateParser;
import com.novoda.notils.caster.Views;
import com.novoda.notils.exception.DeveloperError;

public class BirthdayPickerDialog extends MementoDialog {

    public static final String TAG = "fm_tag:birthday";
    private static final String KEY_DATE = "key:birthday";
    private DateParser parser = new DateParser();

    private OnBirthdaySelectedListener listener;
    private BirthdayDatePicker datePicker;

    private Birthday initialBirthday;

    public static BirthdayPickerDialog createDialogFor(Birthday birthday, OnBirthdaySelectedListener listener) {
        Bundle args = new Bundle();
        args.putString(KEY_DATE, birthday.toString());
        BirthdayPickerDialog fragment = new BirthdayPickerDialog();
        fragment.setArguments(args);
        fragment.setOnBirthdaySetListener(listener);
        return fragment;
    }

    public static BirthdayPickerDialog createDialog(OnBirthdaySelectedListener listener) {
        BirthdayPickerDialog fragment = new BirthdayPickerDialog();
        fragment.setOnBirthdaySetListener(listener);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initialBirthday = extractBirthday(getArguments());
    }

    private Birthday extractBirthday(Bundle arguments) {
        if (arguments == null || !arguments.containsKey(KEY_DATE)) {
            DayDate today = DayDate.today();
            return Birthday.on(today);
        } else {
            String birthday = arguments.getString(KEY_DATE);
            try {
                DayDate parsedDate = parser.parse(birthday);
                return Birthday.on(parsedDate);
            } catch (DateParseException e) {
                throw new DeveloperError("Invalid birthday to display [" + birthday + "]");
            }
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View view = LayoutInflater.from(getThemedContext()).inflate(R.layout.dialog_birthday_picker, null, false);
        datePicker = Views.findById(view, R.id.dialog_birthday_picker);
        datePicker.setDisplayingDate(initialBirthday);

        return new AlertDialog.Builder(getActivity())
                .setTitle(R.string.birthday_picker_dialog_title)
                .setView(view)
                .setPositiveButton(R.string.birthday_picker_dialog_positive, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        listener.onBirthdaySet(datePicker.getDisplayingBirthday());
                    }
                })
                .create();
    }

    private void setOnBirthdaySetListener(OnBirthdaySelectedListener listener) {
        this.listener = listener;
    }

    public static void resetListenerToDialog(FragmentManager fragmentManager, OnBirthdaySelectedListener onBirthdaySelectedListener) {
        BirthdayPickerDialog dialog = (BirthdayPickerDialog) fragmentManager.findFragmentByTag(TAG);
        if (dialog != null) {
            dialog.setOnBirthdaySetListener(onBirthdaySelectedListener);
        }
    }

    public interface OnBirthdaySelectedListener {
        void onBirthdaySet(Birthday birthday);
    }
}

@@ -371,16 +371,106 @@
 .View;%0A%0A
+import com.alexstyl.specialdates.ErrorTracker;%0Aimport com.alexstyl.specialdates.Optional;%0A
 import c
@@ -866,59 +866,8 @@
 ews;
-%0Aimport com.novoda.notils.exception.DeveloperError;
 %0A%0Apu
@@ -1194,24 +1194,33 @@
 private 
+Optional%3C
 Birthday
  initial
@@ -1211,16 +1211,17 @@
 Birthday
+%3E
  initial
@@ -2069,24 +2069,33 @@
 private 
+Optional%3C
 Birthday
  extract
@@ -2086,16 +2086,17 @@
 Birthday
+%3E
  extract
@@ -2209,77 +2209,31 @@
     
-DayDate today = DayDate.today();%0A            return Birthday.on(today
+return Optional.absent(
 );%0A 
@@ -2325,18 +2325,131 @@
     
+return extractFrom(birthday);%0A        %7D%0A    %7D%0A%0A    private Optional%3CBirthday%3E extractFrom(String birthday) %7B%0A        
 try %7B%0A
-    
     
@@ -2513,27 +2513,38 @@
         
-    
 return 
+new Optional%3C%3E(
 Birthday
@@ -2554,30 +2554,27 @@
 (parsedDate)
+)
 ;%0A
-    
         %7D ca
@@ -2618,101 +2618,68 @@
     
-    throw new DeveloperError(%22Invalid birthday to display %5B%22 + birthday + %22%5D%22);%0A            %7D
+ErrorTracker.track(e);%0A            return Optional.absent();
 %0A   
@@ -2964,24 +2964,71 @@
 ay_picker);%0A
+        if (initialBirthday.isPresent()) %7B%0A    
         date
@@ -3067,18 +3067,34 @@
 Birthday
-);
+.get());%0A        %7D
 %0A%0A      

Use optional instead of null