package com.mokelab.sample.contentprovider.provider.todo;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;

/**
 * Content Provider for Todo
 */
public class TodoProvider extends ContentProvider {

    private static final String AUTHORITY = "com.mokelab.todo.provider";

    // region URI type
    private static final int URITYPE_TODO_LIST = 1;
    private static final int URITYPE_TODO = 2;
    // endregion

    private static final UriMatcher sUriMatcher;

    static {
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        sUriMatcher.addURI(AUTHORITY, "users/*/todos", URITYPE_TODO_LIST);
        sUriMatcher.addURI(AUTHORITY, "users/*/todos/#", URITYPE_TODO);
    }

    private TodoDB mTodoDB;
    private TodoDAO mTodoDAO;

    @Override
    public boolean onCreate() {
        mTodoDB = new TodoDB(getContext());
        mTodoDAO = new TodoDAO(mTodoDB);
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        return null;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues contentValues) {
        switch (sUriMatcher.match(uri)) {
        case URITYPE_TODO_LIST:
            return insertLocal(uri, contentValues);
        }
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues contentValues, String selection, String[] selectionArgs) {
        return 0;
    }

    private Uri insertLocal(Uri uri, ContentValues contentValues) {
        // Uri = content://com.mokelab.todo.provider/users/{userId}/todos
        // pathSegments[0] = users
        // pathSegments[1] = {userId}
        // pathSegments[2] = todos
        String userId = uri.getPathSegments().get(1);
        String todo = contentValues.getAsString(TodoColumns.TODO);

        long id = mTodoDAO.insert(userId, "", System.currentTimeMillis(), 0, todo);
        if (id == -1) { return null; }

        return Uri.parse("content://" + AUTHORITY + "/users/" + userId + "/todos/" + id);
    }
}

@@ -1146,24 +1146,196 @@
 ortOrder) %7B%0A
+        switch (sUriMatcher.match(uri)) %7B%0A        case URITYPE_TODO_LIST:%0A            return queryTodoList(uri, projection, selection, selectionArgs, sortOrder);%0A        %7D%0A
         retu
@@ -2517,11 +2517,499 @@
 );%0A    %7D
+%0A%0A    private Cursor queryTodoList(Uri uri, String%5B%5D projection, String selection, String%5B%5D selectionArgs, String sortOrder) %7B%0A        // Uri = content://com.mokelab.todo.provider/users/%7BuserId%7D/todos%0A        // pathSegments%5B0%5D = users%0A        // pathSegments%5B1%5D = %7BuserId%7D%0A        // pathSegments%5B2%5D = todos%0A        String userId = uri.getPathSegments().get(1);%0A        Cursor cursor = mTodoDAO.query(userId, projection, selection, selectionArgs, sortOrder);%0A        return cursor;%0A    %7D
 %0A%7D%0A

Implement query method