package com.kfzs.duanduan.datashare.provider;

// @formatter:off

import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.util.Log;

import com.kfzs.duanduan.datashare.BuildConfig;
import com.kfzs.duanduan.datashare.BuildConfigSaver;
import com.kfzs.duanduan.datashare.provider.base.BaseContentProvider;
import com.kfzs.duanduan.datashare.provider.downloadtask.DownloadtaskColumns;
import com.kfzs.duanduan.datashare.provider.records.RecordsColumns;
import com.kfzs.duanduan.datashare.provider.session.SessionColumns;

import java.util.Arrays;

public class KFZSDDProvider extends BaseContentProvider {
    private static final String TAG = KFZSDDProvider.class.getSimpleName();

    private static final boolean DEBUG = BuildConfig.DEBUG;

    private static final String TYPE_CURSOR_ITEM = "vnd.android.cursor.item/";
    private static final String TYPE_CURSOR_DIR = "vnd.android.cursor.dir/";

    public static final String AUTHORITY = "com.kfzs.duanduan.datashare.provider." + BuildConfigSaver.getInstance().getDatashare();
    public static final String CONTENT_URI_BASE = "content://" + AUTHORITY;

    private static final int URI_TYPE_DOWNLOADTASK = 0;
    private static final int URI_TYPE_DOWNLOADTASK_ID = 1;

    private static final int URI_TYPE_RECORDS = 2;
    private static final int URI_TYPE_RECORDS_ID = 3;

    private static final int URI_TYPE_SESSION = 4;
    private static final int URI_TYPE_SESSION_ID = 5;


    private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);

    static {
        URI_MATCHER.addURI(AUTHORITY, DownloadtaskColumns.TABLE_NAME, URI_TYPE_DOWNLOADTASK);
        URI_MATCHER.addURI(AUTHORITY, DownloadtaskColumns.TABLE_NAME + "/#", URI_TYPE_DOWNLOADTASK_ID);
        URI_MATCHER.addURI(AUTHORITY, RecordsColumns.TABLE_NAME, URI_TYPE_RECORDS);
        URI_MATCHER.addURI(AUTHORITY, RecordsColumns.TABLE_NAME + "/#", URI_TYPE_RECORDS_ID);
        URI_MATCHER.addURI(AUTHORITY, SessionColumns.TABLE_NAME, URI_TYPE_SESSION);
        URI_MATCHER.addURI(AUTHORITY, SessionColumns.TABLE_NAME + "/#", URI_TYPE_SESSION_ID);
    }

    @Override
    protected SQLiteOpenHelper createSqLiteOpenHelper() {
        return KFZSDDSQLiteOpenHelper.getInstance(getContext());
    }

    @Override
    protected boolean hasDebug() {
        return DEBUG;
    }

    @Override
    public String getType(Uri uri) {
        int match = URI_MATCHER.match(uri);
        switch (match) {
            case URI_TYPE_DOWNLOADTASK:
                return TYPE_CURSOR_DIR + DownloadtaskColumns.TABLE_NAME;
            case URI_TYPE_DOWNLOADTASK_ID:
                return TYPE_CURSOR_ITEM + DownloadtaskColumns.TABLE_NAME;

            case URI_TYPE_RECORDS:
                return TYPE_CURSOR_DIR + RecordsColumns.TABLE_NAME;
            case URI_TYPE_RECORDS_ID:
                return TYPE_CURSOR_ITEM + RecordsColumns.TABLE_NAME;

            case URI_TYPE_SESSION:
                return TYPE_CURSOR_DIR + SessionColumns.TABLE_NAME;
            case URI_TYPE_SESSION_ID:
                return TYPE_CURSOR_ITEM + SessionColumns.TABLE_NAME;

        }
        return null;
    }

    @Override
    public Uri insert(@NonNull Uri uri, ContentValues values) {
        if (DEBUG) Log.d(TAG, "insert uri=" + uri + " values=" + values);
        return super.insert(uri, values);
    }

    @Override
    public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) {
        if (DEBUG) Log.d(TAG, "bulkInsert uri=" + uri + " values.length=" + values.length);
        return super.bulkInsert(uri, values);
    }

    @Override
    public int update(@NonNull Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        if (DEBUG)
            Log.d(TAG, "update uri=" + uri + " values=" + values + " selection=" + selection + " selectionArgs=" + Arrays.toString(selectionArgs));
        return super.update(uri, values, selection, selectionArgs);
    }

    @Override
    public int delete(@NonNull Uri uri, String selection, String[] selectionArgs) {
        if (DEBUG)
            Log.d(TAG, "delete uri=" + uri + " selection=" + selection + " selectionArgs=" + Arrays.toString(selectionArgs));
        return super.delete(uri, selection, selectionArgs);
    }

    @Override
    public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        if (DEBUG)
            Log.d(TAG, "query uri=" + uri + " selection=" + selection + " selectionArgs=" + Arrays.toString(selectionArgs) + " sortOrder=" + sortOrder
                    + " groupBy=" + uri.getQueryParameter(QUERY_GROUP_BY) + " having=" + uri.getQueryParameter(QUERY_HAVING) + " limit=" + uri.getQueryParameter(QUERY_LIMIT));
        return super.query(uri, projection, selection, selectionArgs, sortOrder);
    }

    @Override
    protected QueryParams getQueryParams(Uri uri, String selection, String[] projection) {
        QueryParams res = new QueryParams();
        String id = null;
        int matchedId = URI_MATCHER.match(uri);
        switch (matchedId) {
            case URI_TYPE_DOWNLOADTASK:
            case URI_TYPE_DOWNLOADTASK_ID:
                res.table = DownloadtaskColumns.TABLE_NAME;
                res.idColumn = DownloadtaskColumns._ID;
                res.tablesWithJoins = DownloadtaskColumns.TABLE_NAME;
                res.orderBy = DownloadtaskColumns.DEFAULT_ORDER;
                break;

            case URI_TYPE_RECORDS:
            case URI_TYPE_RECORDS_ID:
                res.table = RecordsColumns.TABLE_NAME;
                res.idColumn = RecordsColumns._ID;
                res.tablesWithJoins = RecordsColumns.TABLE_NAME;
                res.orderBy = RecordsColumns.DEFAULT_ORDER;
                break;

            case URI_TYPE_SESSION:
            case URI_TYPE_SESSION_ID:
                res.table = SessionColumns.TABLE_NAME;
                res.idColumn = SessionColumns._ID;
                res.tablesWithJoins = SessionColumns.TABLE_NAME;
                res.orderBy = SessionColumns.DEFAULT_ORDER;
                break;

            default:
                throw new IllegalArgumentException("The uri '" + uri + "' is not supported by this ContentProvider");
        }

        switch (matchedId) {
            case URI_TYPE_DOWNLOADTASK_ID:
            case URI_TYPE_RECORDS_ID:
            case URI_TYPE_SESSION_ID:
                id = uri.getLastPathSegment();
        }
        if (id != null) {
            if (selection != null) {
                res.selection = res.table + "." + res.idColumn + "=" + id + " and (" + selection + ")";
            } else {
                res.selection = res.table + "." + res.idColumn + "=" + id;
            }
        } else {
            res.selection = selection;
        }
        return res;
    }
}
