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

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;

import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.DataColumns;
import net.micode.notes.data.Notes.DataConstants;
import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.data.Notes.TextNote;

/**
 * 清单管理类
 * 封装了清单相关的所有功能，包括清单的创建、查询、转换等
 */
public class ChecklistManager {
    private static final String TAG = "ChecklistManager";
    private ContentResolver mContentResolver;

    /**
     * 构造函数
     * @param contentResolver 内容解析器
     */
    public ChecklistManager(ContentResolver contentResolver) {
        mContentResolver = contentResolver;
    }

    /**
     * 检查一个笔记是否为清单
     * @param noteId 笔记ID
     * @return 是否为清单
     */
    public boolean isChecklistNote(long noteId) {
        // 使用ContentResolver查询笔记是否为清单
        String selection = DataColumns.NOTE_ID + "=? AND " + DataColumns.MIME_TYPE + "=? AND " + DataColumns.DATA1 + "=? AND " + DataColumns.DATA2 + "=?";
        String[] selectionArgs = {String.valueOf(noteId), DataConstants.NOTE, TextNote.MODE, String.valueOf(TextNote.MODE_CHECK_LIST)};

        Cursor cursor = mContentResolver.query(Notes.CONTENT_DATA_URI, null, selection, selectionArgs, null);
        boolean isChecklist = false;

        if (cursor != null) {
            isChecklist = cursor.moveToFirst();
            cursor.close();
        }

        return isChecklist;
    }

    /**
     * 创建清单笔记
     * @param folderId 文件夹ID
     * @param widgetId 小部件ID
     * @param widgetType 小部件类型
     * @param bgResId 背景资源ID
     * @param context 上下文
     * @return 创建的清单笔记
     */
    public WorkingNote createChecklistNote(long folderId, int widgetId, int widgetType, int bgResId, android.content.Context context) {
        WorkingNote note = WorkingNote.createEmptyNote(context, folderId, widgetId, widgetType, bgResId);
        note.setCheckListMode(TextNote.MODE_CHECK_LIST);
        return note;
    }

    /**
     * 为工作笔记设置清单类型
     * @param workingNote 工作笔记
     */
    public void setChecklistType(WorkingNote workingNote) {
        // 这里可以根据需要设置笔记类型
        // workingNote.setNoteType(Notes.TYPE_CHECKLIST);
    }

    /**
     * 将普通笔记转换为清单
     * @param noteId 笔记ID
     * @param context 上下文
     * @return 是否转换成功
     */
    public boolean convertToChecklist(long noteId, android.content.Context context) {
        WorkingNote note = WorkingNote.load(context, noteId);
        if (note != null) {
            note.setCheckListMode(TextNote.MODE_CHECK_LIST);
            return note.saveNote();
        }
        return false;
    }

    /**
     * 将清单转换为普通笔记
     * @param noteId 笔记ID
     * @param context 上下文
     * @return 是否转换成功
     */
    public boolean convertToNormal(long noteId, android.content.Context context) {
        WorkingNote note = WorkingNote.load(context, noteId);
        if (note != null) {
            note.setCheckListMode(TextNote.MODE_NORMAL);
            return note.saveNote();
        }
        return false;
    }

    /**
     * 获取清单笔记的选择条件
     * @return SQL选择条件
     */
    public String getChecklistSelection() {
        // 使用子查询来检查笔记是否为清单模式
        return "EXISTS (SELECT 1 FROM note_data WHERE note_id = " + NoteColumns.ID +
                " AND data1 = '" + TextNote.MODE + "' AND data2 = '" + String.valueOf(TextNote.MODE_CHECK_LIST) + "')";
    }

    /**
     * 为Intent添加清单模式标记
     * @param intent 要修改的Intent
     * @param forceChecklist 是否强制进入清单模式
     */
    public void addChecklistModeToIntent(Intent intent, boolean forceChecklist) {
        if (forceChecklist) {
            intent.putExtra("FORCE_CHECKLIST_MODE", true);
        }
    }

    /**
     * 处理Intent中的清单模式标记，创建相应类型的笔记
     * @param intent Intent对象
     * @param workingNote 工作笔记对象
     */
    public void handleChecklistModeFromIntent(Intent intent, WorkingNote workingNote) {
        boolean forceChecklistMode = intent.getBooleanExtra("FORCE_CHECKLIST_MODE", false);
        if (forceChecklistMode) {
            workingNote.setCheckListMode(TextNote.MODE_CHECK_LIST);
        }
    }
}