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

import android.content.Context;
import android.database.Cursor;
import android.text.TextUtils;

import net.micode.notes.data.Contact;
import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.tool.DataUtils;

/**
 * 笔记项数据类
 * 用于存储和管理单个笔记项的所有相关数据
 */
public class NoteItemData {
    // 数据库查询的列定义
    static final String [] PROJECTION = new String [] {
        NoteColumns.ID,                // 笔记ID
        NoteColumns.ALERTED_DATE,      // 提醒日期
        NoteColumns.BG_COLOR_ID,       // 背景颜色ID
        NoteColumns.CREATED_DATE,      // 创建日期
        NoteColumns.HAS_ATTACHMENT,    // 是否有附件
        NoteColumns.MODIFIED_DATE,     // 修改日期
        NoteColumns.NOTES_COUNT,       // 笔记数量
        NoteColumns.PARENT_ID,         // 父文件夹ID
        NoteColumns.SNIPPET,           // 笔记摘要
        NoteColumns.TYPE,              // 笔记类型
        NoteColumns.WIDGET_ID,         // 小部件ID
        NoteColumns.WIDGET_TYPE,       // 小部件类型
    };

    // 查询结果列的索引常量
    private static final int ID_COLUMN = 0;
    private static final int ALERTED_DATE_COLUMN = 1;
    private static final int BG_COLOR_ID_COLUMN = 2;
    private static final int CREATED_DATE_COLUMN = 3;
    private static final int HAS_ATTACHMENT_COLUMN = 4;
    private static final int MODIFIED_DATE_COLUMN = 5;
    private static final int NOTES_COUNT_COLUMN = 6;
    private static final int PARENT_ID_COLUMN = 7;
    private static final int SNIPPET_COLUMN = 8;
    private static final int TYPE_COLUMN = 9;
    private static final int WIDGET_ID_COLUMN = 10;
    private static final int WIDGET_TYPE_COLUMN = 11;

    // 笔记基本属性
    private long mId;                  // 笔记ID
    private long mAlertDate;           // 提醒时间
    private int mBgColorId;            // 背景颜色
    private long mCreatedDate;         // 创建时间
    private boolean mHasAttachment;    // 是否有附件
    private long mModifiedDate;        // 修改时间
    private int mNotesCount;           // 笔记数量
    private long mParentId;            // 父文件夹ID
    private String mSnippet;           // 笔记摘要
    private int mType;                 // 笔记类型
    private int mWidgetId;             // 小部件ID
    private int mWidgetType;           // 小部件类型
    private String mName;              // 联系人姓名(通话记录用)
    private String mPhoneNumber;       // 电话号码(通话记录用)

    // 笔记在列表中的位置状态
    private boolean mIsLastItem;               // 是否为最后一项
    private boolean mIsFirstItem;              // 是否为第一项
    private boolean mIsOnlyOneItem;            // 是否为唯一项
    private boolean mIsOneNoteFollowingFolder; // 是否为文件夹后的单个笔记
    private boolean mIsMultiNotesFollowingFolder; // 是否为文件夹后的多个笔记之一

    /**
     * 构造函数
     * @param context 上下文对象
     * @param cursor 数据库游标
     */
    public NoteItemData(Context context, Cursor cursor) {
        // 从游标中读取笔记数据
        mId = cursor.getLong(ID_COLUMN);
        mAlertDate = cursor.getLong(ALERTED_DATE_COLUMN);
        mBgColorId = cursor.getInt(BG_COLOR_ID_COLUMN);
        mCreatedDate = cursor.getLong(CREATED_DATE_COLUMN);
        mHasAttachment = (cursor.getInt(HAS_ATTACHMENT_COLUMN) > 0);
        mModifiedDate = cursor.getLong(MODIFIED_DATE_COLUMN);
        mNotesCount = cursor.getInt(NOTES_COUNT_COLUMN);
        mParentId = cursor.getLong(PARENT_ID_COLUMN);
        mSnippet = cursor.getString(SNIPPET_COLUMN);
        // 移除笔记内容中的复选框标记
        mSnippet = mSnippet.replace(NoteEditActivity.TAG_CHECKED, "").replace(
                NoteEditActivity.TAG_UNCHECKED, "");
        mType = cursor.getInt(TYPE_COLUMN);
        mWidgetId = cursor.getInt(WIDGET_ID_COLUMN);
        mWidgetType = cursor.getInt(WIDGET_TYPE_COLUMN);

        // 处理通话记录相关信息
        mPhoneNumber = "";
        if (mParentId == Notes.ID_CALL_RECORD_FOLDER) {
            mPhoneNumber = DataUtils.getCallNumberByNoteId(context.getContentResolver(), mId);
            if (!TextUtils.isEmpty(mPhoneNumber)) {
                mName = Contact.getContact(context, mPhoneNumber);
                if (mName == null) {
                    mName = mPhoneNumber;
                }
            }
        }

        if (mName == null) {
            mName = "";
        }
        // 检查笔记在列表中的位置
        checkPostion(cursor);
    }

