/*
 * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.micode.notes.provider;

import android.content.ContentProvider;
import android.content.ContentResolver;
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.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

import net.micode.notes.data.Notes;
import net.micode.notes.data.NotesDatabaseHelper;

import java.util.HashMap;

/**
 * 小米便签内容提供者
 * 负责处理便签数据的CRUD操作
 * 提供内容URI接口给外部应用访问便签数据
 */
public class NotesProvider extends ContentProvider {
    // 日志标签
    private static final String TAG = "NotesProvider";
    // 数据库帮助类
    private NotesDatabaseHelper mOpenHelper;
    // 内容解析器
    private ContentResolver mContentResolver;

    // URI匹配器常量
    private static final int NOTES = 1;
    private static final int NOTE_ID = 2;
    private static final int FOLDERS = 3;
    private static final int FOLDER_ID = 4;
    private static final int DATA = 5;
    private static final int DATA_ID = 6;
    private static final int DATA_NOTE_ID = 7;
    private static final int DATA_NOTE_ID_MIME_TYPE = 8;

    // URI匹配器
    private static final UriMatcher sUriMatcher;
    // 便签查询投影映射
    private static HashMap<String, String> sNotesProjectionMap;
    // 数据查询投影映射
    private static HashMap<String, String> sDataProjectionMap;

    static {
        // 初始化URI匹配器
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        sUriMatcher.addURI(Notes.AUTHORITY, "notes", NOTES);
        sUriMatcher.addURI(Notes.AUTHORITY, "notes/#", NOTE_ID);
        sUriMatcher.addURI(Notes.AUTHORITY, "folders", FOLDERS);
        sUriMatcher.addURI(Notes.AUTHORITY, "folders/#", FOLDER_ID);
        sUriMatcher.addURI(Notes.AUTHORITY, "data", DATA);
        sUriMatcher.addURI(Notes.AUTHORITY, "data/#", DATA_ID);
        sUriMatcher.addURI(Notes.AUTHORITY, "data/#/data", DATA_NOTE_ID);
        sUriMatcher.addURI(Notes.AUTHORITY, "data/#/data/*", DATA_NOTE_ID_MIME_TYPE);

        // 初始化便签查询投影映射
        sNotesProjectionMap = new HashMap<String, String>();
        sNotesProjectionMap.put(Notes.NoteColumns.ID, Notes.NoteColumns.ID);
        sNotesProjectionMap.put(Notes.NoteColumns.PARENT_ID, Notes.NoteColumns.PARENT_ID);
        sNotesProjectionMap.put(Notes.NoteColumns.ALERTED_DATE, Notes.NoteColumns.ALERTED_DATE);
        sNotesProjectionMap.put(Notes.NoteColumns.BG_COLOR_ID, Notes.NoteColumns.BG_COLOR_ID);
        sNotesProjectionMap.put(Notes.NoteColumns.CREATED_DATE, Notes.NoteColumns.CREATED_DATE);
        sNotesProjectionMap.put(Notes.NoteColumns.HAS_ATTACHMENT, Notes.NoteColumns.HAS_ATTACHMENT);
        sNotesProjectionMap.put(Notes.NoteColumns.MODIFIED_DATE, Notes.NoteColumns.MODIFIED_DATE);
        sNotesProjectionMap.put(Notes.NoteColumns.NOTES_COUNT, Notes.NoteColumns.NOTES_COUNT);
        sNotesProjectionMap.put(Notes.NoteColumns.SNIPPET, Notes.NoteColumns.SNIPPET);
        sNotesProjectionMap.put(Notes.NoteColumns.TYPE, Notes.NoteColumns.TYPE);
        sNotesProjectionMap.put(Notes.NoteColumns.WIDGET_ID, Notes.NoteColumns.WIDGET_ID);
        sNotesProjectionMap.put(Notes.NoteColumns.WIDGET_TYPE, Notes.NoteColumns.WIDGET_TYPE);
        sNotesProjectionMap.put(Notes.NoteColumns.SYNC_ID, Notes.NoteColumns.SYNC_ID);
        sNotesProjectionMap.put(Notes.NoteColumns.LOCAL_MODIFIED, Notes.NoteColumns.LOCAL_MODIFIED);
        sNotesProjectionMap.put(Notes.NoteColumns.ORIGIN_PARENT_ID, Notes.NoteColumns.ORIGIN_PARENT_ID);
        sNotesProjectionMap.put(Notes.NoteColumns.GTASK_ID, Notes.NoteColumns.GTASK_ID);
        sNotesProjectionMap.put(Notes.NoteColumns.VERSION, Notes.NoteColumns.VERSION);

        // 初始化数据查询投影映射
        sDataProjectionMap = new HashMap<String, String>();
        sDataProjectionMap.put(Notes.DataColumns.ID, Notes.DataColumns.ID);
        sDataProjectionMap.put(Notes.DataColumns.MIME_TYPE, Notes.DataColumns.MIME_TYPE);
        sDataProjectionMap.put(Notes.DataColumns.NOTE_ID, Notes.DataColumns.NOTE_ID);
        sDataProjectionMap.put(Notes.DataColumns.CREATED_DATE, Notes.DataColumns.CREATED_DATE);
        sDataProjectionMap.put(Notes.DataColumns.MODIFIED_DATE, Notes.DataColumns.MODIFIED_DATE);
        sDataProjectionMap.put(Notes.DataColumns.CONTENT, Notes.DataColumns.CONTENT);
        sDataProjectionMap.put(Notes.DataColumns.DATA1, Notes.DataColumns.DATA1);
        sDataProjectionMap.put(Notes.DataColumns.DATA2, Notes.DataColumns.DATA2);
        sDataProjectionMap.put(Notes.DataColumns.DATA3, Notes.DataColumns.DATA3);
        sDataProjectionMap.put(Notes.DataColumns.DATA4, Notes.DataColumns.DATA4);
        sDataProjectionMap.put(Notes.DataColumns.DATA5, Notes.DataColumns.DATA5);
    }

