/*
 * 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; //类NoteItemData包含在包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 { //存储了很多note有关的属性、常用变量
    static final String [] PROJECTION = new String [] { //将便签id，提醒时间，背景颜色id，创建时间，关联桌面挂件，修改时间，便签数量，父文件夹id，文件夹id（便签的一段内容），便签的种类，桌面挂件的id，桌面挂件的类型的名称放在一个PROJECTION的字符串数组里
        NoteColumns.ID, //每个便签的序号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,
        NoteColumns.SNIPPET, //文件夹名称或者文本注释内容
        NoteColumns.TYPE,
        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; //PROJECT的字符串名称对应的值
    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利用光标和context获取的内容
        mId = cursor.getLong(ID_COLUMN); //gettype：获取指定数据，并以type类型传回
        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); //使用DataUtils类中定义的函数获取电话号码信息
            if (!TextUtils.isEmpty(mPhoneNumber)) { //如果mPhoneNumber变量不等于空string，即这个note的PhoneNumber数据是有效的
                mName = Contact.getContact(context, mPhoneNumber); //mphonenumber里有符合字符串，则用contart功能连接
                if (mName == null) {
                    mName = mPhoneNumber; //如果没保存名字，就以电话号码命名
                }
            }
        }

        if (mName == null) { //如果没有对name复制成功，则把name设置为空值
            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) { //如果是note格式并且不是第一个元素，则获取光标位置并根据上一行信息更改标记
            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)) { //若数据行数大于当前位置+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() { //若数据父id为保存至文件夹模式的id且满足电话号码单元不为空，则isCallRecord为true
        return mIsMultiNotesFollowingFolder;
    }

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

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

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

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

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

    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() { //获得父进程的id
        return mParentId;
    }

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

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

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

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

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

    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)); //判断是否为电话记录。如果类型匹配，且文本部位空，则表明是电话记录，返回真。否则，返回假
    }
 
    public static int getNoteType(Cursor cursor) { //获得便签的类型
        return cursor.getInt(TYPE_COLUMN);
    }
}
