package com.example.georgekaraolanis.project10_inventoryapp.data;

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

import com.example.georgekaraolanis.project10_inventoryapp.data.InventoryContract.InventoryEntry;

public class InventoryItemProvider extends ContentProvider{

    /*tag for logging*/
    public static final String LOG_TAG = InventoryItemProvider.class.getSimpleName();

    /*URI matcher code for list of items */
    private static final int ITEMS = 100;

    /*URI matcher code for one item */
    private static final int ITEM_ID = 101;

    /*URI matcher*/
    private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

    /*Static initializer for what uris the matcher should recognize as accepted*/
    static {
        uriMatcher.addURI(InventoryContract.CONTENT_AUTHORITY, InventoryContract.PATH_INVENTORY, ITEMS);
        uriMatcher.addURI(InventoryContract.CONTENT_AUTHORITY, InventoryContract.PATH_INVENTORY + "/#", ITEM_ID);
    }

    /*The db helper object*/
    private InventoryDbHelper dbHelper;

    @Override
    public boolean onCreate() {
        dbHelper = new InventoryDbHelper(getContext());
        return true;
    }

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

        /*Get readable database*/
        SQLiteDatabase database = dbHelper.getReadableDatabase();

        /*Cursor to return*/
        Cursor cursor;

        /*Get the match from the uri matcher*/
        int match = uriMatcher.match(uri);

        /*Check the match*/
        switch (match) {
            case ITEMS:
                cursor = database.query(InventoryEntry.TABLE_NAME, projection, selection, selectionArgs,
                        null, null, sortOrder);
                break;
            case ITEM_ID:
                selection = InventoryEntry._ID + "=?";
                selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) };

                cursor = database.query(InventoryEntry.TABLE_NAME, projection, selection, selectionArgs,
                        null, null, sortOrder);
                break;
            default:
                throw new IllegalArgumentException("Cannot query unknown URI " + uri);
        }

        /*Set notification uri on cursor. If the uri changes, we need to change cursor*/
        cursor.setNotificationUri(getContext().getContentResolver(),uri);

        /*Return the cursor*/
        return cursor;
    }

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

    @Override
    public Uri insert(Uri uri,ContentValues values) {
        return null;
    }

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

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

@@ -311,16 +311,41 @@
 net.Uri;
+%0Aimport android.util.Log;
 %0A%0Aimport
@@ -2790,115 +2790,2411 @@
     
-return null;%0A    %7D%0A%0A    @Override%0A    public Uri insert(Uri uri,ContentValues values) %7B%0A        return null
+final int match = uriMatcher.match(uri);%0A        switch (match) %7B%0A            case ITEMS:%0A                return InventoryEntry.CONTENT_LIST_TYPE;%0A            case ITEM_ID:%0A                return InventoryEntry.CONTENT_ITEM_TYPE;%0A            default:%0A                throw new IllegalStateException(%22Unknown URI %22 + uri + %22 with match %22 + match);%0A        %7D%0A    %7D%0A%0A    @Override%0A    public Uri insert(Uri uri,ContentValues values) %7B%0A        final int match = uriMatcher.match(uri);%0A        switch (match) %7B%0A            case ITEMS:%0A                return insertItem(uri, values);%0A            default:%0A                throw new IllegalArgumentException(%22Insertion is not supported for %22 + uri);%0A        %7D%0A    %7D%0A%0A    /*Insert an item into database*/%0A    private Uri insertItem(Uri uri, ContentValues values)%7B%0A%0A        /*Check that the item name is not null*/%0A        String name = values.getAsString(InventoryEntry.COLUMN_ITEM_NAME);%0A        if (name == null) %7B%0A            throw new IllegalArgumentException(%22Item requires a name%22);%0A        %7D%0A%0A        /*Check that image path is not null*/%0A        String imagePath = values.getAsString(InventoryEntry.COLUMN_ITEM_IMAGE);%0A        if (imagePath == null) %7B%0A            throw new IllegalArgumentException(%22Item requires an image%22);%0A        %7D%0A%0A        /*Check that quantity is greater than or equal to zero*/%0A        Integer quantity = values.getAsInteger(InventoryEntry.COLUMN_ITEM_QUANTITY);%0A        if (quantity != null && quantity %3C 0) %7B%0A            throw new IllegalArgumentException(%22Item requires a valid quantity%22);%0A        %7D%0A%0A        /*Check that price is greater than or equal to zero*/%0A        Float price = values.getAsFloat(InventoryEntry.COLUMN_ITEM_PRICE);%0A        if (price != null && price %3C 0) %7B%0A            throw new IllegalArgumentException(%22Item requires a valid price%22);%0A        %7D%0A%0A        /*Get writeable database*/%0A        SQLiteDatabase database = dbHelper.getWritableDatabase();%0A%0A        /*Insert new item with the given values*/%0A        long id = database.insert(InventoryEntry.TABLE_NAME,null,values);%0A        if (id == -1) %7B%0A            Log.e(LOG_TAG, %22Failed to insert row for %22 + uri);%0A            return null;%0A        %7D%0A%0A        /*Notify listeners that we have new data*/%0A        getContext().getContentResolver().notifyChange(uri, null);%0A%0A        /*Return uri with the new id*/%0A        return ContentUris.withAppendedId(uri, id)
 ;%0A  

Implement insert method