package com.greenmoon55.easytodo;

import android.app.ListActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.*;
import android.widget.ListView;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;

import java.util.List;


public class TodoListActivity extends ListActivity {
    private static final int ACTIVITY_CREATE = 0;
    private static final int ACTIVITY_EDIT = 1;
    private static final int ACTIVITY_AUTH = 2;

    private static final int DELETE_ID = Menu.FIRST + 1;

    private volatile List<Todo> todos;

    private class RemoteDataTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            todos = AVService.findTodos();
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            TodoAdapter adapter = new TodoAdapter(TodoListActivity.this, todos);
            setListAdapter(adapter);
            registerForContextMenu(getListView());
            TextView empty = (TextView) findViewById(android.R.id.empty);
            if (todos != null && !todos.isEmpty()) {
                empty.setVisibility(View.INVISIBLE);
            } else {
                empty.setVisibility(View.VISIBLE);
            }
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (AVUser.getCurrentUser() == null) {
            Intent intent = new Intent(TodoListActivity.this, AuthActivity.class);
            startActivityForResult(intent, ACTIVITY_AUTH);
        }
        setContentView(R.layout.activity_todo_list);
        new RemoteDataTask().execute();
    }


    @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_todo_list, menu);
        return true;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, DELETE_ID, 0, "Delete Todo");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        final Todo todo = todos.get(info.position);
        switch (item.getItemId()) {
            case DELETE_ID:
                new RemoteDataTask() {
                    @Override
                    protected Void doInBackground(Void... params) {
                        try {
                            todo.delete();
                        } catch (AVException e) {
                            e.printStackTrace();
                        }
                        super.doInBackground();
                        return null;
                    }
                }.execute();
                return true;
            default:
                return super.onContextItemSelected(item);
        }
    }

    @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();

        if (id == R.id.create_todo) {
            createTodo();
            return true;
        }

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

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, CreateTodo.class);
        i.putExtra("content", todos.get(position).getString("content"));
        i.putExtra("objectId", todos.get(position).getObjectId());
        startActivityForResult(i, ACTIVITY_EDIT);
    }

    private void createTodo() {
        Intent i = new Intent(this, CreateTodo.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }
}

@@ -411,16 +411,45 @@
 extView;
+%0Aimport android.widget.Toast;
 %0A%0Aimport
@@ -4309,24 +4309,617 @@
 IT);%0A    %7D%0A%0A
+    @Override%0A    protected void onActivityResult(int requestCode, int resultCode, Intent intent) %7B%0A        super.onActivityResult(requestCode, resultCode, intent);%0A        if (intent == null) %7B%0A            return;%0A        %7D%0A        // %E6%9A%82%E6%97%B6%E6%8F%90%E7%A4%BA%E4%BF%A1%E6%81%AF%0A        boolean success = intent.getBooleanExtra(%22success%22, true);%0A        Toast toast = null;%0A        if (success) %7B%0A            // %E9%87%8D%E6%96%B0%E6%9F%A5%E8%AF%A2%EF%BC%8C%E5%88%B7%E6%96%B0ListView%0A            new RemoteDataTask().execute();%0A        %7D else %7B%0A            toast = Toast.makeText(getApplicationContext(), %22Save failed!%22, Toast.LENGTH_SHORT);%0A            toast.show();%0A        %7D%0A    %7D%0A%0A
     private 

Refresh list on create/update todo