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

// 导入Log类用于日志记录
import android.util.Log;
// 导入工具类用于字符串操作
import net.micode.notes.tool.GTaskStringUtils;
// 导入JSON相关类
import org.json.JSONException;
import org.json.JSONObject;

// 定义一个MetaData类,继承自Task类
public class MetaData extends Task {
    // 定义一个静态常量TAG,用于日志标识
    private final static String TAG = MetaData.class.getSimpleName();

    // 定义一个私有字符串变量mRelatedGid用于存储相关的GID
    private String mRelatedGid = null;

    // 设置元数据的方法,接收GID和JSON对象
    public void setMeta(String gid, JSONObject metaInfo) {
        try {
            // 将GID放入metaInfo JSON对象中
            metaInfo.put(GTaskStringUtils.META_HEAD_GTASK_ID, gid);
        } catch (JSONException e) {
            // 如果放入失败,记录错误日志
            Log.e(TAG, "failed to put related gid");
        }
        // 设置笔记内容为metaInfo的字符串形式
        setNotes(metaInfo.toString());
        // 设置笔记名称
        setName(GTaskStringUtils.META_NOTE_NAME);
    }

    // 获取相关GID的方法
    public String getRelatedGid() {
        return mRelatedGid; // 返回mRelatedGid
    }

    // 重写isWorthSaving方法,判断是否值得保存
    @Override
    public boolean isWorthSaving() {
        return getNotes() != null; // 如果笔记内容不为null,则值得保存
    }

    // 重写从远程JSON设置内容的方法
    @Override
    public void setContentByRemoteJSON(JSONObject js) {
        super.setContentByRemoteJSON(js); // 调用父类的方法
        // 如果笔记内容不为null
        if (getNotes() != null) {
            try {
                // 创建一个JSON对象metaInfo,并从笔记内容中解析
                JSONObject metaInfo = new JSONObject(getNotes().trim());
                // 获取相关GID并赋值给mRelatedGid
                mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID);
            } catch (JSONException e) {
                // 捕获JSON处理异常,记录警告日志并将mRelatedGid设为null
                Log.w(TAG, "failed to get related gid");
                mRelatedGid = null;
            }
        }
    }

    // 重写从本地JSON设置内容的方法,该方法不应该被调用
    @Override
    public void setContentByLocalJSON(JSONObject js) {
        // 抛出IllegalAccessError异常,表示该方法不应被调用
        throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called");
    }

    // 重写从内容获取本地JSON的方法,该方法不应该被调用
    @Override
    public JSONObject getLocalJSONFromContent() {
        // 抛出IllegalAccessError异常,表示该方法不应被调用
        throw new IllegalAccessError("MetaData:getLocalJSONFromContent should not be called");
    }

    // 重写获取同步操作的方法,该方法不应该被调用
    @Override
    public int getSyncAction(Cursor c) {
        // 抛出IllegalAccessError异常,表示该方法不应被调用
        throw new IllegalAccessError("MetaData:getSyncAction should not be called");
    }
}