package com.revoapp.calendar.database;

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

import com.revoapp.api.logger.RevoLogger;

public class CalendarContentProvider extends ContentProvider {
    private DatabaseHandler database;
    private PillsDatabaseHandler pillsDatabase;

    // used for the UriMacher
    private static final int PILLS = 1;
    private static final int PILLS_TIME = 2;
    private static final int EVENTS = 3;
    private static final int PILLS_ID = 10;
    private static final int PILLS_TIME_ID = 20;
    private static final int EVENTS_ID = 30;

    private static final String AUTHORITY = "com.revoapp.calendar.database.CalendarContentProvider";

    private static final String BASE_PATH = "pills";
    private static final String BASE_PATH2 = "pills_time";
    private static final String BASE_PATH_ENVETS = "events";
    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
            + "/" + BASE_PATH);

    public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE
            + "/todos";
    public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE
            + "/todo";

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

    static {
        sURIMatcher.addURI(AUTHORITY, BASE_PATH, PILLS);
        sURIMatcher.addURI(AUTHORITY, BASE_PATH + "/#", PILLS_ID);
        sURIMatcher.addURI(AUTHORITY, BASE_PATH2, PILLS_TIME);
        sURIMatcher.addURI(AUTHORITY, BASE_PATH2 + "/#", PILLS_TIME_ID);
        sURIMatcher.addURI(AUTHORITY, BASE_PATH_ENVETS, EVENTS);
        sURIMatcher.addURI(AUTHORITY, BASE_PATH_ENVETS + "/#", EVENTS_ID);
    }

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

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

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

    @Override
    public boolean onCreate() {
        database = new DatabaseHandler(getContext());
        pillsDatabase = new PillsDatabaseHandler(getContext());
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
        SQLiteDatabase db;

        switch (sURIMatcher.match(uri)) {
            case PILLS:
                queryBuilder.setTables(BASE_PATH);
                db = pillsDatabase.getWritableDatabase();
                break;
            case PILLS_ID:
                queryBuilder.setTables(BASE_PATH);
                db = pillsDatabase.getWritableDatabase();
                break;
            case PILLS_TIME:
                queryBuilder.setTables(BASE_PATH2);
                db = pillsDatabase.getWritableDatabase();
                break;
            case PILLS_TIME_ID:
                queryBuilder.setTables(BASE_PATH2);
                db = pillsDatabase.getWritableDatabase();
                // queryBuilder.setTables("pills_time");
                // String id = uri.getPathSegments().get(1);
                // queryBuilder.appendWhere(CountriesDb.KEY_ROWID + "=" + id);
                break;
            case EVENTS:
                queryBuilder.setTables(BASE_PATH_ENVETS);
                db = database.getWritableDatabase();
                break;
            case EVENTS_ID:
                queryBuilder.setTables(BASE_PATH_ENVETS);
                db = database.getWritableDatabase();
                break;
            default:
                throw new IllegalArgumentException("Unsupported URI: " + uri);
        }

        Cursor cursor = queryBuilder.query(db, projection, selection,
                selectionArgs, null, null, sortOrder);
        // make sure that potential listeners are getting notified
        cursor.setNotificationUri(getContext().getContentResolver(), uri);

        return cursor;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        switch (sURIMatcher.match(uri)) {
            case PILLS_ID:
                int pillId = Integer.parseInt(selectionArgs[0]);
                RevoLogger.log(RevoLogger.DEBUG, this.getClass().getName(),
                        "update", "taking pill id:" + pillId);
                return pillsDatabase.takePill(pillId);
            default:
                return 0;
        }
    }
}