/*
 * 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.text.format.DateUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.micode.notes.R;
import net.micode.notes.data.Notes;
import net.micode.notes.tool.DataUtils;
import net.micode.notes.tool.ResourceParser.NoteItemBgResources;

//定义NotesListItem 类，扩展了LinearLayout 类，用于表示一个笔记的布局
public class NotesListItem extends LinearLayout {
    private ImageView mAlert;           //用于显示警告图标
    private TextView mTitle;            //显示笔记标题
    private TextView mTime;             //显示最后修改时间
    private TextView mCallName;         //显示通话名称
    private NoteItemData mItemData;     //保存当前笔记项的数据
    private CheckBox mCheckBox;         //多选模式下的复选框


    public NotesListItem(Context context) {
        super(context);
        //从布局文件 note_item.xml中加载布局
        inflate(context, R.layout.note_item, this);
        //通过findViewById 方法获取对应的控件对象
        mAlert = (ImageView) findViewById(R.id.iv_alert_icon);
        mTitle = (TextView) findViewById(R.id.tv_title);
        mTime = (TextView) findViewById(R.id.tv_time);
        mCallName = (TextView) findViewById(R.id.tv_name);
        mCheckBox = (CheckBox) findViewById(android.R.id.checkbox);
    }

    //绑定数据到当前的笔记项，设置显示内容和样式
    public void bind(Context context, NoteItemData data, boolean choiceMode, boolean checked) {
        //如果是多选模式且笔记类型为普通笔记
        if (choiceMode && data.getType() == Notes.TYPE_NOTE) {
            mCheckBox.setVisibility(View.VISIBLE);
            mCheckBox.setChecked(checked);
        } else {
            mCheckBox.setVisibility(View.GONE);
        }

        //保存数据到成员变量中
        mItemData = data;
        //根据笔记的类型分别设置不同的显示方式
        if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) {
            //如果是通话记录文件夹
            mCallName.setVisibility(View.GONE);
            mAlert.setVisibility(View.VISIBLE);
            mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem);
            mTitle.setText(context.getString(R.string.call_record_folder_name)
                    + context.getString(R.string.format_folder_files_count, data.getNotesCount()));
            mAlert.setImageResource(R.drawable.call_record);
        } else if (data.getParentId() == Notes.ID_CALL_RECORD_FOLDER) {
            //如果笔记是通话记录文件夹中的一项
            mCallName.setVisibility(View.VISIBLE);
            mCallName.setText(data.getCallName());
            mTitle.setTextAppearance(context,R.style.TextAppearanceSecondaryItem);
            mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet()));
            if (data.hasAlert()) {
                mAlert.setImageResource(R.drawable.clock);
                mAlert.setVisibility(View.VISIBLE);
            } else {
                mAlert.setVisibility(View.GONE);
            }
        } else {
            //如果笔记既不是通话记录也不是通话记录文件夹中的一项
            mCallName.setVisibility(View.GONE);
            mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem);

            //如果笔记为文件夹类型
            if (data.getType() == Notes.TYPE_FOLDER) {
                mTitle.setText(data.getSnippet()
                        + context.getString(R.string.format_folder_files_count,
                                data.getNotesCount()));
                mAlert.setVisibility(View.GONE);
            } else {
                //如果笔记为普通笔记
                mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet()));
                if (data.hasAlert()) {
                    mAlert.setImageResource(R.drawable.clock);
                    mAlert.setVisibility(View.VISIBLE);
                } else {
                    mAlert.setVisibility(View.GONE);
                }
            }
        }
        //设置显示笔记最后修改时间，格式化为相对时间(如"5分钟前")
        mTime.setText(DateUtils.getRelativeTimeSpanString(data.getModifiedDate()));

        //根据数据设置背景图案
        setBackground(data);
    }

    //设置背景图案的私有方法
    private void setBackground(NoteItemData data) {
        int id = data.getBgColorId();
        //根据笔记类型和位置(身处列表中的位置)，选择不同的背景
        if (data.getType() == Notes.TYPE_NOTE) {
            if (data.isSingle() || data.isOneFollowingFolder()) {
                setBackgroundResource(NoteItemBgResources.getNoteBgSingleRes(id));
            } else if (data.isLast()) {
                setBackgroundResource(NoteItemBgResources.getNoteBgLastRes(id));
            } else if (data.isFirst() || data.isMultiFollowingFolder()) {
                setBackgroundResource(NoteItemBgResources.getNoteBgFirstRes(id));
            } else {
                setBackgroundResource(NoteItemBgResources.getNoteBgNormalRes(id));
            }
        } else {
            //如果是文件夹，则使用文件夹的背景资源
            setBackgroundResource(NoteItemBgResources.getFolderBgRes());
        }
    }

    //获取当前笔记项的数据
    public NoteItemData getItemData() {
        return mItemData;
    }
}
