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

//cursor类用于在Android中进行数据库查询操作的游标类
import android.database.Cursor;
//log类用于在Android应用程序中输出日志的类
import android.util.Log;

//GTaskStringUtils用于处理相关的字符串操作
import net.micode.notes.tool.GTaskStringUtils;

//JSONException类用于JSON解析过程中可能出现的异常类
import org.json.JSONException;
//JSONObject类用于在Java中创建和操作JSON对象的类
import org.json.JSONObject;


public class MetaData extends Task {
    private final static String TAG = MetaData.class.getSimpleName();

    private String mRelatedGid = null;
    
    //此函数用于设置元数据信息，首先接受两个参数gid（字符串类型）和metainfo，gid是Goole任务的ID，metainfo是包含元数据信息的JSON对象。
    public void setMeta(String gid, JSONObject metaInfo) {
        //在try-catch块中，首先使用put方法将gid添加到metainfo中，如果添加失败，会抛出JSONException异常，然后在catch块中通过日志输出错误信息。
        try {
            metaInfo.put(GTaskStringUtils.META_HEAD_GTASK_ID, gid);
	  
        } catch (JSONException e) {
            Log.e(TAG, "failed to put related gid");
        }
        //然后将metainfo转换为字符串并调用setNotes方法进行设置。
        setNotes(metaInfo.toString());
        setName(GTaskStringUtils.META_NOTE_NAME);
    }

    //获取相关联的gid
    public String getRelatedGid() {
        return mRelatedGid;
    }

    //判断当前数据是否为空，若为空则返回真值并保存。
    @Override
    public boolean isWorthSaving() {
        return getNotes() != null;
    }
    
    //这段代码是对note类中setContentByRemoteJSON的重写，作用是通过远程JSON数据设置笔记里的内容
    @Override
    public void setContentByRemoteJSON(JSONObject js) {
        super.setContentByRemoteJSON(js);
        if (getNotes() != null) {
            //在 try-catch 块中，尝试从JSON对象中获取相关的任务 ID，并将其赋值给成员变量mRelatedGid，如果获取失败或者注释不是有效的 JSON 格式，则将 mRelatedGid 设为 null。
            try {
                JSONObject metaInfo = new JSONObject(getNotes().trim());
                mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID);
            } catch (JSONException e) {
                Log.w(TAG, "failed to get related gid");
                mRelatedGid = null;
            }
        }
    }

    //函数作用是使用本地JSON数据对象设置元数据内容
    @Override
    public void setContentByLocalJSON(JSONObject js) {
        // this function should not be called
        throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called");
    }

    //函数作用是从元数据内容中获取JSON对象
    @Override
    public JSONObject getLocalJSONFromContent() {
        throw new IllegalAccessError("MetaData:getLocalJSONFromContent should not be called");
    }
  
    //函数作用是获取实时动作状态
    @Override
    public int getSyncAction(Cursor c) {
        throw new IllegalAccessError("MetaData:getSyncAction should not be called");
    }

}
