/*
 * Copyright 2013-2015 The GDG Frisbee Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gdg.frisbee.android.provider;

import android.app.SearchManager;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.provider.BaseColumns;

import org.gdg.frisbee.android.activity.SearchActivity;
import org.gdg.frisbee.android.api.model.Chapter;
import org.gdg.frisbee.android.api.model.Directory;
import org.gdg.frisbee.android.app.App;
import org.gdg.frisbee.android.cache.ModelCache;

import timber.log.Timber;

public class GdgProvider extends ContentProvider {

    public static final String AUTHORITY = "org.gdg.frisbee.android.provider.GdgProvider";

    private static final String[] CHAPTER_COLUMNS = new String[]{BaseColumns._ID, SearchManager.SUGGEST_COLUMN_TEXT_1,
            SearchManager.SUGGEST_COLUMN_TEXT_2, SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
            SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID};

    private static final int SEARCH_SUGGEST = 0;

    private static UriMatcher uriMatcher;
    static {
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, SEARCH_SUGGEST);
    }

    private Directory mDirectory;

    @Override
    public boolean onCreate() {
        App.getInstance().getModelCache().getAsync("chapter_list_hub", false, new ModelCache.CacheListener() {
            @Override
            public void onGet(Object item) {
                mDirectory = (Directory)item;
                Timber.d("Initialized ContentProvider");
            }

            @Override
            public void onNotFound(String key) {
            }
        });
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
                        String sortOrder) {
        String searchString = uri.getLastPathSegment();
        return getSuggestions(searchString);
    }

    private MatrixCursor getSuggestions(String query) {
        MatrixCursor cursor = new MatrixCursor(CHAPTER_COLUMNS);

        for(Chapter chapter : mDirectory.getGroups()) {
            if(chapter.getName().toLowerCase().contains(query.toLowerCase())) {
                cursor.addRow(new Object[] { chapter.getGplusId(), chapter.getName(), chapter.getCity()+", "+chapter.getCountry(), SearchActivity.ACTION_FOUND, chapter.getGplusId() });
            }
        }
        return cursor;
    }

    @Override
    public String getType(Uri uri) {
        switch (uriMatcher.match(uri)) {
            case SEARCH_SUGGEST:
                return SearchManager.SUGGEST_MIME_TYPE;

            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues contentValues) {
        throw new UnsupportedOperationException();
    }

    @Override
    public int delete(Uri uri, String s, String[] strings) {
        throw new UnsupportedOperationException();
    }

    @Override
    public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
        throw new UnsupportedOperationException();
    }
}

@@ -1749,24 +1749,26 @@
 riMatcher;%0D%0A
+%0D%0A
     static %7B
@@ -2019,412 +2019,8 @@
  %7B%0D%0A
-        App.getInstance().getModelCache().getAsync(%22chapter_list_hub%22, false, new ModelCache.CacheListener() %7B%0D%0A            @Override%0D%0A            public void onGet(Object item) %7B%0D%0A                mDirectory = (Directory)item;%0D%0A                Timber.d(%22Initialized ContentProvider%22);%0D%0A            %7D%0D%0A%0D%0A            @Override%0D%0A            public void onNotFound(String key) %7B%0D%0A            %7D%0D%0A        %7D);%0D%0A
     
@@ -2199,24 +2199,102 @@
 rtOrder) %7B%0D%0A
+%0D%0A        if(! isSetupDone()) %7B%0D%0A            prepareProvider();%0D%0A        %7D%0D%0A%0D%0A
         Stri
@@ -2385,32 +2385,373 @@
 ing);%0D%0A    %7D%0D%0A%0D%0A
+    private boolean isSetupDone() %7B%0D%0A        return mDirectory != null;%0D%0A    %7D%0D%0A%0D%0A    private void prepareProvider() %7B%0D%0A        final App app = (App) getContext().getApplicationContext();%0D%0A        mDirectory = (Directory) app.getInstance().getModelCache().get(%22chapter_list_hub%22);%0D%0A        Timber.d(%22Initialized ContentProvider%22);%0D%0A    %7D%0D%0A%0D%0A
     private Matr
@@ -2870,16 +2870,17 @@
      for
+ 
 (Chapter
@@ -2931,16 +2931,17 @@
       if
+ 
 (chapter
@@ -3045,11 +3045,9 @@
 ct%5B%5D
- %7B 
+%7B
 chap
@@ -3104,14 +3104,18 @@
 ty()
-+
+ + 
 %22, %22
-+
+ + 
 chap
@@ -3181,17 +3181,16 @@
 plusId()
- 
 %7D);%0D%0A   

Move initialisation of content provider from onCreate to first query