/*
 * 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.database.Cursor;
import android.util.Log;

import net.micode.notes.tool.GTaskStringUtils;

import org.json.JSONException;
import org.json.JSONObject;
public class MetaData extends Task {  // 定义名为MetaData的类，继承自Task类
    private final static String TAG = MetaData.class.getSimpleName();  // 定义静态字符串TAG，并初始化为MetaData类的简单名称

    private String mRelatedGid = null;  // 声明私有字符串变量mRelatedGid，并初始化为null

    public void setMeta(String gid, JSONObject metaInfo) {  // 定义一个名为setMeta的方法，接受gid和metaInfo两个参数
        try {  // 尝试执行以下代码块
            metaInfo.put(GTaskStringUtils.META_HEAD_GTASK_ID, gid);  // 向metaInfo中放入键为META_HEAD_GTASK_ID、值为gid的键值对
        } catch (JSONException e) {  // 如果捕获到JSONException异常
            Log.e(TAG, "failed to put related gid");  // 记录错误日志："failed to put related gid"
        }
        setNotes(metaInfo.toString());  // 调用setNotes方法，将metaInfo转换为字符串后设置为备注信息
        setName(GTaskStringUtils.META_NOTE_NAME);  // 调用setName方法，设置名称为META_NOTE_NAME
    }

    public String getRelatedGid() {  // 定义一个名为getRelatedGid的方法
        return mRelatedGid;  // 返回mRelatedGid变量的值
    }

    @Override
    public boolean isWorthSaving() {  // 覆盖父类的isWorthSaving方法
        return getNotes() != null;  // 返回是否备注信息不为null
    }

    @Override
    public void setContentByRemoteJSON(JSONObject js) {  // 覆盖父类的setContentByRemoteJSON方法，接受JSONObject类型的js参数
        super.setContentByRemoteJSON(js);  // 调用父类的setContentByRemoteJSON方法
        if (getNotes() != null) {  // 如果备注信息不为null
            try {  // 尝试执行以下代码块
                JSONObject metaInfo = new JSONObject(getNotes().trim());  // 创建新的JSONObject对象metaInfo，通过trim方法去除备注信息的空格
                mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID);  // 从metaInfo中获取键为META_HEAD_GTASK_ID的值，并设置给mRelatedGid
            } catch (JSONException e) {  // 如果捕获到JSONException异常
                Log.w(TAG, "failed to get related gid");  // 记录警告日志："failed to get related gid"
                mRelatedGid = null;  // 将mRelatedGid设置为null
            }
        }
    }

    @Override
    public void setContentByLocalJSON(JSONObject js) {  // 覆盖父类的setContentByLocalJSON方法，接受JSONObject类型的js参数
        // this function should not be called  // 注释：不应调用该函数
        throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called");  // 抛出IllegalAccessError异常，内容为"MetaData:setContentByLocalJSON should not be called"
    }

    @Override
    public JSONObject getLocalJSONFromContent() {  // 覆盖父类的getLocalJSONFromContent方法
        throw new IllegalAccessError("MetaData:getLocalJSONFromContent should not be called");  // 抛出IllegalAccessError异常，内容为"MetaData:getLocalJSONFromContent should not be called"
    }

    @Override
    public int getSyncAction(Cursor c) {  // 覆盖父类的getSyncAction方法，接受Cursor类型的c参数
        throw new IllegalAccessError("MetaData:getSyncAction should not be called");  // 抛出IllegalAccessError异常，内容为"MetaData:getSyncAction should not be called"
    }
}
