package com.nouribygi.masnavi.database;

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

import com.nouribygi.masnavi.util.MasnaviSettings;


public class MasnaviDataProvider extends ContentProvider {

    public static String AUTHORITY =
            "com.nouribygi.masnavi.database.MasnaviDataProvider";
    public static final Uri
            SEARCH_URI = Uri.parse("content://" + AUTHORITY + "/masnavi/search");

    public static final String VERSES_MIME_TYPE =
            ContentResolver.CURSOR_DIR_BASE_TYPE +
                    "/vnd.com.nouribygi.masnavi";
    public static final String AYAH_MIME_TYPE =
            ContentResolver.CURSOR_ITEM_BASE_TYPE +
                    "/vnd.com.nouribygi.masnavi";

    // UriMatcher stuff
    private static final int SEARCH_VERSES = 0;
    private static final int GET_VERSE = 1;
    private static final int SEARCH_SUGGEST = 2;
    private static final UriMatcher sURIMatcher = buildUriMatcher();

    private DatabaseHandler mDatabase = null;

    private static UriMatcher buildUriMatcher() {
        UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
        matcher.addURI(AUTHORITY, "masnavi/search", SEARCH_VERSES);
        matcher.addURI(AUTHORITY, "masnavi/search/*", SEARCH_VERSES);
        matcher.addURI(AUTHORITY, "masnavi/search/*/*", SEARCH_VERSES);
        matcher.addURI(AUTHORITY, "masnavi/verse/#/#", GET_VERSE);
        matcher.addURI(AUTHORITY, "masnavi/verse/*/#/#", GET_VERSE);
        matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY,
                SEARCH_SUGGEST);
        matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*",
                SEARCH_SUGGEST);
        return matcher;
    }

    @Override
    public boolean onCreate() {
        mDatabase = DatabaseHandler.getInstance(getContext());
        return true;
    }

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

        String query = "";
        if (selectionArgs.length >= 1)
            query = selectionArgs[0];

        int bookCode = MasnaviSettings.getSelectedBook(getContext());

        return mDatabase.search(query, bookCode);

    }

    @Override
    public String getType(Uri uri) {
        switch (sURIMatcher.match(uri)) {
            case SEARCH_VERSES:
                return VERSES_MIME_TYPE;
            case GET_VERSE:
                return AYAH_MIME_TYPE;
            case SEARCH_SUGGEST:
                return SearchManager.SUGGEST_MIME_TYPE;
            default:
                throw new IllegalArgumentException("Unknown URL " + uri);
        }
    }

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

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        throw new UnsupportedOperationException();
    }

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