package org.jboss.aerogear.unifiedpush.quickstart.activities;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;
import org.jboss.aerogear.unifiedpush.quickstart.Constants;
import org.jboss.aerogear.unifiedpush.quickstart.R;
import org.jboss.aerogear.unifiedpush.quickstart.model.Contact;
import org.jboss.aerogear.unifiedpush.quickstart.model.User;
import org.jboss.aerogear.unifiedpush.quickstart.util.WebClient;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import static android.widget.Toast.LENGTH_SHORT;

public class ContactActivity extends ActionBarActivity {

    private EditText firstName;
    private EditText lastName;
    private EditText phone;
    private EditText email;
    private EditText birthDate;

    private final Calendar dateSelected = Calendar.getInstance();
    private final SimpleDateFormat displayDateFormat = new SimpleDateFormat("MM/dd/yyyy");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        firstName = (EditText) findViewById(R.id.first_name);
        lastName = (EditText) findViewById(R.id.last_name);
        phone = (EditText) findViewById(R.id.phone);
        email = (EditText) findViewById(R.id.email);
        birthDate = (EditText) findViewById(R.id.birth_date);

        ImageView calendar = (ImageView) findViewById(R.id.calendar);
        calendar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogFragment newFragment = new DatePickerFragment();
                newFragment.show(getSupportFragmentManager(), "datePicker");
            }
        });


        Button save = (Button) findViewById(R.id.save);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Contact contact = retriveContactFromForm();
                addContact(contact);
            }
        });

    }

    public boolean onOptionsItemSelected(MenuItem item) {
        finish();
        return super.onOptionsItemSelected(item);
    }

    private Contact retriveContactFromForm() {
        Contact contact = new Contact();
        contact.setFirstName(firstName.getText().toString());
        contact.setLastName(lastName.getText().toString());
        contact.setPhoneNumber(phone.getText().toString());
        contact.setEmail(email.getText().toString());
        contact.setBirthDate(birthDate.getText().toString());
        return contact;
    }

    private void addContact(final Contact contact) {
        new AsyncTask<Void, Void, Boolean>() {
            @Override
            protected Boolean doInBackground(Void... voids) {
                return new WebClient(Constants.URL_CONTACTS).newContact(contact);
            }

            @Override
            protected void onPostExecute(Boolean registered) {
                if (registered) {
                    Toast.makeText(getApplicationContext(), getString(R.string.contact_added), LENGTH_SHORT).show();
                    finish();
                } else {
                    Toast.makeText(getApplicationContext(), getString(R.string.an_error_occurred), LENGTH_SHORT).show();
                }
            }
        }.execute();
    }

    public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            int year = dateSelected.get(Calendar.YEAR);
            int month = dateSelected.get(Calendar.MONTH);
            int day = dateSelected.get(Calendar.DAY_OF_MONTH);

            return new DatePickerDialog(getActivity(), this, year, month, day);
        }

        public void onDateSet(DatePicker view, int year, int month, int day) {
            dateSelected.set(Calendar.DAY_OF_MONTH, day);
            dateSelected.set(Calendar.MONTH, month);
            dateSelected.set(Calendar.YEAR, year);
            birthDate.setText(displayDateFormat.format(dateSelected.getTime()));
        }
    }

}

@@ -531,69 +531,8 @@
 ct;%0A
-import org.jboss.aerogear.unifiedpush.quickstart.model.User;%0A
 impo
@@ -592,16 +592,16 @@
 Client;%0A
+
 %0Aimport 
@@ -628,16 +628,41 @@
 Format;%0A
+import java.util.Arrays;%0A
 import j
@@ -679,16 +679,39 @@
 alendar;
+%0Aimport java.util.List;
 %0A%0Aimport
@@ -1066,23 +1066,16 @@
 Format d
-isplayD
 ateForma
@@ -1104,18 +1104,18 @@
 at(%22
-MM/dd/yyyy
+yyyy-MM-dd
 %22);%0A
@@ -2811,37 +2811,48 @@
 ate(
-birthDate.getText().toString(
+dateFormat.format(dateSelected.getTime()
 ));%0A
@@ -4333,24 +4333,24 @@
 EAR, year);%0A
+
             
@@ -4372,15 +4372,8 @@
 xt(d
-isplayD
 ateF

Fix contact date format