/*
 * 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.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import net.micode.notes.data.Notes.DataColumns;
import net.micode.notes.data.Notes.DataConstants;
import net.micode.notes.data.Notes.NoteColumns;

/**
 * NotesDatabaseHelper类用于管理便签应用的数据库创建和版本升级。
 */
public class NotesDatabaseHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "note.db"; // 数据库名称
    private static final int DB_VERSION = 4; // 数据库版本

    /**
     * 数据库表名接口
     */
    public interface TABLE {
        public static final String NOTE = "note"; // 便签表
        public static final String DATA = "data"; // 数据表
    }

    private static final String TAG = "NotesDatabaseHelper"; // 日志标签
    private static NotesDatabaseHelper mInstance; // 单例实例

    /**
     * 创建便签表的SQL语句
     */
    private static final String CREATE_NOTE_TABLE_SQL =
        "CREATE TABLE " + TABLE.NOTE + "(" +
            NoteColumns.ID + " INTEGER PRIMARY KEY," + // 便签ID
            NoteColumns.PARENT_ID + " INTEGER NOT NULL DEFAULT 0," + // 父ID
            NoteColumns.ALERTED_DATE + " INTEGER NOT NULL DEFAULT 0," + // 提醒日期
            NoteColumns.BG_COLOR_ID + " INTEGER NOT NULL DEFAULT 0," + // 背景颜色ID
            NoteColumns.CREATED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + // 创建日期
            NoteColumns.HAS_ATTACHMENT + " INTEGER NOT NULL DEFAULT 0," + // 是否有附件
            NoteColumns.MODIFIED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + // 修改日期
            NoteColumns.NOTES_COUNT + " INTEGER NOT NULL DEFAULT 0," + // 便签数量
            NoteColumns.SNIPPET + " TEXT NOT NULL DEFAULT ''," + // 便签摘要
            NoteColumns.TYPE + " INTEGER NOT NULL DEFAULT 0," + // 便签类型
            NoteColumns.WIDGET_ID + " INTEGER NOT NULL DEFAULT 0," + // 小部件ID
            NoteColumns.WIDGET_TYPE + " INTEGER NOT NULL DEFAULT -1," + // 小部件类型
            NoteColumns.SYNC_ID + " INTEGER NOT NULL DEFAULT 0," + // 同步ID
            NoteColumns.LOCAL_MODIFIED + " INTEGER NOT NULL DEFAULT 0," + // 本地修改标志
            NoteColumns.ORIGIN_PARENT_ID + " INTEGER NOT NULL DEFAULT 0," + // 原始父ID
            NoteColumns.GTASK_ID + " TEXT NOT NULL DEFAULT ''," + // Google任务ID
            NoteColumns.VERSION + " INTEGER NOT NULL DEFAULT 0" + // 版本号
        ")";

    /**
     * 创建数据表的SQL语句
     */
    private static final String CREATE_DATA_TABLE_SQL =
        "CREATE TABLE " + TABLE.DATA + "(" +
            DataColumns.ID + " INTEGER PRIMARY KEY," + // 数据ID
            DataColumns.MIME_TYPE + " TEXT NOT NULL," + // MIME类型
            DataColumns.NOTE_ID + " INTEGER NOT NULL DEFAULT 0," + // 便签ID
            NoteColumns.CREATED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + // 创建日期
            NoteColumns.MODIFIED_DATE + " INTEGER NOT NULL DEFAULT (strftime('%s','now') * 1000)," + // 修改日期
            DataColumns.CONTENT + " TEXT NOT NULL DEFAULT ''," + // 数据内容
            DataColumns.DATA1 + " INTEGER," + // 数据1
            DataColumns.DATA2 + " INTEGER," + // 数据2
            DataColumns.DATA3 + " TEXT NOT NULL DEFAULT ''," + // 数据3
            DataColumns.DATA4 + " TEXT NOT NULL DEFAULT ''," + // 数据4
            DataColumns.DATA5 + " TEXT NOT NULL DEFAULT ''" + // 数据5
        ")";

    /**
     * 创建数据表便签ID索引的SQL语句
     */
    private static final String CREATE_DATA_NOTE_ID_INDEX_SQL =
        "CREATE INDEX IF NOT EXISTS note_id_index ON " +
        TABLE.DATA + "(" + DataColumns.NOTE_ID + ");";

    // 定义触发器SQL语句...

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

    /**
     * 创建便签表
     */
    public void createNoteTable(SQLiteDatabase db) {
        db.execSQL(CREATE_NOTE_TABLE_SQL);
        reCreateNoteTableTriggers(db);
        createSystemFolder(db);
        Log.d(TAG, "note table has been created");
    }

    /**
     * 重新创建便签表触发器
     */
    private void reCreateNoteTableTriggers(SQLiteDatabase db) {
        // 删除旧触发器...
        // 创建新触发器...
    }

    /**
     * 创建系统文件夹
     */
    private void createSystemFolder(SQLiteDatabase db) {
        ContentValues values = new ContentValues();
        // 插入系统文件夹...
    }

    /**
     * 创建数据表
     */
    public void createDataTable(SQLiteDatabase db) {
        db.execSQL(CREATE_DATA_TABLE_SQL);
        reCreateDataTableTriggers(db);
        db.execSQL(CREATE_DATA_NOTE_ID_INDEX_SQL);
        Log.d(TAG, "data table has been created");
    }

    /**
     * 重新创建数据表触发器
     */
    private void reCreateDataTableTriggers(SQLiteDatabase db) {
        // 删除旧触发器...
        // 创建新触发器...
    }

    /**
     * 获取单例实例
     */
    static synchronized NotesDatabaseHelper getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new NotesDatabaseHelper(context);
        }
        return mInstance;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        createNoteTable(db);
        createDataTable(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 升级数据库版本...
    }

    // 升级方法...
}
