package com.odoo.work.orm.sync.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

import com.odoo.work.orm.OModel;
import com.odoo.work.orm.models.ModelRegistry;

public class BaseContentProvider extends ContentProvider {
    public static final String TAG = BaseContentProvider.class.getSimpleName();
    private final int COLLECTION = 1;
    private final int SINGLE_ROW = 2;
    public UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);

    public OModel getModel(Context context, Uri uri) {
        return ModelRegistry.getModel(context, uri.getQueryParameter("model"));
    }

    @Override
    public boolean onCreate() {
        return true;
    }

    private void setMatcher(OModel model) {
        String authority = model.getAuthority();
        matcher.addURI(authority, model.getModelName(), COLLECTION);
        matcher.addURI(authority, model.getModelName() + "/#", SINGLE_ROW);
    }

    @Nullable
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String order) {
        OModel model = getModel(getContext(), uri);
        SQLiteDatabase db = model.getWritableDatabase();
        Cursor cr = db.query(model.getTableName(), projection, selection, selectionArgs, null, null, order);
        Context ctx = getContext();
        if (cr != null && ctx != null)
            cr.setNotificationUri(ctx.getContentResolver(), uri);
        return cr;
    }

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

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, ContentValues contentValues) {
        OModel model = getModel(getContext(), uri);
        SQLiteDatabase db = model.getWritableDatabase();
        long new_id = db.insert(model.getTableName(), null, contentValues);
        notifyDataChange(uri);
        return Uri.withAppendedPath(uri, new_id + "");
    }

    @Override
    public int delete(Uri uri, String s, String[] strings) {
        OModel model = getModel(getContext(), uri);
        notifyDataChange(uri);
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues contentValues, String where, String[] args) {
        OModel model = getModel(getContext(), uri);
        setMatcher(model);
        int match = matcher.match(uri);
        int count = 0;
        SQLiteDatabase db = null;
        switch (match) {
            case COLLECTION:
                break;
            case SINGLE_ROW:
                db = model.getWritableDatabase();
                int updateId = Integer.parseInt(uri.getLastPathSegment());
                where = "_id = ?";
                args = new String[]{updateId + ""};
                count = db.update(model.getTableName(), contentValues, where, args);
                break;
        }
        if (db != null) db.close();
        notifyDataChange(uri);
        return count;
    }

    private void notifyDataChange(Uri uri) {
        // Send broadcast to registered ContentObservers, to refresh UI.
        Context ctx = getContext();
        assert ctx != null;
        ctx.getContentResolver().notifyChange(uri, null);
    }
}

@@ -373,33 +373,8 @@
 ble;
-%0Aimport android.util.Log;
 %0A%0Aim
@@ -2572,218 +2572,22 @@
     
-int match = matcher.match(uri);%0A        int count = 0;%0A        SQLiteDatabase db = null;%0A        switch (match) %7B%0A            case COLLECTION:%0A                break;%0A            case SINGLE_ROW:%0A               
+SQLiteDatabase
  db 
@@ -2617,24 +2617,16 @@
 base();%0A
-        
         
@@ -2692,24 +2692,16 @@
         
-   
-     
 where = 
@@ -2711,24 +2711,16 @@
 d = ?%22;%0A
-        
         
@@ -2763,23 +2763,19 @@
         
-       
+int
  count =
@@ -2840,77 +2840,8 @@
 s);%0A
-                break;%0A        %7D%0A        if (db != null) db.close();%0A
     

update record in local