package gr.mybook.myprovider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;

public class MyContentProvider extends ContentProvider {
	//---helper for database---
	private static final String DATABASE_NAME = "MyDB.db";
	private static final int DATABASE_VERSION = 1;
	private static final String DATABASE_TABLE = "students";
	public static final String COLUMN_ID = "_id";
	public static final String STUDENT_ID = "serial";
	public static final String STUDENT_NAME = "name";
	public static final String STUDENT_PHONE = "phone";
	private static final String DATABASE_CREATE = "create table students (_id integer primary key, " + "serial text not null, name text not null, phone text not null);";
	private static final String DATABASE_UPGRADE = "DROP TABLE IF EXISTS"+DATABASE_TABLE;

	//---for content provider---
	public static final String AUTHORITY ="gr.mybook.myprovider";
	public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + DATABASE_TABLE);
	private static final int STUDENTS = 1;
	private static final int STUDENTS_ID = 2;
	
	private static final UriMatcher uriMatcher;
	static {
		uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		uriMatcher.addURI(AUTHORITY, DATABASE_TABLE, STUDENTS);
		uriMatcher.addURI(AUTHORITY, DATABASE_TABLE + "/#", STUDENTS_ID);
	}

	private SQLiteDatabase db;
	
	private static class MyHelper extends SQLiteOpenHelper {
		MyHelper(Context context, String name, CursorFactory factory, int version) {
			super(context, DATABASE_NAME, factory, DATABASE_VERSION);
		}
		
			@Override
			public void onCreate(SQLiteDatabase db) {
				db.execSQL(DATABASE_CREATE);
			}

			@Override
			public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
				db.execSQL(DATABASE_UPGRADE);
				onCreate(db);
			}
	}

	public MyContentProvider() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		int rowsCount = 0;
		switch (uriMatcher.match(uri)) {
		case STUDENTS:
			rowsCount = db.delete(DATABASE_TABLE, selection, selectionArgs);
			break;
		case STUDENTS_ID:
			String id = uri.getLastPathSegment();
			if (TextUtils.isEmpty(selection)) {
				rowsCount = db.delete(DATABASE_TABLE, COLUMN_ID + "=" + id, null);
			}
			else {
				rowsCount = db.delete(DATABASE_TABLE, COLUMN_ID + "=" + id + " and " + selection, selectionArgs);
			}
			break;
			default: throw new IllegalArgumentException("Unkown URI: " + uri);
		}
		getContext().getContentResolver().notifyChange(uri,  null);
		return rowsCount;
	}

	@Override
	public String getType(Uri uri) {
		switch (uriMatcher.match(uri)) {
			//---uri for all students---
			case STUDENTS:
				return "vnd.android.cursor.dir/vnd.myprovider.students";
				//---uri for a student---
			case STUDENTS_ID:
				return "vnd.android.cursor.item/vnd.myprovider.students";
			default: throw new IllegalArgumentException("Unsupported URI: " + uri);
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		long rowID = 0;
		switch (uriMatcher.match(uri)) {
		case STUDENTS:
			rowID = db.insert(DATABASE_TABLE, null, values);
			break;
			default: throw new IllegalArgumentException("Unkown URI: " + uri);
		}
		getContext().getContentResolver().notifyChange(uri, null);
			return Uri.parse(DATABASE_TABLE + "/" + rowID);
	}

	@Override
	public boolean onCreate() {
		Context context =getContext();
		MyHelper dbHelper = new MyHelper(context, null, null, 1);
		db = dbHelper.getWritableDatabase();
		if (db == null) {
			return false;
		} else {return true;}
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
			String sortOrder) {
		SQLiteQueryBuilder sBuilder = new SQLiteQueryBuilder();
		sBuilder.setTables(DATABASE_TABLE);
		switch (uriMatcher.match(uri)) {
			//---uri for a student---
			case STUDENTS_ID:
				sBuilder.appendWhere(COLUMN_ID + "="  + uri.getLastPathSegment());
				break;
				//---uri for all students---
			case STUDENTS:
				break;
		default: throw new IllegalArgumentException("UNKOWN URI");
	}

		Cursor cursor = sBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
		cursor.setNotificationUri(getContext().getContentResolver(), uri);
		return cursor;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
		int rowsCount = 0;
		switch (uriMatcher.match(uri)) {
		case STUDENTS:
			rowsCount = db.update(DATABASE_TABLE, values, selection, selectionArgs);
			break;
		case STUDENTS_ID:
			String id = uri.getLastPathSegment();
			if (TextUtils.isEmpty(selection)) {
				rowsCount = db.update(DATABASE_TABLE, values, COLUMN_ID + "=" + id, null);
			}
			else {
				rowsCount = db.update(DATABASE_TABLE, values, COLUMN_ID + "=" + id + " and " + selection, selectionArgs);
			}
			break;
			default: throw new IllegalArgumentException("Unkown URI " + uri);
		}
		getContext().getContentResolver().notifyChange(uri,  null);
		return rowsCount;
	}

}