    /**
     * 检查笔记在列表中的位置状态
     * @param cursor 数据库游标
     */
    private void checkPostion(Cursor cursor) {
        mIsLastItem = cursor.isLast();
        mIsFirstItem = cursor.isFirst();
        mIsOnlyOneItem = (cursor.getCount() == 1);
        mIsMultiNotesFollowingFolder = false;
        mIsOneNoteFollowingFolder = false;

        // 检查是否为文件夹后的笔记
        if (mType == Notes.TYPE_NOTE && !mIsFirstItem) {
            int position = cursor.getPosition();
            if (cursor.moveToPrevious()) {
                if (cursor.getInt(TYPE_COLUMN) == Notes.TYPE_FOLDER
                        || cursor.getInt(TYPE_COLUMN) == Notes.TYPE_SYSTEM) {
                    if (cursor.getCount() > (position + 1)) {
                        mIsMultiNotesFollowingFolder = true;
                    } else {
                        mIsOneNoteFollowingFolder = true;
                    }
                }
                if (!cursor.moveToNext()) {
                    throw new IllegalStateException("cursor move to previous but can't move back");
                }
            }
        }
    }

    // 以下是各种getter方法

    /**
     * 判断是否为文件夹后的单个笔记
     */
    public boolean isOneFollowingFolder() {
        return mIsOneNoteFollowingFolder;
    }

    /**
     * 判断是否为文件夹后的多个笔记之一
     */
    public boolean isMultiFollowingFolder() {
        return mIsMultiNotesFollowingFolder;
    }

    /**
     * 判断是否为最后一项
     */
    public boolean isLast() {
        return mIsLastItem;
    }

    /**
     * 获取通话记录联系人名称
     */
    public String getCallName() {
        return mName;
    }

    /**
     * 判断是否为第一项
     */
    public boolean isFirst() {
        return mIsFirstItem;
    }

    /**
     * 判断是否为唯一项
     */
    public boolean isSingle() {
        return mIsOnlyOneItem;
    }

    /**
     * 获取笔记ID
     */
    public long getId() {
        return mId;
    }

    /**
     * 获取提醒时间
     */
    public long getAlertDate() {
        return mAlertDate;
    }

    /**
     * 获取创建时间
     */
    public long getCreatedDate() {
        return mCreatedDate;
    }

    /**
     * 判断是否有附件
     */
    public boolean hasAttachment() {
        return mHasAttachment;
    }

    /**
     * 获取修改时间
     */
    public long getModifiedDate() {
        return mModifiedDate;
    }

    /**
     * 获取背景颜色ID
     */
    public int getBgColorId() {
        return mBgColorId;
    }

    /**
     * 获取父文件夹ID
     */
    public long getParentId() {
        return mParentId;
    }

    /**
     * 获取笔记数量
     */
    public int getNotesCount() {
        return mNotesCount;
    }

    /**
     * 获取文件夹ID
     */
    public long getFolderId () {
        return mParentId;
    }

    /**
     * 获取笔记类型
     */
    public int getType() {
        return mType;
    }

    /**
     * 获取小部件类型
     */
    public int getWidgetType() {
        return mWidgetType;
    }

    /**
     * 获取小部件ID
     */
    public int getWidgetId() {
        return mWidgetId;
    }

    /**
     * 获取笔记摘要
     */
    public String getSnippet() {
        return mSnippet;
    }

    /**
     * 判断是否设置了提醒
     */
    public boolean hasAlert() {
        return (mAlertDate > 0);
    }

    /**
     * 判断是否为通话记录
     */
    public boolean isCallRecord() {
        return (mParentId == Notes.ID_CALL_RECORD_FOLDER && !TextUtils.isEmpty(mPhoneNumber));
    }

    /**
     * 获取笔记类型的静态方法
     * @param cursor 数据库游标
     * @return 笔记类型
     */
    public static int getNoteType(Cursor cursor) {
        return cursor.getInt(TYPE_COLUMN);
    }
}
