package com.example.checkregister;

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

public class CheckRegisterProvider extends ContentProvider {
  public static final String AUTHORITY = "com.example.checkregister.provider";
  public static final Uri AUTHORITY_URI = Uri.parse("content://" + AUTHORITY);
  public static final Uri CONTENT_URI = Uri.withAppendedPath(AUTHORITY_URI, "check");
  public static final String REGISTER_TYPE = "vnd.android.cursor.dir";
  public static final String REGISTER_CHECK_TYPE = "vnd.android.cursor.item";
  public static final String KEY_ID = CheckRegisterDBAdapter.KEY_ID;
  public static final String KEY_CHECKNUM = CheckRegisterDBAdapter.KEY_CHECKNUM;
  public static final String KEY_DATE = CheckRegisterDBAdapter.KEY_DATE;
  public static final String KEY_PAYEE = CheckRegisterDBAdapter.KEY_PAYEE;
  public static final String KEY_AMOUNT = CheckRegisterDBAdapter.KEY_AMOUNT;
  public static final String KEY_BALANCE = CheckRegisterDBAdapter.KEY_BALANCE;
  public static final String KEY_MEMO = CheckRegisterDBAdapter.KEY_MEMO;
  public static final String KEY_ACCOUNT_NAME  = CheckRegisterDBAdapter.KEY_ACCOUNT_NAME;

  private static final int ALLCHECKS = 1;
  private static final int CHECK_ID = 2;

  private static CheckRegisterDBAdapter dba;
  private static UriMatcher uriMatcher;
  static {
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    uriMatcher.addURI(AUTHORITY, "check", ALLCHECKS);
    uriMatcher.addURI(AUTHORITY, "check/#", CHECK_ID);
  }

  @Override
  public boolean onCreate() {
    dba = new CheckRegisterDBAdapter(getContext());
    dba.open();
    return true;
  }

  @Override
  public String getType(Uri uri) {
    switch (uriMatcher.match(uri)) {
      case ALLCHECKS:
        return REGISTER_TYPE;
      case CHECK_ID:
        return REGISTER_CHECK_TYPE;
      default:
        throw new IllegalArgumentException("Unsupported URI: " + uri);
    }
  }

  @Override
  public Cursor query(Uri uri, String[] projection, String selection,
      String[] selectionArgs, String sortOrder) {
    Cursor c;
    switch (uriMatcher.match(uri)) {
      case ALLCHECKS:
        c = dba.getCheckList(projection, selection, selectionArgs, sortOrder);
        break;
      case CHECK_ID:
        long id = Long.parseLong(uri.getPathSegments().get(1));
        c = dba.getCheckRecord(id);
        break;
      default:
        throw new IllegalArgumentException("Unsupported URI: " + uri);
    }
    return c;
  }

  @Override
  public Uri insert(Uri uri, ContentValues values) {
    long id = dba.addCheck(values);
    if (id > 0) {
      Uri newUri = ContentUris.withAppendedId(CONTENT_URI, id);
      getContext().getContentResolver().notifyChange(uri, null);
      return newUri;
    }
    throw new SQLException("Error inserting record for URI: " + uri);
  }

  @Override
  public int update(Uri uri, ContentValues values, String selection,
      String[] selectionArgs) {
    long rowsUpdated = 0;
    switch (uriMatcher.match(uri)) {
      case ALLCHECKS:
        rowsUpdated = dba.updateCheck(values, selection, selectionArgs);
        break;
      case CHECK_ID:
        long id = Long.parseLong(uri.getPathSegments().get(1));
        rowsUpdated = dba.updateCheck(id, values);
        break;
      default:
        throw new IllegalArgumentException("Unsupported URI: " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return (int) rowsUpdated;
  }

  @Override
  public int delete(Uri uri, String selection, String[] selectionArgs) {
    long rowsDeleted = 0;
    switch (uriMatcher.match(uri)) {
      case ALLCHECKS:
        rowsDeleted = dba.deleteCheck(selection, selectionArgs);
        break;
      case CHECK_ID:
        long id = Long.parseLong(uri.getPathSegments().get(1));
        rowsDeleted = dba.deleteCheck(id);
        break;
      default:
        throw new IllegalArgumentException("Unsupported URI: " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return (int) rowsDeleted;
  }

}