    /**
     * 初始化内容提供者
     */
    @Override
    public boolean onCreate() {
        mOpenHelper = NotesDatabaseHelper.getInstance(getContext());
        mContentResolver = getContext().getContentResolver();
        return true;
    }

    /**
     * 查询便签数据
     */
    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        String groupBy = null;

        // 根据URI匹配结果设置查询条件
        switch (sUriMatcher.match(uri)) {
            case NOTES:
                qb.setTables(NotesDatabaseHelper.TABLE.NOTE);
                qb.setProjectionMap(sNotesProjectionMap);
                break;

            case NOTE_ID:
                qb.setTables(NotesDatabaseHelper.TABLE.NOTE);
                qb.setProjectionMap(sNotesProjectionMap);
                qb.appendWhere(Notes.NoteColumns.ID + "=" + uri.getPathSegments().get(1));
                break;

            case FOLDERS:
                qb.setTables(NotesDatabaseHelper.TABLE.NOTE);
                qb.setProjectionMap(sNotesProjectionMap);
                qb.appendWhere(Notes.NoteColumns.TYPE + "=" + Notes.TypeColumns.TYPE_FOLDER);
                break;

            case FOLDER_ID:
                qb.setTables(NotesDatabaseHelper.TABLE.NOTE);
                qb.setProjectionMap(sNotesProjectionMap);
                qb.appendWhere(Notes.NoteColumns.ID + "=" + uri.getPathSegments().get(1));
                break;

            case DATA:
                qb.setTables(NotesDatabaseHelper.TABLE.DATA);
                qb.setProjectionMap(sDataProjectionMap);
                break;

            case DATA_ID:
                qb.setTables(NotesDatabaseHelper.TABLE.DATA);
                qb.setProjectionMap(sDataProjectionMap);
                qb.appendWhere(Notes.DataColumns.ID + "=" + uri.getPathSegments().get(1));
                break;

            case DATA_NOTE_ID:
                qb.setTables(NotesDatabaseHelper.TABLE.DATA);
                qb.setProjectionMap(sDataProjectionMap);
                qb.appendWhere(Notes.DataColumns.NOTE_ID + "=" + uri.getPathSegments().get(1));
                break;

            case DATA_NOTE_ID_MIME_TYPE:
                qb.setTables(NotesDatabaseHelper.TABLE.DATA);
                qb.setProjectionMap(sDataProjectionMap);
                qb.appendWhere(Notes.DataColumns.NOTE_ID + "=" + uri.getPathSegments().get(1));
                qb.appendWhere(" AND " + Notes.DataColumns.MIME_TYPE + "='" + 
                              uri.getPathSegments().get(3) + "'");
                break;

            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }

        // 如果没有指定排序方式，使用默认排序
        String orderBy;
        if (TextUtils.isEmpty(sortOrder)) {
            orderBy = Notes.NoteColumns.MODIFIED_DATE + " DESC";
        } else {
            orderBy = sortOrder;
        }

        // 获取数据库并执行查询
        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        Cursor c = qb.query(db, projection, selection, selectionArgs, groupBy, null, orderBy);

