/*
 * 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;// 定义包net.micode.notes.gtask.data

import android.content.ContentResolver;// 导入android.content.ContentResolver类
import android.content.ContentUris;// 导入android.content.ContentUris类
import android.content.ContentValues;// 导入android.content.ContentValues类
import android.content.Context;// 导入android.content.Context类
import android.database.Cursor;// 导入android.database.Cursor类
import android.net.Uri;// 导入android.net.Uri类
import android.util.Log;// 导入android.util.Log类

import net.micode.notes.data.Notes;// 导入net.micode.notes.data.Notes类
import net.micode.notes.data.Notes.DataColumns; // 导入net.micode.notes.data.Notes.DataColumns类
import net.micode.notes.data.Notes.DataConstants;// 导入net.micode.notes.data.Notes.DataConstants类
import net.micode.notes.data.Notes.NoteColumns;// 导入net.micode.notes.data.Notes.NoteColumns类
import net.micode.notes.data.NotesDatabaseHelper.TABLE;// 导入net.micode.notes.data.NotesDatabaseHelper.TABLE类
import net.micode.notes.gtask.exception.ActionFailureException;// 导入net.micode.notes.gtask.exception.ActionFailureException类

import org.json.JSONException;// 导入org.json.JSONException类
import org.json.JSONObject;// 导入org.json.JSONObject类


public class SqlData {// 定义类SqlData
    private static final String TAG = SqlData.class.getSimpleName();// 定义私有静态常量TAG，赋值为SqlData类的简单名称

    private static final int INVALID_ID = -99999;// 定义私有静态常量INVALID_ID，赋值为-99999

    public static final String[] PROJECTION_DATA = new String[] {// 定义公共静态常量PROJECTION_DATA，赋值为String数组
            DataColumns.ID, DataColumns.MIME_TYPE, DataColumns.CONTENT, DataColumns.DATA1,
            DataColumns.DATA3
    };

    public static final int DATA_ID_COLUMN = 0;// 定义公共静态常量DATA_ID_COLUMN，赋值为0

    public static final int DATA_MIME_TYPE_COLUMN = 1;// 定义公共静态常量DATA_MIME_TYPE_COLUMN，赋值为1

    public static final int DATA_CONTENT_COLUMN = 2;// 定义公共静态常量DATA_CONTENT_COLUMN，赋值为2

    public static final int DATA_CONTENT_DATA_1_COLUMN = 3;// 定义公共静态常量DATA_CONTENT_DATA_1_COLUMN，赋值为3

    public static final int DATA_CONTENT_DATA_3_COLUMN = 4;// 定义公共静态常量DATA_CONTENT_DATA_3_COLUMN，赋值为4

    private ContentResolver mContentResolver;// 声明私有ContentResolver类型变量mContentResolver

    private boolean mIsCreate;// 声明私有boolean类型变量mIsCreate

    private long mDataId;// 声明私有long类型变量mDataId

    private String mDataMimeType;// 声明私有String类型变量mDataMimeType

    private String mDataContent;// 声明私有String类型变量mDataContent

    private long mDataContentData1;// 声明私有long类型变量mDataContentData1

    private String mDataContentData3;// 声明私有String类型变量mDataContentData3

    private ContentValues mDiffDataValues;// 声明私有ContentValues类型变量mDiffDataValues


    public SqlData(Context context) {// 定义构造函数SqlData，参数为Context类型变量context
        mContentResolver = context.getContentResolver();// 将mContentResolver赋值为context的ContentResolver
        mIsCreate = true;// 将mIsCreate赋值为true
        mDataId = INVALID_ID;// 将mDataId赋值为INVALID_ID
        mDataMimeType = DataConstants.NOTE;// 将mDataMimeType赋值为DataConstants.NOTE
        mDataContent = "";// 将mDataContent赋值为空字符串
        mDataContentData1 = 0;// 将mDataContentData1赋值为0
        mDataContentData3 = "";// 将mDataContentData3赋值为空字符串
        mDiffDataValues = new ContentValues();// 创建新的ContentValues对象并赋值给mDiffDataValues
    }

    public SqlData(Context context, Cursor c) {// 定义构造函数SqlData，参数为Context类型变量context和Cursor类型变量c
        mContentResolver = context.getContentResolver();// 将mContentResolver赋值为context的ContentResolver
        mIsCreate = false;// 将mIsCreate赋值为false
        loadFromCursor(c);// 调用loadFromCursor方法，传入参数c
        mDiffDataValues = new ContentValues();// 创建新的ContentValues对象并赋值给mDiffDataValues
    }

    private void loadFromCursor(Cursor c) { // 定义私有方法loadFromCursor，参数为Cursor类型变量c
        mDataId = c.getLong(DATA_ID_COLUMN);// 从Cursor c中获取long类型数据并赋值给mDataId
        mDataMimeType = c.getString(DATA_MIME_TYPE_COLUMN);// 从Cursor c中获取String类型数据并赋值给mDataMimeType
        mDataContent = c.getString(DATA_CONTENT_COLUMN);// 从Cursor c中获取String类型数据并赋值给mDataContent
        mDataContentData1 = c.getLong(DATA_CONTENT_DATA_1_COLUMN);// 从Cursor c中获取long类型数据并赋值给mDataContentData1
        mDataContentData3 = c.getString(DATA_CONTENT_DATA_3_COLUMN);// 从Cursor c中获取String类型数据并赋值给mDataContentData3
    }

    public void setContent(JSONObject js) throws JSONException {// 定义公共方法setContent，参数为JSONObject类型变量js，抛出JSONException异常
        long dataId = js.has(DataColumns.ID) ? js.getLong(DataColumns.ID) : INVALID_ID;// 从JSONObject js中获取数据ID，如果存在则赋值给dataId，否则赋值为INVALID_ID
        if (mIsCreate || mDataId != dataId) {// 如果mIsCreate为true或者mDataId不等于dataId
            mDiffDataValues.put(DataColumns.ID, dataId);// 将dataId放入mDiffDataValues中
        }
        mDataId = dataId;// 将dataId赋值给mDataId

        String dataMimeType = js.has(DataColumns.MIME_TYPE) ? js.getString(DataColumns.MIME_TYPE)
                : DataConstants.NOTE;// 从JSONObject js中获取MIME类型，如果存在则赋值给dataMimeType，否则赋值为DataConstants.NOTE
        if (mIsCreate || !mDataMimeType.equals(dataMimeType)) {// 如果mIsCreate为true或者mDataMimeType不等于dataMimeType
            mDiffDataValues.put(DataColumns.MIME_TYPE, dataMimeType);// 将dataMimeType放入mDiffDataValues中
        }
        mDataMimeType = dataMimeType;// 将dataMimeType赋值给mDataMimeType

        String dataContent = js.has(DataColumns.CONTENT) ? js.getString(DataColumns.CONTENT) : "";// 从JSONObject js中获取内容，如果存在则赋值给dataContent，否则赋值为空字符串
        if (mIsCreate || !mDataContent.equals(dataContent)) {// 如果mIsCreate为true或者mDataContent不等于dataContent
            mDiffDataValues.put(DataColumns.CONTENT, dataContent);// 将dataContent放入mDiffDataValues中
        }
        mDataContent = dataContent;// 将dataContent赋值给mDataContent

        long dataContentData1 = js.has(DataColumns.DATA1) ? js.getLong(DataColumns.DATA1) : 0;// 从JSONObject js中获取DATA1，如果存在则赋值给dataContentData1，否则赋值为0
        if (mIsCreate || mDataContentData1 != dataContentData1) {// 如果mIsCreate为true或者mDataContentData1不等于dataContentData1
            mDiffDataValues.put(DataColumns.DATA1, dataContentData1);// 将dataContentData1放入mDiffDataValues中
        }
        mDataContentData1 = dataContentData1;// 将dataContentData1赋值给mDataContentData1

        String dataContentData3 = js.has(DataColumns.DATA3) ? js.getString(DataColumns.DATA3) : "";// 从JSONObject js中获取DATA3，如果存在则赋值给dataContentData3，否则赋值为空字符串
        if (mIsCreate || !mDataContentData3.equals(dataContentData3)) {// 如果mIsCreate为true或者mDataContentData3不等于dataContentData3
            mDiffDataValues.put(DataColumns.DATA3, dataContentData3);// 将dataContentData3放入mDiffDataValues中
        }
        mDataContentData3 = dataContentData3;// 将dataContentData3赋值给mDataContentData3
    }

    public JSONObject getContent() throws JSONException {// 定义公共方法getContent，抛出JSONException异常
        if (mIsCreate) {// 如果mIsCreate为true
            Log.e(TAG, "it seems that we haven't created this in database yet");// 输出日志信息
            return null;// 返回null
        }
        JSONObject js = new JSONObject();// 创建新的JSONObject对象并赋值给js
        js.put(DataColumns.ID, mDataId);// 将mDataId放入js中
        js.put(DataColumns.MIME_TYPE, mDataMimeType);// 将mDataMimeType放入js中
        js.put(DataColumns.CONTENT, mDataContent);// 将mDataContent放入js中
        js.put(DataColumns.DATA1, mDataContentData1);// 将mDataContentData1放入js中
        js.put(DataColumns.DATA3, mDataContentData3);// 将mDataContentData3放入js中
        return js;// 返回js
    }

    public void commit(long noteId, boolean validateVersion, long version) {// 定义公共方法commit，参数为long类型变量noteId，boolean类型变量validateVersion，long类型变量version

        if (mIsCreate) {// 如果mIsCreate为true
            if (mDataId == INVALID_ID && mDiffDataValues.containsKey(DataColumns.ID)) {// 如果mDataId等于INVALID_ID并且mDiffDataValues包含DataColumns.ID
                mDiffDataValues.remove(DataColumns.ID);// 从mDiffDataValues中移除DataColumns.ID
            }

            mDiffDataValues.put(DataColumns.NOTE_ID, noteId);// 将noteId放入mDiffDataValues中
            Uri uri = mContentResolver.insert(Notes.CONTENT_DATA_URI, mDiffDataValues);// 调用ContentResolver的insert方法，将mDiffDataValues插入到Notes.CONTENT_DATA_URI，并将返回的Uri赋值给uri
            try {
                mDataId = Long.valueOf(uri.getPathSegments().get(1));// 从uri中获取pathSegments的第二个元素，并转换为long类型赋值给mDataId
            } catch (NumberFormatException e) {// 捕获NumberFormatException异常
                Log.e(TAG, "Get note id error :" + e.toString());// 输出日志信息
                throw new ActionFailureException("create note failed");// 抛出ActionFailureException异常
            }
        } else {
            if (mDiffDataValues.size() > 0) {// 如果mDiffDataValues的大小大于0
                int result = 0;// 声明并初始化int类型变量result为0
                if (!validateVersion) {// 如果validateVersion为false
                    result = mContentResolver.update(ContentUris.withAppendedId(
                            Notes.CONTENT_DATA_URI, mDataId), mDiffDataValues, null, null);// 调用ContentResolver的update方法，更新Notes.CONTENT_DATA_URI中mDataId对应的数据，更新值为mDiffDataValues
                } else {
                    result = mContentResolver.update(ContentUris.withAppendedId(
                                    Notes.CONTENT_DATA_URI, mDataId), mDiffDataValues,
                            " ? in (SELECT " + NoteColumns.ID + " FROM " + TABLE.NOTE
                                    + " WHERE " + NoteColumns.VERSION + "=?)", new String[] {
                                    String.valueOf(noteId), String.valueOf(version)
                            });// 调用ContentResolver的update方法，更新Notes.CONTENT_DATA_URI中mDataId对应的数据，更新值为mDiffDataValues，更新条件为对应的noteId和version
                }
                if (result == 0) {
                    Log.w(TAG, "there is no update. maybe user updates note when syncing");// 输出警告日志信息
                }
            }
        }

        mDiffDataValues.clear(); // 清空mDiffDataValues
        mIsCreate = false;// 将mIsCreate赋值为false
    }

    public long getId() {// 定义公共方法getId
        return mDataId;
    }//返回mDataId
}
