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



/**
 * @projectName（项目名称）: xiaomi label
 * @package（包）: ui
 * @className（类名称）: NotesListItem
 * @description（类描述）: 继承于LinearLayout，创建便签列表项目选项
 * @author（创建人）: zhangchaoqun
 * @createDate（创建时间）: datetime
 * @updateUser（修改人）: user
 * @updateDate（修改时间）: datetime
 * @updateRemark（修改备注）: 说明本次修改内容
 * @version（版本）: v1.0
 */
public class NotesListItem extends LinearLayout {
    private ImageView mAlert;//闹钟图片
    private TextView mTitle;//标题
    private TextView mTime;//时间
    private TextView mCallName;//名称
    private NoteItemData mItemData;//标签数据
    private CheckBox mCheckBox;//打勾框


    /**
     * @description 描述:初始化基本信息
     * @param 参数1:context
     * @param 参数2:
     * @param 参数3:
     * @return 返回值:创建NotesListItem的实例化对象
     * @author zhangchaoqun
     */
    public NotesListItem(Context context) {
        super(context);
        inflate(context, R.layout.note_item, this);
        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);
    }


    /**
     * @description 描述:根据data的属性对各个控件的属性的控制，主要是可见性Visibility，内容setText，格式setTextAppearance
     * @param 参数1:context
     * @param 参数2:data
     * @param 参数3:choiceMode
     * @param 参数4:checked
     * @return 返回值:void
     * @author zhangchaoqun
     */
    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;
        //设置控件属性，一共三种情况，由data的id和父id是否与保存到文件夹的id一致来决定
        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);
                }
            }
        }
        //设置内容，获取相关时间，从data里编辑的日期中获取
        mTime.setText(DateUtils.getRelativeTimeSpanString(data.getModifiedDate()));

        setBackground(data);
    }


    /**
     * @description 描述:根据data的文件属性来设置背景
     * @param 参数1:data
     * @param 参数2:
     * @param 参数3:
     * @return 返回值:void
     * @author zhangchaoqun
     */
    private void setBackground(NoteItemData data) {
        int id = data.getBgColorId();
        //若是note型文件，则4种情况，对于4种不同情况的背景来源
        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));
            }
        }
        //若不是note直接调用文件夹的背景来源
        else {
            setBackgroundResource(NoteItemBgResources.getFolderBgRes());
        }
    }

    public NoteItemData getItemData() {
        return mItemData;
    }
}
