package cmput301f17t09.goalsandhabits.Main_Habits;


import android.content.Context;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import java.util.Date;

import cmput301f17t09.goalsandhabits.R;


public class HabitHistoryActivity extends AppCompatActivity {

    private Habit habit;
    Context context;

    private HabitEventArrayAdapter habitEventArrayAdapter;
    private ListView habitEventsList;

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

        Bundle extras = getIntent().getExtras();
        if (extras!=null){
            if (extras.containsKey(MainActivity.EXTRA_HABIT_SERIAL)){
                habit = (Habit) extras.getSerializable(MainActivity.EXTRA_HABIT_SERIAL);
            }
        }
        if (habit==null) finish();

        context = this;

        habitEventsList = (ListView) findViewById(R.id.habitEventList);
        habitEventArrayAdapter = new HabitEventArrayAdapter(this, habit.getEvents());
        habitEventsList.setAdapter(habitEventArrayAdapter);

        Toolbar toolbar = (Toolbar) findViewById(R.id.actionbar);
        toolbar.setTitle("Habit History of " + habit.getTitle());
        toolbar.setNavigationIcon(R.drawable.ic_close_button);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.addHabitEventButton);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, NewHabitEventActivity.class);
                startActivityForResult(intent, MainActivity.REQUEST_CODE_NEW_HABIT_EVENT);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if (resultCode == RESULT_OK){
            switch (requestCode){
                case MainActivity.REQUEST_CODE_NEW_HABIT_EVENT:{
                    Date date;
                    if (!data.hasExtra(MainActivity.EXTRA_HABIT_STARTDATE)) return;
                    date = (Date) data.getSerializableExtra(MainActivity.EXTRA_HABIT_STARTDATE);
                    HabitEvent habitEvent = new HabitEvent(date);
                    if (data.hasExtra(MainActivity.EXTRA_HABIT_NAME)){
                        habitEvent.setComment(data.getStringExtra(MainActivity.EXTRA_HABIT_NAME));
                    }
                    habit.addHabitEvent(habitEvent);
                    habitEventArrayAdapter.notifyDataSetChanged();
                }
            }
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home: {
                finish();
                return true;
            }
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void finish() {
        Intent data = new Intent();
        data.putExtra(MainActivity.EXTRA_HABIT_SERIAL, habit);
        setResult(RESULT_OK, data);
        super.finish();
    }
}

@@ -46,16 +46,51 @@
 bits;%0A%0A%0A
+import android.app.DialogFragment;%0A
 import a
@@ -320,16 +320,42 @@
 oolbar;%0A
+import android.view.Menu;%0A
 import a
@@ -435,16 +435,44 @@
 tView;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -589,16 +589,77 @@
 ctivity 
+implements EditHabitEventDialog.EditHabitEventDialogListener 
 %7B%0A%0A    p
@@ -797,16 +797,112 @@
 tsList;%0A
+    private HabitEvent habitEvent;%0A    protected static final int EVENT_DELETED_RESULT_CODE = 4;
 %0A    @Ov
@@ -1112,24 +1112,91 @@
 etExtras();%0A
+        habitEvent = (HabitEvent) extras.getSerializable(%22EVENT%22);%0A
         if (
@@ -1669,24 +1669,25 @@
 yAdapter);%0A%0A
+%0A
         Tool
@@ -2334,24 +2334,826 @@
   %7D);%0A    %7D%0A
+    public boolean onCreateOptionsMenu(Menu menu)%7B%0A        getMenuInflater().inflate(R.menu.menu_view_habit_events, menu);%0A        return true;%0A    %7D%0A%0A    /**%0A     * Handles the buttons in the action bar%0A     * @param item%0A     * @return%0A     */%0A    @Override%0A    public boolean onOptionsItemSelected(MenuItem item) %7B%0A        switch (item.getItemId()) %7B%0A            case android.R.id.home: %7B%0A                finish();%0A                return true;%0A            %7D%0A            case R.id.editButton:%7B%0A                showEditDialog();%0A                //finish();%0A                return true;%0A            %7D%0A            case R.id.deleteButton:%7B%0A                finish();%0A                return true;%0A            %7D%0A%0A            default:%0A                return super.onOptionsItemSelected(item);%0A        %7D%0A    %7D
 %0A    @Overri
@@ -4017,320 +4017,226 @@
   %7D%0A
-%0A    @Override%0A    public boolean onOptionsItemSelected(MenuItem item) %7B%0A        switch (item.getItemId()) %7B%0A            case android.R.id.home: %7B%0A                finish();%0A                return true;%0A            %7D%0A            default:%0A                return super.onOptionsItemSelected(item);%0A        %7D
+    public void showEditDialog() %7B%0A      DialogFragment dialog = EditHabitDialog.newInstance(habitEvent.getComment(), habitEvent.getPhotoPath());%0A      dialog.show(getFragmentManager(), %22EditHabitEventDialog%22);
 %0A    %7D%0A%0A
     
@@ -4227,24 +4227,25 @@
 g%22);%0A    %7D%0A%0A
+%0A
     @Overrid
@@ -4437,11 +4437,225 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public void onDialogPositiveClick(DialogFragment dialog, String s, String newComment, Date newDate) %7B%0A%0A    %7D%0A%0A    @Override%0A    public void onDialogNegative%C6%92Click(DialogFragment dialog) %7B%0A%0A    %7D
 %0A%7D%0A

Update HabitHistoryActivity.java