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

/**
 * MetaData类用于处理Google Tasks同步过程中的元数据
 * 继承自Task类，专门用于存储与Google Tasks任务相关的元数据信息
 * 这些元数据以JSON格式存储在笔记内容中
 */
public class MetaData extends Task {
    private final static String TAG = MetaData.class.getSimpleName();

    // 关联的Google Tasks任务ID
    private String mRelatedGid = null;

    /**
     * 设置元数据信息
     * @param gid 关联的Google Tasks任务ID
     * @param metaInfo 元数据信息的JSONObject对象
     */
    public void setMeta(String gid, JSONObject metaInfo) {
        try {
            // 将关联的Google Tasks任务ID添加到元数据中
            metaInfo.put(GTaskStringUtils.META_HEAD_GTASK_ID, gid);
        } catch (JSONException e) {
            Log.e(TAG, "failed to put related gid");
        }
        // 将元数据JSON转换为字符串并设置为笔记内容
        setNotes(metaInfo.toString());
        // 设置元数据笔记的名称
        setName(GTaskStringUtils.META_NOTE_NAME);
    }

    /**
     * 获取关联的Google Tasks任务ID
     * @return 关联的任务ID
     */
    public String getRelatedGid() {
        return mRelatedGid;
    }

    /**
     * 判断当前元数据是否值得保存
     * @return 如果笔记内容不为空则返回true，否则返回false
     */
    @Override
    public boolean isWorthSaving() {
        return getNotes() != null;
    }

    /**
     * 根据远程JSON数据设置元数据内容
     * @param js 远程获取的JSON对象
     */
    @Override
    public void setContentByRemoteJSON(JSONObject js) {
        // 调用父类方法设置基本内容
        super.setContentByRemoteJSON(js);
        if (getNotes() != null) {
            try {
                // 解析笔记内容中的JSON数据
                JSONObject metaInfo = new JSONObject(getNotes().trim());
                // 从元数据中提取关联的Google Tasks任务ID
                mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID);
            } catch (JSONException e) {
                Log.w(TAG, "failed to get related gid");
                mRelatedGid = null;
            }
        }
    }

    /**
     * 此方法不应该被调用，因为元数据不支持从本地JSON设置内容
     * @param js 本地JSON对象
     */
    @Override
    public void setContentByLocalJSON(JSONObject js) {
        throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called");
    }

    /**
     * 此方法不应该被调用，因为元数据不支持从内容获取本地JSON
     * @return 不返回任何内容，总是抛出异常
     */
    @Override
    public JSONObject getLocalJSONFromContent() {
        throw new IllegalAccessError("MetaData:getLocalJSONFromContent should not be called");
    }

    /**
     * 此方法不应该被调用，因为元数据不支持获取同步操作
     * @param c 数据库游标
     * @return 不返回任何内容，总是抛出异常
     */
    @Override
    public int getSyncAction(Cursor c) {
        throw new IllegalAccessError("MetaData:getSyncAction should not be called");
    }
}
