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

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

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.NotesDatabaseHelper.TABLE;
import net.micode.notes.gtask.exception.ActionFailureException;

import org.json.JSONException;
import org.json.JSONObject;

// SqlData类用于管理数据库中的笔记数据
public class SqlData {
    // 类的标签，用于日志记录
    private static final String TAG = SqlData.class.getSimpleName();

    // 无效的ID常量
    private static final int INVALID_ID = -99999;

    // 数据库查询的列
    public static final String[] PROJECTION_DATA = new String[] {
            DataColumns.ID, DataColumns.MIME_TYPE, DataColumns.CONTENT, DataColumns.DATA1,
            DataColumns.DATA3
    };

    // 数据库列的索引
    public static final int DATA_ID_COLUMN = 0;
    public static final int DATA_MIME_TYPE_COLUMN = 1;
    public static final int DATA_CONTENT_COLUMN = 2;
    public static final int DATA_CONTENT_DATA_1_COLUMN = 3;
    public static final int DATA_CONTENT_DATA_3_COLUMN = 4;

    // 成员变量
    private ContentResolver mContentResolver; // 内容解析器
    private boolean mIsCreate; // 是否为创建状态
    private long mDataId; // 数据ID
    private String mDataMimeType; // 数据MIME类型
    private String mDataContent; // 数据内容
    private long mDataContentData1; // 数据内容DATA1
    private String mDataContentData3; // 数据内容DATA3
    private ContentValues mDiffDataValues; // 差异数据值

    // 构造函数，初始化SqlData对象
    public SqlData(Context context) {
        mContentResolver = context.getContentResolver();
        mIsCreate = true;
        mDataId = INVALID_ID;
        mDataMimeType = DataConstants.NOTE;
        mDataContent = "";
        mDataContentData1 = 0;
        mDataContentData3 = "";
        mDiffDataValues = new ContentValues();
    }

    // 从Cursor构造SqlData对象
    public SqlData(Context context, Cursor c) {
        mContentResolver = context.getContentResolver();
        mIsCreate = false;
        loadFromCursor(c);
        mDiffDataValues = new ContentValues();
    }

    // 从Cursor加载数据
    private void loadFromCursor(Cursor c) {
        mDataId = c.getLong(DATA_ID_COLUMN);
        mDataMimeType = c.getString(DATA_MIME_TYPE_COLUMN);
        mDataContent = c.getString(DATA_CONTENT_COLUMN);
        mDataContentData1 = c.getLong(DATA_CONTENT_DATA_1_COLUMN);
        mDataContentData3 = c.getString(DATA_CONTENT_DATA_3_COLUMN);
    }

    // 设置内容
    public void setContent(JSONObject js) throws JSONException {
        // 根据JSON对象设置数据
        // ...
    }

    // 获取内容
    public JSONObject getContent() throws JSONException {
        // 根据当前数据创建JSON对象
        // ...
    }

    // 提交数据到数据库
    public void commit(long noteId, boolean validateVersion, long version) {
        // 根据创建状态和差异数据值，将数据插入或更新到数据库
        // ...
    }

    // 获取数据ID
    public long getId() {
        return mDataId;
    }
}