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

/**
 * 元数据管理类，用于处理与Google Task同步相关的元数据信息
 * 继承自Task类，主要管理关联任务ID和元数据的存储与解析
 */
public class MetaData extends Task {
    // 日志标签
    private final static String TAG = MetaData.class.getSimpleName();

    // 关联的Google Task服务器ID
    private String mRelatedGid = null;

    /**
     * 设置元数据信息
     * @param gid 关联的Google Task服务器ID
     * @param metaInfo 需要存储的元数据JSON对象
     * 将gid存入metaInfo后，序列化为字符串存储到notes字段
     * 并设置固定名称标识为元数据笔记
     */
    public void setMeta(String gid, JSONObject metaInfo) {
        try {
            // 将关联的GTask ID注入元数据头部
            metaInfo.put(GTaskStringUtils.META_HEAD_GTASK_ID, gid);
        } catch (JSONException e) {
            Log.e(TAG, "failed to put related gid");
        }
        // 序列化元数据到笔记内容字段
        setNotes(metaInfo.toString());
        // 设置固定名称标识为元数据笔记
        setName(GTaskStringUtils.META_NOTE_NAME);
    }

    /**
     * 获取关联的GTask ID
     */
    public String getRelatedGid() {
        return mRelatedGid;
    }

    /**
     * 判断是否需要保存（当notes字段有内容时需保存）
     */
    @Override
    public boolean isWorthSaving() {
        return getNotes() != null;
    }

    /**
     * 通过远程JSON数据设置内容
     * 解析notes字段中的元数据并提取关联的GTask ID
     */
    @Override
    public void setContentByRemoteJSON(JSONObject js) {
        super.setContentByRemoteJSON(js);
        if (getNotes() != null) {
            try {
                // 反序列化notes字段内容
                JSONObject metaInfo = new JSONObject(getNotes().trim());
                // 提取关联的GTask ID
                mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID);
            } catch (JSONException e) {
                Log.w(TAG, "failed to get related gid");
                mRelatedGid = null;
            }
        }
    }

    // 以下方法在元数据管理中不应被调用，直接抛出异常

    /**
     * 元数据不应通过本地JSON设置内容
     * @throws IllegalAccessError 始终抛出，标识非法调用
     */
    @Override
    public void setContentByLocalJSON(JSONObject js) {
        throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called");
    }

    /**
     * 元数据不需要生成本地JSON内容
     * @throws IllegalAccessError 始终抛出，标识非法调用
     */
    @Override
    public JSONObject getLocalJSONFromContent() {
        throw new IllegalAccessError("MetaData:getLocalJSONFromContent should not be called");
    }

    /**
     * 元数据不处理本地数据库同步操作
     * @throws IllegalAccessError 始终抛出，标识非法调用
     */
    @Override
    public int getSyncAction(Cursor c) {
        throw new IllegalAccessError("MetaData:getSyncAction should not be called");
    }
}