package de.fau.amos.virtualledger.android.views.transactionOverview;

import android.app.DatePickerDialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.logging.Logger;

import de.fau.amos.virtualledger.R;

public class SpecifyDateDialog extends DialogFragment {

    private Calendar startDate = new GregorianCalendar();
    private Calendar endDate = new GregorianCalendar();


    DatePickerDialog.OnDateSetListener startDateListener = new
            DatePickerDialog.OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear,
                                      int dayOfMonth) {
                    // TODO Auto-generated method stub
                    startDate.set(Calendar.YEAR, year);
                    startDate.set(Calendar.MONTH, monthOfYear);
                    startDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    logger().info("Startdate changed to: " + startDate);
                }

                private Logger logger() {
                    return Logger.getLogger(this.getClass().getCanonicalName() + "{" + this.hashCode() + "}");
                }

            };

    DatePickerDialog.OnDateSetListener endDateListener = new
            DatePickerDialog.OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear,
                                      int dayOfMonth) {
                    // TODO Auto-generated method stub
                    endDate.set(Calendar.YEAR, year);
                    endDate.set(Calendar.MONTH, monthOfYear);
                    endDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                    logger().info("Startdate changed to: " + endDate);
                }

                private Logger logger() {
                    return Logger.getLogger(this.getClass().getCanonicalName() + "{" + this.hashCode() + "}");
                }

            };


    static SpecifyDateDialog newInstance() {
        return new SpecifyDateDialog();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.date_range_chooser_dialog, container, false);
        Button startDateButton = (Button) v.findViewById(R.id.start_date_button);
        startDateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DatePickerDialog(v.getContext(), startDateListener, startDate
                        .get(Calendar.YEAR), startDate.get(Calendar.MONTH),
                        startDate.get(Calendar.DAY_OF_MONTH)).show();
            }
        });

        final Button endDateButton = (Button) v.findViewById(R.id.end_date_button);
        endDateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DatePickerDialog(v.getContext(), endDateListener, endDate
                        .get(Calendar.YEAR), endDate.get(Calendar.MONTH),
                        endDate.get(Calendar.DAY_OF_MONTH)).show();
            }
        });
        return v;
    }
}
@@ -161,16 +161,59 @@
 Bundle;%0A
+import android.support.annotation.NonNull;%0A
 import a
@@ -240,16 +240,16 @@
 flater;%0A
-
 import a
@@ -667,17 +667,86 @@
 ndar();%0A
+    private Button startDateButton;%0A    private Button endDateButton;
 %0A
-
 %0A    Dat
@@ -1254,32 +1254,128 @@
 H, dayOfMonth);%0A
+                    startDateButton.setText(generateButtonText(year, monthOfYear, dayOfMonth));%0A
                 
@@ -1638,16 +1638,171 @@
     %7D;%0A%0A
+    @NonNull%0A    private String generateButtonText(int year, int monthOfYear, int dayOfMonth) %7B%0A        return dayOfMonth+%22.%22+monthOfYear+%22.%22+year;%0A    %7D%0A%0A
     Date
@@ -2301,32 +2301,126 @@
 H, dayOfMonth);%0A
+                    endDateButton.setText(generateButtonText(year, monthOfYear, dayOfMonth));%0A
                 
@@ -3137,23 +3137,16 @@
         
-Button 
 startDat
@@ -3595,24 +3595,24 @@
    %7D);%0A%0A
+
         
 final Bu
@@ -3607,21 +3607,8 @@
     
-final Button 
 endD

set chosen date to button in select date dialog of filter in transaction overview