package com.kfzs.duanduan.datashare.provider.records;

// @formatter:off
import java.util.Date;

import android.content.Context;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.support.v4.content.CursorLoader;

import com.kfzs.duanduan.datashare.provider.base.AbstractSelection;

/**
 * Selection for the {@code records} table.
 */
@SuppressWarnings({"unused", "WeakerAccess", "Recycle"})
public class RecordsSelection extends AbstractSelection<RecordsSelection> {
    @Override
    protected Uri baseUri() {
        return RecordsColumns.CONTENT_URI;
    }

    /**
     * Query the given content resolver using this selection.
     *
     * @param contentResolver The content resolver to query.
     * @param projection A list of which columns to return. Passing null will return all columns, which is inefficient.
     * @return A {@code RecordsCursor} object, which is positioned before the first entry, or null.
     */
    public RecordsCursor query(ContentResolver contentResolver, String[] projection) {
        Cursor cursor = contentResolver.query(uri(), projection, sel(), args(), order());
        if (cursor == null) return null;
        return new RecordsCursor(cursor);
    }

    /**
     * Equivalent of calling {@code query(contentResolver, null)}.
     */
    public RecordsCursor query(ContentResolver contentResolver) {
        return query(contentResolver, null);
    }

    /**
     * Query the given content resolver using this selection.
     *
     * @param context The context to use for the query.
     * @param projection A list of which columns to return. Passing null will return all columns, which is inefficient.
     * @return A {@code RecordsCursor} object, which is positioned before the first entry, or null.
     */
    public RecordsCursor query(Context context, String[] projection) {
        Cursor cursor = context.getContentResolver().query(uri(), projection, sel(), args(), order());
        if (cursor == null) return null;
        return new RecordsCursor(cursor);
    }

    /**
     * Equivalent of calling {@code query(context, null)}.
     */
    public RecordsCursor query(Context context) {
        return query(context, null);
    }


    /**
     * {@inheritDoc}
     */
    @Override
    public CursorLoader getCursorLoader(Context context, String[] projection) {
        return new CursorLoader(context, uri(), projection, sel(), args(), order()) {
            @Override
            public Cursor loadInBackground() {
                return new RecordsCursor(super.loadInBackground());
            }
        };
    }


    public RecordsSelection id(long... value) {
        addEquals("records." + RecordsColumns._ID, toObjectArray(value));
        return this;
    }

    public RecordsSelection idNot(long... value) {
        addNotEquals("records." + RecordsColumns._ID, toObjectArray(value));
        return this;
    }

    public RecordsSelection orderById(boolean desc) {
        orderBy("records." + RecordsColumns._ID, desc);
        return this;
    }

    public RecordsSelection orderById() {
        return orderById(false);
    }

    public RecordsSelection createTime(long... value) {
        addEquals(RecordsColumns.CREATE_TIME, toObjectArray(value));
        return this;
    }

    public RecordsSelection createTimeNot(long... value) {
        addNotEquals(RecordsColumns.CREATE_TIME, toObjectArray(value));
        return this;
    }

    public RecordsSelection createTimeGt(long value) {
        addGreaterThan(RecordsColumns.CREATE_TIME, value);
        return this;
    }

    public RecordsSelection createTimeGtEq(long value) {
        addGreaterThanOrEquals(RecordsColumns.CREATE_TIME, value);
        return this;
    }

    public RecordsSelection createTimeLt(long value) {
        addLessThan(RecordsColumns.CREATE_TIME, value);
        return this;
    }

    public RecordsSelection createTimeLtEq(long value) {
        addLessThanOrEquals(RecordsColumns.CREATE_TIME, value);
        return this;
    }

    public RecordsSelection orderByCreateTime(boolean desc) {
        orderBy(RecordsColumns.CREATE_TIME, desc);
        return this;
    }

    public RecordsSelection orderByCreateTime() {
        orderBy(RecordsColumns.CREATE_TIME, false);
        return this;
    }

    public RecordsSelection name(String... value) {
        addEquals(RecordsColumns.NAME, value);
        return this;
    }

    public RecordsSelection nameNot(String... value) {
        addNotEquals(RecordsColumns.NAME, value);
        return this;
    }

    public RecordsSelection nameLike(String... value) {
        addLike(RecordsColumns.NAME, value);
        return this;
    }

    public RecordsSelection nameContains(String... value) {
        addContains(RecordsColumns.NAME, value);
        return this;
    }

    public RecordsSelection nameStartsWith(String... value) {
        addStartsWith(RecordsColumns.NAME, value);
        return this;
    }

    public RecordsSelection nameEndsWith(String... value) {
        addEndsWith(RecordsColumns.NAME, value);
        return this;
    }

    public RecordsSelection orderByName(boolean desc) {
        orderBy(RecordsColumns.NAME, desc);
        return this;
    }

    public RecordsSelection orderByName() {
        orderBy(RecordsColumns.NAME, false);
        return this;
    }
}
