package com.alexstyl.specialdates.datedetails;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.MenuItem;

import com.alexstyl.specialdates.BuildConfig;
import com.alexstyl.specialdates.ErrorTracker;
import com.alexstyl.specialdates.Optional;
import com.alexstyl.specialdates.R;
import com.alexstyl.specialdates.analytics.AnalyticsProvider;
import com.alexstyl.specialdates.analytics.Screen;
import com.alexstyl.specialdates.date.Date;
import com.alexstyl.specialdates.date.DateFormatUtils;
import com.alexstyl.specialdates.support.AskForSupport;
import com.alexstyl.specialdates.ui.activity.MainActivity;
import com.alexstyl.specialdates.ui.base.ThemedActivity;
import com.alexstyl.specialdates.ui.widget.MementoToolbar;
import com.novoda.notils.caster.Views;

public class DateDetailsActivity extends ThemedActivity {

    private static final String EXTRA_DAY = BuildConfig.APPLICATION_ID + ".dayOfMonth";
    private static final String EXTRA_MONTH = BuildConfig.APPLICATION_ID + ".month";
    private static final String EXTRA_YEAR = BuildConfig.APPLICATION_ID + ".year";
    private static final String EXTRA_SOURCE = BuildConfig.APPLICATION_ID + ".source";
    /**
     * The activity was lauched because the user tapped on a notification
     */
    private static final int SOURCE_NOTIFICATION = 1;

    private Date displayingDate;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_details);

        AnalyticsProvider.getAnalytics(this).trackScreen(Screen.DATE_DETAILS);
        MementoToolbar toolbar = Views.findById(this, R.id.memento_toolbar);
        toolbar.displayAsUp();
        setSupportActionBar(toolbar);

        Optional<Date> receivedDate = getCalendarFromIntent(getIntent());
        if (!receivedDate.isPresent()) {
            finish();
            ErrorTracker.track(new NullPointerException("Tried to open DateDetails with no date in the intent:[" + getIntent() + "]"));
            return;
        }
        this.displayingDate = receivedDate.get();

        if (getIntent().hasExtra(EXTRA_SOURCE)) {
            int intExtra = getIntent().getIntExtra(EXTRA_SOURCE, -1);
            if (intExtra == SOURCE_NOTIFICATION) {
                new AskForSupport(context()).requestForRatingSooner();
            }
        }

        if (savedInstanceState == null) {
            Fragment fragment = DateDetailsFragment.newInstance(displayingDate);

            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, fragment)
                    .commit();

        }

        String titleDate = DateFormatUtils.formatTimeStampString(
                DateDetailsActivity.this, displayingDate.toMillis(),
                true
        );
        setTitle(titleDate);
    }

    private Optional<Date> getCalendarFromIntent(Intent intent) {
        if (intent.hasExtra(EXTRA_DAY)) {
            Bundle extras = intent.getExtras();
            int year = extras.getInt(EXTRA_YEAR);
            int month = extras.getInt(EXTRA_MONTH);
            int dayOfMonth = extras.getInt(EXTRA_DAY);

            return new Optional<>(Date.on(dayOfMonth, month, year));
        }
        return Optional.absent();

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                Intent i = new Intent(DateDetailsActivity.this, MainActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
                finish();
                return true;

            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public static Intent getStartIntent(Context context, Date date) {
        Intent i = new Intent(context, DateDetailsActivity.class);
        i.putExtra(DateDetailsActivity.EXTRA_DAY, date.getDayOfMonth());
        i.putExtra(DateDetailsActivity.EXTRA_MONTH, date.getMonth());
        i.putExtra(DateDetailsActivity.EXTRA_YEAR, date.getYear());
        return i;
    }

    public static Intent getStartIntentFromExternal(Context context, Date date) {
        Intent startIntent = getStartIntent(context, date);
        startIntent.putExtra(EXTRA_SOURCE, SOURCE_NOTIFICATION);
        return startIntent;
    }

}

@@ -1407,42 +1407,8 @@
 1;%0A%0A
-    private Date displayingDate;%0A%0A
     
@@ -2113,13 +2113,13 @@
     
-this.
+Date 
 disp

Change field to local