package com.chat.db.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
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.SQLiteQueryBuilder;
import android.net.Uri;
import android.provider.BaseColumns;
import android.util.Log;

public class ContactProvider extends ContentProvider{
	private final static String AUTHORITY = ContactProvider.class.getCanonicalName();

	/**ÁªÏµÈËÊý¾Ý¿â*/
	private final static String DB_NAME =  "contact.db";

	/**ÁªÏµÈË*/
	private final static String CONTACT_TABLE = "contact";

	/**ÁªÏµÈË×é*/
	private final static String CONTACT_GROUP_TABLE = "group";

	/**Êý¾Ý¿â°æ±¾*/
	private final static int DB_VERSION = 1;

	/**ÁªÏµÈË uri*/
	public final static Uri CONTACT_URI = Uri.parse("content://"+AUTHORITY+"/"+CONTACT_TABLE);

	/**ÁªÏµ×é uri*/
	public final static Uri CONTACT_GROUP_URI = Uri.parse("content://"+AUTHORITY+"/"+CONTACT_GROUP_TABLE);

	private SQLiteOpenHelper dbHelper;
	private SQLiteDatabase db;
	private static final UriMatcher URI_MATCHER;

	/**UriMatcherÆ¥ÅäÖµ*/
	public static final int CONTACTS = 1;
	public static final int GROUPS = 2;

	static{
		URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
		URI_MATCHER.addURI(AUTHORITY, CONTACT_TABLE, CONTACTS);
		URI_MATCHER.addURI(AUTHORITY, CONTACT_GROUP_TABLE, GROUPS);
	}

	@Override
	public boolean onCreate() {
		dbHelper  = new ContactDatabaseHelper(getContext());
		return (dbHelper == null) ?false:true;
	}

	/**¸ù¾Ýuri²éÑ¯³öselectionÌõ¼þËùÆ¥ÅäµÄÈ«²¿¼ÇÂ¼ÆäÖÐprojection¾ÍÊÇÒ»¸öÁÐÃûÁÐ±í£¬±íÃ÷Ö»Ñ¡ÔñÖ¸¶¨µÄÊý¾ÝÁÐ*/
	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,String sortOrder) {
		Log.e("SQLite£º","½øÈë²éÑ¯ ");
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		db = dbHelper.getReadableDatabase();
		Cursor ret = null;

		switch(URI_MATCHER.match(uri)){
		case CONTACTS:
			qb.setTables(CONTACT_TABLE);
			ret = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
			break;
		case GROUPS:
			
			break;
		}

		ret.setNotificationUri(getContext().getContentResolver(), uri);
		return ret;
	}


	/**¸ù¾ÝuriËù²åÈëvalues*/
	@Override
	public Uri insert(Uri uri, ContentValues values) {
		Log.e("SQLite£º","½øÈë²åÈë ");
		db = dbHelper.getWritableDatabase();
		Uri result = null;
		switch(URI_MATCHER.match(uri)){
		case CONTACTS:
			long rowId = db.insert(CONTACT_TABLE, ContactColumns.ACCOUNT, values);
			result = ContentUris.withAppendedId(uri, rowId);
			break;
		default:break;
		}
		if(result!=null){
			getContext().getContentResolver().notifyChange(result,null);
		}
		return result;
	}


	/** ¸ù¾ÝUriÉ¾³ýselectionÌõ¼þËùÆ¥ÅäµÄÈ«²¿¼ÇÂ¼ */
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		db = dbHelper.getWritableDatabase();
		int count = 0;
		Log.e("SQLite£º","½øÈëÉ¾³ý ");
		switch(URI_MATCHER.match(uri)){
		case CONTACTS:
			count = db.delete(CONTACT_TABLE, selection, selectionArgs);
			break;
		default:break;
		}
		if (count != 0) {
			getContext().getContentResolver().notifyChange(uri, null);
		}
		return count;
	}

	/**¸ù¾ÝuriÐÞ¸ÄselectionÌõ¼þËùÆ¥ÅäµÄÈ«²¿¼ÇÂ¼*/
	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
		db = dbHelper.getWritableDatabase();
		int count = 0;
		Log.e("SQLite£º","½øÈë¸üÐÂ ");
		switch(URI_MATCHER.match(uri)){
		case CONTACTS:
			count = db.update(CONTACT_TABLE, values, selection, selectionArgs);
			break;
		default:break;
		}
		Log.e("SQLite£º","¸üÐÂ½á¹û " + count);
		if (count != 0) {
			getContext().getContentResolver().notifyChange(uri, null);
			
		}
		return count;
	}
	
	/**
	 *¸Ã·½·¨ÓÃÓÚ·µ»Øµ±Ç°UriËù´ú±íµÄÊý¾ÝµÄMIMEÀàÐÍ 
	 */
	@Override
	public String getType(Uri uri) {
		return null;
	}

	/**ÁªÏµÈËÐÅÏ¢Êý¾Ý¿â*/
	private class ContactDatabaseHelper extends SQLiteOpenHelper{

		public ContactDatabaseHelper(Context context){
			super(context, DB_NAME, null, DB_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("CREATE TABLE " + CONTACT_TABLE  + "("
					+ ContactColumns._ID+ " INTEGER PRIMARY KEY,"
					+ ContactColumns.AVATAR + " BLOB,"
					+ContactColumns.SORT + " TEXT,"
					+ContactColumns.NAME + " TEXT,"
					+ContactColumns.JID + " TEXT,"
					+ContactColumns.TYPE + " TEXT,"
					+ContactColumns.STATUS + " TEXT,"
					+ContactColumns.ACCOUNT + " TEXT);");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS "+ CONTACT_TABLE);
			onCreate(db);
		}
	}

	/**
	 * ÁªÏµÈËÊôÐÔ
	 *BaseColumnsÊÇ×Ô¶¨ÒåÁÐÃû£¬ ÀïÃæÓÐÁ½¸ö×Ö¶Î _id,_count,ÏÂÃæÊÇÀ©Õ¹
	 */
	public static class ContactColumns implements BaseColumns{
		//ÓÃ»§Í·Ïñ
		public static final String AVATAR = "avatar";
		//ÓÃ»§±¸×¢
		public static final String NAME = "name";
		//Ö÷ÈËµÄºÃÓÑ
		public static final String ACCOUNT = "account";
		//ºÃÓÑµÄÊ××ÖÄ¸
		public static final String SORT = "sort";
		//Ö÷ÈË
		public static final String JID = "jid";
		//ºÃÓÑÀàÐÍ(Ìí¼ÓºÃÓÑÊ±£ºboth£¬to£¬from)
		public static final String TYPE = "type";
		//ºÃÓÑ×´Ì¬£¨ÔÚÏß»¹ÊÇÀëÏß £©
		public static final String STATUS = "status";
		

	}
}
