package com.k3nx.taskit;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import java.util.Date;


public class TaskListActivity extends ActionBarActivity {

    private static final String TAG = "TaskListActivity";
    private static final int EDIT_TASK_REQUEST = 10;

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

        Task[] items = new Task[3];
        items[0] = new Task();
        items[0].setName("Task 1");
        items[0].setDueDate(new Date());
        items[1] = new Task();
        items[1].setName("Task 2");
        items[1].setDone(true);
        items[2] = new Task();
        items[2].setName("Task 3");

        ListView listView = (ListView)findViewById(R.id.task_list);
        listView.setAdapter(new TaskAdapter(items));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d(TAG, "Position Clicked is " + position);
                Task task = (Task)parent.getAdapter().getItem(position);
                Intent i = new Intent(TaskListActivity.this, TaskActivity.class);
                i.putExtra(TaskActivity.EXTRA, task);
                //startActivity(i);
                startActivityForResult(i, EDIT_TASK_REQUEST);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == EDIT_TASK_REQUEST) {
            if (resultCode == RESULT_OK) {
                Task task = (Task) data.getSerializableExtra(TaskActivity.EXTRA);
                Log.d(TAG, task.getName());
            }
        }
    }

    private class TaskAdapter extends ArrayAdapter<Task> {
        TaskAdapter(Task[] tasks) {
            // Using a custom layout requires the TextView id
            super(TaskListActivity.this, R.layout.task_list_row, R.id.task_item_name, tasks);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = super.getView(position, convertView, parent);
            Task task = getItem(position);
            TextView taskName = (TextView)convertView.findViewById(R.id.task_item_name);
            taskName.setText(task.getName());

            CheckBox doneBox = (CheckBox)convertView.findViewById(R.id.task_item_done);
            doneBox.setChecked(task.isDone());

            return convertView;
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_task_list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

@@ -627,16 +627,116 @@
  = 10;%0A%0A
+    private Task%5B%5D mTasks;%0A    private int mLastPositionClicked;%0A    private TaskAdapter mAdapter;%0A%0A
     @Ove
@@ -908,19 +908,13 @@
     
+m
 Task
-%5B%5D item
 s = 
@@ -930,28 +930,29 @@
 3%5D;%0A        
-item
+mTask
 s%5B0%5D = new T
@@ -958,36 +958,37 @@
 Task();%0A        
-item
+mTask
 s%5B0%5D.setName(%22Ta
@@ -1003,20 +1003,21 @@
         
-item
+mTask
 s%5B0%5D.set
@@ -1041,28 +1041,29 @@
 ));%0A        
-item
+mTask
 s%5B1%5D = new T
@@ -1069,36 +1069,37 @@
 Task();%0A        
-item
+mTask
 s%5B1%5D.setName(%22Ta
@@ -1114,20 +1114,21 @@
         
-item
+mTask
 s%5B1%5D.set
@@ -1143,28 +1143,29 @@
 e);%0A        
-item
+mTask
 s%5B2%5D = new T
@@ -1179,20 +1179,21 @@
         
-item
+mTask
 s%5B2%5D.set
@@ -1289,28 +1289,19 @@
     
-listView.set
+m
 Adapter
-(
+ = 
 new 
@@ -1316,14 +1316,53 @@
 ter(
-items)
+mTasks);%0A        listView.setAdapter(mAdapter
 );%0A 
@@ -1553,24 +1553,73 @@
  long id) %7B%0A
+                mLastPositionClicked = position;%0A
             
@@ -2344,16 +2344,118 @@
 EXTRA);%0A
+                mTasks%5BmLastPositionClicked%5D = task;%0A                mAdapter.notifyDataSetChanged();%0A
         

Update Task Adapter