        // 设置通知URI，当数据变化时通知ContentResolver
        c.setNotificationUri(mContentResolver, uri);
        return c;
    }

    /**
     * 获取内容类型
     */
    @Override
    public String getType(Uri uri) {
        switch (sUriMatcher.match(uri)) {
            case NOTES:
                return Notes.NoteContent.CONTENT_TYPE;
            case NOTE_ID:
                return Notes.NoteContent.CONTENT_ITEM_TYPE;
            case FOLDERS:
                return Notes.FolderContent.CONTENT_TYPE;
            case FOLDER_ID:
                return Notes.FolderContent.CONTENT_ITEM_TYPE;
            case DATA:
                return Notes.DataContent.CONTENT_TYPE;
            case DATA_ID:
                return Notes.DataContent.CONTENT_ITEM_TYPE;
            case DATA_NOTE_ID:
                return Notes.DataContent.CONTENT_TYPE;
            case DATA_NOTE_ID_MIME_TYPE:
                return Notes.DataContent.CONTENT_TYPE;
            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }
    }

    /**
     * 插入便签数据
     */
    @Override
    public Uri insert(Uri uri, ContentValues initialValues) {
        // 确保ContentValues不为空
        ContentValues values;
        if (initialValues != null) {
            values = new ContentValues(initialValues);
        } else {
            values = new ContentValues();
        }

        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        long rowId;
        Uri baseUri;

        // 根据URI匹配结果执行不同的插入操作
        switch (sUriMatcher.match(uri)) {
            case NOTES:
                // 处理父文件夹ID为空的情况
                if (!values.containsKey(Notes.NoteColumns.PARENT_ID)) {
                    values.put(Notes.NoteColumns.PARENT_ID, Notes.FolderConstants.ID_ROOT_FOLDER);
                }
                
                // 处理类型为空的情况
                if (!values.containsKey(Notes.NoteColumns.TYPE)) {
                    values.put(Notes.NoteColumns.TYPE, Notes.TypeColumns.TYPE_NOTE);
                }
                
                // 处理便签摘要为空的情况
                if (!values.containsKey(Notes.NoteColumns.SNIPPET)) {
                    values.put(Notes.NoteColumns.SNIPPET, "");
                }
                
                // 插入便签数据
                rowId = db.insert(NotesDatabaseHelper.TABLE.NOTE, null, values);
                if (rowId > 0) {
                    baseUri = Notes.NoteContent.CONTENT_URI;
                    Uri newUri = ContentUris.withAppendedId(baseUri, rowId);
                    mContentResolver.notifyChange(newUri, null);
                    return newUri;
                }
                break;

            case DATA:
                // 处理关联便签ID为空的情况
                if (!values.containsKey(Notes.DataColumns.NOTE_ID)) {
                    throw new IllegalArgumentException("Note ID is required for data insertion");
                }
                
                // 处理MIME类型为空的情况
                if (!values.containsKey(Notes.DataColumns.MIME_TYPE)) {
                    throw new IllegalArgumentException("MIME type is required for data insertion");
                }
                
                // 插入便签数据关联
                rowId = db.insert(NotesDatabaseHelper.TABLE.DATA, null, values);
                if (rowId > 0) {
                    baseUri = Notes.DataContent.CONTENT_URI;
                    Uri newUri = ContentUris.withAppendedId(baseUri, rowId);
                    
                    // 如果是提醒类型，更新便签的提醒日期
                    if (Notes.DataConstants.ALERT.equals(values.getAsString(Notes.DataColumns.MIME_TYPE))) {
                        long alertDate = values.getAsLong(Notes.DataColumns.DATA1);
                        updateAlertDate(values.getAsLong(Notes.DataColumns.NOTE_ID), alertDate);
                    }
                    
                    mContentResolver.notifyChange(newUri, null);
                    return newUri;
                }
                break;

            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }

        throw new IllegalArgumentException("Failed to insert row into " + uri);
    }

    /**
     * 更新便签数据
     */
    @Override
    public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        int count;
        String finalWhere;

        // 根据URI匹配结果执行不同的更新操作
        switch (sUriMatcher.match(uri)) {
            case NOTES:
                count = db.update(NotesDatabaseHelper.TABLE.NOTE, values, where, whereArgs);
                break;

            case NOTE_ID:
                finalWhere = Notes.NoteColumns.ID + "=" + uri.getPathSegments().get(1);
                if (where != null) {
                    finalWhere = finalWhere + " AND " + where;
                }
                
                // 如果更新了便签类型，处理特殊情况
                if (values.containsKey(Notes.NoteColumns.TYPE)) {
                    int type = values.getAsInteger(Notes.NoteColumns.TYPE);
                    if (type == Notes.TypeColumns.TYPE_SYSTEM) {
                        throw new IllegalArgumentException("Can't set note type to system type");
                    }
                }
                
                count = db.update(NotesDatabaseHelper.TABLE.NOTE, values, finalWhere, whereArgs);
                break;

            case DATA:
                count = db.update(NotesDatabaseHelper.TABLE.DATA, values, where, whereArgs);
                break;

            case DATA_ID:
                finalWhere = Notes.DataColumns.ID + "=" + uri.getPathSegments().get(1);
                if (where != null) {
                    finalWhere = finalWhere + " AND " + where;
                }
                
                // 如果更新了提醒数据，更新便签的提醒日期
                if (values.containsKey(Notes.DataColumns.MIME_TYPE) && 
                    Notes.DataConstants.ALERT.equals(values.getAsString(Notes.DataColumns.MIME_TYPE))) {
                    long alertDate = values.getAsLong(Notes.DataColumns.DATA1);
                    updateAlertDate(getNoteIdFromDataId(ContentUris.parseId(uri)), alertDate);
                }
                
                count = db.update(NotesDatabaseHelper.TABLE.DATA, values, finalWhere, whereArgs);
                break;

            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }

        // 通知内容解析器数据已更改
        mContentResolver.notifyChange(uri, null);
        return count;
    }

    /**
     * 删除便签数据
     */
    @Override
    public int delete(Uri uri, String where, String[] whereArgs) {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        int count;
        String finalWhere;

        // 根据URI匹配结果执行不同的删除操作
        switch (sUriMatcher.match(uri)) {
            case NOTES:
                // 不能删除系统文件夹
                finalWhere = Notes.NoteColumns.TYPE + "!=" + Notes.TypeColumns.TYPE_SYSTEM;
                if (where != null) {
                    finalWhere = finalWhere + " AND " + where;
                }
                count = db.delete(NotesDatabaseHelper.TABLE.NOTE, finalWhere, whereArgs);
                break;

            case NOTE_ID:
                // 不能删除系统文件夹
                finalWhere = Notes.NoteColumns.ID + "=" + uri.getPathSegments().get(1) +
                        " AND " + Notes.NoteColumns.TYPE + "!=" + Notes.TypeColumns.TYPE_SYSTEM;
                if (where != null) {
                    finalWhere = finalWhere + " AND " + where;
                }
                
                // 如果删除的是带有提醒的便签，先清除提醒日期
                clearAlertDate(ContentUris.parseId(uri));
                
                count = db.delete(NotesDatabaseHelper.TABLE.NOTE, finalWhere, whereArgs);
                break;

            case DATA:
                count = db.delete(NotesDatabaseHelper.TABLE.DATA, where, whereArgs);
                break;

            case DATA_ID:
                finalWhere = Notes.DataColumns.ID + "=" + uri.getPathSegments().get(1);
                if (where != null) {
                    finalWhere = finalWhere + " AND " + where;
                }
                
                // 如果删除的是提醒数据，清除便签的提醒日期
                clearAlertDate(getNoteIdFromDataId(ContentUris.parseId(uri)));
                
                count = db.delete(NotesDatabaseHelper.TABLE.DATA, finalWhere, whereArgs);
                break;

            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }

        // 通知内容解析器数据已更改
        mContentResolver.notifyChange(uri, null);
        return count;
    }

    /**
     * 更新便签的提醒日期
     */
    private void updateAlertDate(long noteId, long alertDate) {
        ContentValues values = new ContentValues();
        values.put(Notes.NoteColumns.ALERTED_DATE, alertDate);
        values.put(Notes.NoteColumns.LOCAL_MODIFIED, 1);
        
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        db.update(NotesDatabaseHelper.TABLE.NOTE, values, 
                Notes.NoteColumns.ID + "=" + noteId, null);
    }

    /**
     * 清除便签的提醒日期
     */
    private void clearAlertDate(long noteId) {
        ContentValues values = new ContentValues();
        values.put(Notes.NoteColumns.ALERTED_DATE, 0);
        values.put(Notes.NoteColumns.LOCAL_MODIFIED, 1);
        
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        db.update(NotesDatabaseHelper.TABLE.NOTE, values, 
                Notes.NoteColumns.ID + "=" + noteId, null);
    }

    /**
     * 从数据ID获取关联的便签ID
     */
    private long getNoteIdFromDataId(long dataId) {
        String[] projection = new String[] { Notes.DataColumns.NOTE_ID };
        String selection = Notes.DataColumns.ID + " = ?";
        String[] selectionArgs = new String[] { String.valueOf(dataId) };
        
        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        Cursor cursor = db.query(NotesDatabaseHelper.TABLE.DATA, projection, 
                selection, selectionArgs, null, null, null);
        
        long noteId = -1;
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                noteId = cursor.getLong(0);
            }
            cursor.close();
        }
        
        return noteId;
    }
}
