package com.example.fueltrack;

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 FuelContentProvider extends ContentProvider {
  public static final String PROVIDER_NAME = "com.example.fueltrack.provider";
  public static final Uri CONTENT_URI = Uri.parse("content://" + PROVIDER_NAME
      + "mileage");
  public static final int ALLMILEAGE = 1;
  public static final int MILEAGE_ID = 2;

  private static FuelDbAdapter dba;
  private static UriMatcher uriMatcher;
  static {
    uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    uriMatcher.addURI(PROVIDER_NAME, "mileage", ALLMILEAGE);
    uriMatcher.addURI(PROVIDER_NAME, "mileage/#", MILEAGE_ID);
  }

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

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

  @Override
  public String getType(Uri uri) {
    switch (uriMatcher.match(uri)) {
      case ALLMILEAGE:
        return "vnc.android.cursor.dir/vnd.com.example.fueltrack";
      case MILEAGE_ID:
        return "vnd.android.cursor.item/vnd.com.example.fueltrack";
      default:
        throw new IllegalArgumentException("Unsupported URI: " + uri);
    }
  }

  @Override
  public Uri insert(Uri uri, ContentValues values) {
    long id = dba.addMileage(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 delete(Uri uri, String selection, String[] selectionArgs) {
    long rowsDeleted = 0;
    switch (uriMatcher.match(uri)) {
      case ALLMILEAGE:
        rowsDeleted = dba.deleteMileage(selection, selectionArgs);
        break;
      case MILEAGE_ID:
        long id = Long.parseLong(uri.getPathSegments().get(1));
        rowsDeleted = dba.deleteMileage(id);
        break;
      default:
        throw new IllegalArgumentException("Unsupported URI: " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return (int) rowsDeleted;
  }

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

}
