/*
 * 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.data;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;
import android.text.TextUtils;

/**
 * 小米便签数据结构定义
 * 定义便签相关的URI、字段和常量
 * 支持便签、文件夹、提醒等多种数据类型
 */
public class Notes {
    // 内容提供者的URI前缀
    public static final String AUTHORITY = "net.micode.notes";
    // 内容提供者的完整URI
    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY);

    /**
     * 便签主表相关常量和字段
     */
    public interface NoteColumns extends BaseColumns {
        // 便签ID
        public static final String ID = BaseColumns._ID;
        // 父文件夹ID
        public static final String PARENT_ID = "parent_id";
        // 提醒日期时间戳
        public static final String ALERTED_DATE = "alerted_date";
        // 便签背景颜色ID
        public static final String BG_COLOR_ID = "bg_color_id";
        // 创建日期
        public static final String CREATED_DATE = "created_date";
        // 是否有附件
        public static final String HAS_ATTACHMENT = "has_attachment";
        // 修改日期
        public static final String MODIFIED_DATE = "modified_date";
        // 便签数量（文件夹才有）
        public static final String NOTES_COUNT = "notes_count";
        // 便签摘要
        public static final String SNIPPET = "snippet";
        // 便签类型
        public static final String TYPE = "type";
        // 桌面小部件ID
        public static final String WIDGET_ID = "widget_id";
        // 桌面小部件类型
        public static final String WIDGET_TYPE = "widget_type";
        // 同步ID
        public static final String SYNC_ID = "sync_id";
        // 本地修改标记
        public static final String LOCAL_MODIFIED = "local_modified";
        // 原始父文件夹ID
        public static final String ORIGIN_PARENT_ID = "origin_parent_id";
        // Google任务ID
        public static final String GTASK_ID = "gtask_id";
        // 版本号
        public static final String VERSION = "version";
    }

    /**
     * 便签数据关联表相关常量和字段
     */
    public interface DataColumns extends BaseColumns {
        // 数据ID
        public static final String ID = BaseColumns._ID;
        // MIME类型，标识数据类型
        public static final String MIME_TYPE = "mime_type";
        // 关联的便签ID
        public static final String NOTE_ID = "note_id";
        // 创建日期
        public static final String CREATED_DATE = "created_date";
        // 修改日期
        public static final String MODIFIED_DATE = "modified_date";
        // 内容文本
        public static final String CONTENT = "content";
        // 附加数据1，可存储电话号码等
        public static final String DATA1 = "data1";
        // 附加数据2
        public static final String DATA2 = "data2";
        // 附加数据3，可存储链接地址等
        public static final String DATA3 = "data3";
        // 附加数据4
        public static final String DATA4 = "data4";
        // 附加数据5
        public static final String DATA5 = "data5";
    }

    /**
     * 便签类型常量
     */
    public interface TypeColumns {
        // 普通便签类型
        public static final int TYPE_NOTE = 0;
        // 文件夹类型
        public static final int TYPE_FOLDER = 1;
        // 系统文件夹类型
        public static final int TYPE_SYSTEM = 2;
    }

    /**
     * 数据MIME类型常量
     */
    public interface DataConstants {
        // 文本便签MIME类型
        public static final String NOTE = "vnd.android.cursor.item/vnd.micode.note";
        // 通话记录MIME类型
        public static final String CALL_RECORD = "vnd.android.cursor.item/vnd.micode.note.call";
        // 提醒MIME类型
        public static final String ALERT = "vnd.android.cursor.item/vnd.micode.note.alert";
        // 链接MIME类型
        public static final String URL = "vnd.android.cursor.item/vnd.micode.note.url";
        // 图片MIME类型
        public static final String IMAGE = "vnd.android.cursor.item/vnd.micode.note.image";
        // 音频MIME类型
        public static final String AUDIO = "vnd.android.cursor.item/vnd.micode.note.audio";
    }

    /**
     * 系统文件夹ID常量
     */
    public interface FolderConstants {
        // 通话记录文件夹ID
        public static final long ID_CALL_RECORD_FOLDER = 1;
        // 根文件夹ID
        public static final long ID_ROOT_FOLDER = 2;
        // 临时文件夹ID
        public static final long ID_TEMPARAY_FOLDER = 3;
        // 回收站文件夹ID
        public static final long ID_TRASH_FOLER = 4;
    }

    /**
     * 桌面小部件类型常量
     */
    public interface WidgetConstants {
        // 无小部件
        public static final int WIDGET_TYPE_NONE = -1;
        // 4x2小部件
        public static final int WIDGET_TYPE_INFORMATION = 0;
        // 4x4小部件
        public static final int WIDGET_TYPE_BIG = 1;
        // 1x1小部件
        public static final int WIDGET_TYPE_MINI = 2;
    }

    /**
     * 便签内容URI
     */
    public static final class NoteContent implements NoteColumns {
        public static final String CONTENT_DIRECTORY = "notes";
        public static final Uri CONTENT_URI = Uri.withAppendedPath(Notes.CONTENT_URI, CONTENT_DIRECTORY);
        public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.micode.note";
        public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.micode.note";

        /**
         * 构建便签URI
         */
        public static Uri buildUri(long id) {
            return ContentUris.withAppendedId(CONTENT_URI, id);
        }

        /**
         * 从URI获取便签ID
         */
        public static long getId(Uri uri) {
            return ContentUris.parseId(uri);
        }
    }

    /**
     * 便签数据内容URI
     */
    public static final class DataContent implements DataColumns {
        public static final String CONTENT_DIRECTORY = "data";
        public static final Uri CONTENT_URI = Uri.withAppendedPath(Notes.CONTENT_URI, CONTENT_DIRECTORY);
        public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.micode.note.data";
        public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.micode.note.data";

        /**
         * 构建数据URI
         */
        public static Uri buildUri(long id) {
            return ContentUris.withAppendedId(CONTENT_URI, id);
        }

        /**
         * 从URI获取数据ID
         */
        public static long getId(Uri uri) {
            return ContentUris.parseId(uri);
        }

        /**
         * 构建特定便签的数据URI
         */
        public static Uri buildDataDirUri(long noteId) {
            return Uri.withAppendedPath(buildUri(noteId), CONTENT_DIRECTORY);
        }

        /**
         * 构建特定类型的数据URI
         */
        public static Uri buildDataUri(long noteId, String mimeType) {
            return Uri.withAppendedPath(buildDataDirUri(noteId), mimeType);
        }
    }

    /**
     * 文件夹内容URI
     */
    public static final class FolderContent implements NoteColumns {
        public static final String CONTENT_DIRECTORY = "folders";
        public static final Uri CONTENT_URI = Uri.withAppendedPath(Notes.CONTENT_URI, CONTENT_DIRECTORY);
        public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.micode.note.folder";
        public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.micode.note.folder";

        /**
         * 构建文件夹URI
         */
        public static Uri buildUri(long id) {
            return ContentUris.withAppendedId(CONTENT_URI, id);
        }

        /**
         * 从URI获取文件夹ID
         */
        public static long getId(Uri uri) {
            return ContentUris.parseId(uri);
        }
    }

    /**
     * 便签工具类
     */
    public static class Utils {
        /**
         * 创建新便签
         */
        public static long createNewNote(ContentResolver resolver, long parentId, String snippet) {
            ContentValues values = new ContentValues();
            values.put(NoteColumns.PARENT_ID, parentId);
            values.put(NoteColumns.SNIPPET, snippet);
            values.put(NoteColumns.TYPE, TypeColumns.TYPE_NOTE);
            Uri uri = resolver.insert(NoteContent.CONTENT_URI, values);
            if (uri != null) {
                return ContentUris.parseId(uri);
            }
            return -1;
        }

        /**
         * 创建新文件夹
         */
        public static long createNewFolder(ContentResolver resolver, long parentId, String name) {
            ContentValues values = new ContentValues();
            values.put(NoteColumns.PARENT_ID, parentId);
            values.put(NoteColumns.SNIPPET, name);
            values.put(NoteColumns.TYPE, TypeColumns.TYPE_FOLDER);
            Uri uri = resolver.insert(NoteContent.CONTENT_URI, values);
            if (uri != null) {
                return ContentUris.parseId(uri);
            }
            return -1;
        }

        /**
         * 添加便签文本内容
         */
        public static long addTextNote(ContentResolver resolver, long noteId, String content) {
            ContentValues values = new ContentValues();
            values.put(DataColumns.NOTE_ID, noteId);
            values.put(DataColumns.MIME_TYPE, DataConstants.NOTE);
            values.put(DataColumns.CONTENT, content);
            Uri uri = resolver.insert(DataContent.CONTENT_URI, values);
            if (uri != null) {
                return ContentUris.parseId(uri);
            }
            return -1;
        }

        /**
         * 添加便签提醒
         */
        public static long addAlert(ContentResolver resolver, long noteId, long alertDate) {
            ContentValues values = new ContentValues();
            values.put(DataColumns.NOTE_ID, noteId);
            values.put(DataColumns.MIME_TYPE, DataConstants.ALERT);
            values.put(DataColumns.DATA1, alertDate);
            Uri uri = resolver.insert(DataContent.CONTENT_URI, values);
            if (uri != null) {
                return ContentUris.parseId(uri);
            }
            return -1;
        }

        /**
         * 添加便签链接
         */
        public static long addUrl(ContentResolver resolver, long noteId, String url, String title) {
            ContentValues values = new ContentValues();
            values.put(DataColumns.NOTE_ID, noteId);
            values.put(DataColumns.MIME_TYPE, DataConstants.URL);
            values.put(DataColumns.DATA1, url);
            values.put(DataColumns.CONTENT, title);
            Uri uri = resolver.insert(DataContent.CONTENT_URI, values);
            if (uri != null) {
                return ContentUris.parseId(uri);
            }
            return -1;
        }

        /**
         * 添加便签通话记录
         */
        public static long addCallRecord(ContentResolver resolver, long noteId, String phoneNumber, String callTime) {
            ContentValues values = new ContentValues();
            values.put(DataColumns.NOTE_ID, noteId);
            values.put(DataColumns.MIME_TYPE, DataConstants.CALL_RECORD);
            values.put(DataColumns.DATA1, phoneNumber);
            values.put(DataColumns.CONTENT, callTime);
            Uri uri = resolver.insert(DataContent.CONTENT_URI, values);
            if (uri != null) {
                return ContentUris.parseId(uri);
            }
            return -1;
        }

        /**
         * 检查是否为系统文件夹
         */
        public static boolean isSystemFolder(long folderId) {
            return folderId == FolderConstants.ID_CALL_RECORD_FOLDER ||
                   folderId == FolderConstants.ID_ROOT_FOLDER ||
                   folderId == FolderConstants.ID_TEMPARAY_FOLDER ||
                   folderId == FolderConstants.ID_TRASH_FOLER;
        }

        /**
         * 检查是否为回收站
         */
        public static boolean isTrashFolder(long folderId) {
            return folderId == FolderConstants.ID_TRASH_FOLER;
        }

        /**
         * 检查是否为通话记录文件夹
         */
        public static boolean isCallRecordFolder(long folderId) {
            return folderId == FolderConstants.ID_CALL_RECORD_FOLDER;
        }

        /**
         * 检查便签是否有提醒
         */
        public static boolean hasAlert(ContentResolver resolver, long noteId) {
            String[] projection = new String[] { DataColumns.ID };
            String selection = DataColumns.NOTE_ID + " = ? AND " + 
                              DataColumns.MIME_TYPE + " = ?";
            String[] selectionArgs = new String[] { 
                String.valueOf(noteId), 
                DataConstants.ALERT 
            };
            
            Cursor cursor = resolver.query(
                DataContent.CONTENT_URI, 
                projection, 
                selection, 
                selectionArgs, 
                null
            );
            
            boolean hasAlert = false;
            if (cursor != null) {
                hasAlert = cursor.getCount() > 0;
                cursor.close();
            }
            
            return hasAlert;
        }

        /**
         * 获取便签的提醒时间
         */
        public static long getAlertDate(ContentResolver resolver, long noteId) {
            String[] projection = new String[] { DataColumns.DATA1 };
            String selection = DataColumns.NOTE_ID + " = ? AND " + 
                              DataColumns.MIME_TYPE + " = ?";
            String[] selectionArgs = new String[] { 
                String.valueOf(noteId), 
                DataConstants.ALERT 
            };
            
            Cursor cursor = resolver.query(
                DataContent.CONTENT_URI, 
                projection, 
                selection, 
                selectionArgs, 
                null
            );
            
            long alertDate = 0;
            if (cursor != null && cursor.moveToFirst()) {
                alertDate = cursor.getLong(0);
                cursor.close();
            }
            
            return alertDate;
        }

        /**
         * 检查便签是否包含链接
         */
        public static boolean hasUrl(ContentResolver resolver, long noteId) {
            String[] projection = new String[] { DataColumns.ID };
            String selection = DataColumns.NOTE_ID + " = ? AND " + 
                              DataColumns.MIME_TYPE + " = ?";
            String[] selectionArgs = new String[] { 
                String.valueOf(noteId), 
                DataConstants.URL 
            };
            
            Cursor cursor = resolver.query(
                DataContent.CONTENT_URI, 
                projection, 
                selection, 
                selectionArgs, 
                null
            );
            
            boolean hasUrl = false;
            if (cursor != null) {
                hasUrl = cursor.getCount() > 0;
                cursor.close();
            }
            
            return hasUrl;
        }

        /**
         * 检查便签是否包含通话记录
         */
        public static boolean hasCallRecord(ContentResolver resolver, long noteId) {
            String[] projection = new String[] { DataColumns.ID };
            String selection = DataColumns.NOTE_ID + " = ? AND " + 
                              DataColumns.MIME_TYPE + " = ?";
            String[] selectionArgs = new String[] { 
                String.valueOf(noteId), 
                DataConstants.CALL_RECORD 
            };
            
            Cursor cursor = resolver.query(
                DataContent.CONTENT_URI, 
                projection, 
                selection, 
                selectionArgs, 
                null
            );
            
            boolean hasCallRecord = false;
            if (cursor != null) {
                hasCallRecord = cursor.getCount() > 0;
                cursor.close();
            }
            
            return hasCallRecord;
        }
    }
}
