/*
 * 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;//引入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,//每个note都有独一无二的id
        NoteColumns.ALERTED_DATE,//设置的警报提醒时间
        NoteColumns.BG_COLOR_ID,//背景颜色的id
        NoteColumns.CREATED_DATE,//创建的时间
        NoteColumns.HAS_ATTACHMENT,//是否有附件。如果是一个text note，它不会有附件，如果是一个多媒体附件，会有至少一个附件
        NoteColumns.MODIFIED_DATE,//修改日期
        NoteColumns.NOTES_COUNT,//便签数量
        NoteColumns.PARENT_ID,//父id
        NoteColumns.SNIPPET,//文件夹名称或者文本注释内容
        NoteColumns.TYPE,// 某一列note的种类，是text note还是folder
        NoteColumns.WIDGET_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;
    private long mAlertDate;
    private int mBgColorId;
    private long mCreatedDate;
    private boolean mHasAttachment;
    private long mModifiedDate;
    private int mNotesCount;
    private long mParentId;
    private String mSnippet;
    private int mType;
    private int mWidgetId;
    private int mWidgetType;
    private String mName;
    private String mPhoneNumber;

    private boolean mIsLastItem;//判断是否为最后的项
    private boolean mIsFirstItem;//判断是否为最开始的项
    private boolean mIsOnlyOneItem;// 判断是否只有一个便签，或者一个文件夹
    private boolean mIsOneNoteFollowingFolder;//判断文件夹下是否只有一个便签
    private boolean mIsMultiNotesFollowingFolder;//判断文件夹下是否有多个便签

    public NoteItemData(Context context, Cursor cursor) {//第一个函数是类NoteItemData的构造函数,它完成了以下工作
        1.对于这个类的所有私有m型变量,通过cursor调用json中相应数据进行初始化
        2.对mPhoneNumber和mName进行单独处理
        3.检查cursor的位置
        mId = cursor.getLong(ID_COLUMN);//getxxx为转换格式
        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) ? true : false;//判断行列
        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);
    }

    private void checkPostion(Cursor cursor) {//根据光标位置设置标记
        mIsLastItem = cursor.isLast() ? true : false;//分别为各种描述状态的变量进行赋值
        mIsFirstItem = cursor.isFirst() ? true : false;
        mIsOnlyOneItem = (cursor.getCount() == 1);
        mIsMultiNotesFollowingFolder = false;//初始化“多重子文件”“单一子文件”2个标记
        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");
                }
            }
        }
    }

    public boolean isOneFollowingFolder() {
        return mIsOneNoteFollowingFolder;
    }//.接下来都是获取标记的函数

    public boolean isMultiFollowingFolder() {
        return mIsMultiNotesFollowingFolder;
    }//若数据父id为保存至文件夹模式的id且满足电话号码单元不为空，则isCallRecord为true

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

    public String getCallName() {
        return mName;
    }//获得便签的姓名

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

    public boolean isSingle() {
        return mIsOnlyOneItem;
    }//判断是否只有一个项

    public long getId() {
        return mId;
    }//获得对应的ID值

    public long getAlertDate() {
        return mAlertDate;
    }//获得对应的提醒时间

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

    public boolean hasAttachment() {
        return mHasAttachment;
    }// 判断是否关联桌面挂件

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

    public int getBgColorId() {
        return mBgColorId;
    }//获得背景颜色的索引

    public long getParentId() {
        return mParentId;
    }//获得父进程的id

    public int getNotesCount() {
        return mNotesCount;
    }//获得便签数量

    public long getFolderId () {
        return mParentId;
    }//获得文件夹id

    public int getType() {
        return mType;
    }//获得项的类型

    public int getWidgetType() {
        return mWidgetType;
    }//获得桌面挂件的类型

    public int getWidgetId() {
        return mWidgetId;
    }//获得获得桌面挂件id

    public String getSnippet() {
        return mSnippet;
    }//获得文件夹名称

    public boolean hasAlert() {
        return (mAlertDate > 0);
    }//判读此便签是否有提醒

    public boolean isCallRecord() {//判断便签项是否为CallRecord
        return (mParentId == Notes.ID_CALL_RECORD_FOLDER && !TextUtils.isEmpty(mPhoneNumber));//如果父类id保存至文件夹模式并且电话号码单元不为空
    }

    public static int getNoteType(Cursor cursor) {
        return cursor.getInt(TYPE_COLUMN);
    }//获得便签的类型
}
