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

/**
 * Google Tasks API 工具类 - 包含与Google Tasks API交互的所有常量定义
 * 包括：
 * 1. JSON键名常量
 * 2. API动作类型常量
 * 3. 预定义文件夹名称
 * 4. 元数据标识符
 *
 * 设计原则：
 * - 所有常量按功能分组
 * - 添加枚举类型增强类型安全
 * - 补充必要的使用说明注释
 */
public final class GTaskStringUtils {

    // 防止实例化
    private GTaskStringUtils() {
        throw new AssertionError("This class cannot be instantiated");
    }

    /* ================== JSON 键名常量 ================== */

    /** JSON字段：操作ID */
    public static final String GTASK_JSON_ACTION_ID = "action_id";

    /** JSON字段：操作列表 */
    public static final String GTASK_JSON_ACTION_LIST = "action_list";

    /** JSON字段：操作类型 */
    public static final String GTASK_JSON_ACTION_TYPE = "action_type";

    /** JSON字段：创建者ID */
    public static final String GTASK_JSON_CREATOR_ID = "creator_id";

    /** JSON字段：子实体 */
    public static final String GTASK_JSON_CHILD_ENTITY = "child_entity";

    /** JSON字段：客户端版本 */
    public static final String GTASK_JSON_CLIENT_VERSION = "client_version";

    /** JSON字段：完成状态 */
    public static final String GTASK_JSON_COMPLETED = "completed";

    /** JSON字段：当前列表ID */
    public static final String GTASK_JSON_CURRENT_LIST_ID = "current_list_id";

    /** JSON字段：默认列表ID */
    public static final String GTASK_JSON_DEFAULT_LIST_ID = "default_list_id";

    /** JSON字段：删除标记 */
    public static final String GTASK_JSON_DELETED = "deleted";

    /** JSON字段：目标列表 */
    public static final String GTASK_JSON_DEST_LIST = "dest_list";

    /** JSON字段：目标父级 */
    public static final String GTASK_JSON_DEST_PARENT = "dest_parent";

    /** JSON字段：目标父级类型 */
    public static final String GTASK_JSON_DEST_PARENT_TYPE = "dest_parent_type";

    /** JSON字段：实体增量 */
    public static final String GTASK_JSON_ENTITY_DELTA = "entity_delta";

    /** JSON字段：实体类型 */
    public static final String GTASK_JSON_ENTITY_TYPE = "entity_type";

    /** JSON字段：获取删除项 */
    public static final String GTASK_JSON_GET_DELETED = "get_deleted";

    /** JSON字段：唯一标识 */
    public static final String GTASK_JSON_ID = "id";

    /** JSON字段：索引位置 */
    public static final String GTASK_JSON_INDEX = "index";

    /** JSON字段：最后修改时间 */
    public static final String GTASK_JSON_LAST_MODIFIED = "last_modified";

    /** JSON字段：最新同步点 */
    public static final String GTASK_JSON_LATEST_SYNC_POINT = "latest_sync_point";

    /** JSON字段：列表ID */
    public static final String GTASK_JSON_LIST_ID = "list_id";

    /** JSON字段：列表集合 */
    public static final String GTASK_JSON_LISTS = "lists";

    /** JSON字段：名称 */
    public static final String GTASK_JSON_NAME = "name";

    /** JSON字段：新ID */
    public static final String GTASK_JSON_NEW_ID = "new_id";

    /** JSON字段：笔记集合 */
    public static final String GTASK_JSON_NOTES = "notes";

    /** JSON字段：父级ID */
    public static final String GTASK_JSON_PARENT_ID = "parent_id";

    /** JSON字段：前置兄弟ID */
    public static final String GTASK_JSON_PRIOR_SIBLING_ID = "prior_sibling_id";

    /** JSON字段：结果集 */
    public static final String GTASK_JSON_RESULTS = "results";

    /** JSON字段：源列表 */
    public static final String GTASK_JSON_SOURCE_LIST = "source_list";

    /** JSON字段：任务集合 */
    public static final String GTASK_JSON_TASKS = "tasks";

    /** JSON字段：类型标识 */
    public static final String GTASK_JSON_TYPE = "type";

    /* ================== 操作类型常量 ================== */

    /** 操作类型：创建 */
    public static final String GTASK_JSON_ACTION_TYPE_CREATE = "create";

    /** 操作类型：获取所有 */
    public static final String GTASK_JSON_ACTION_TYPE_GETALL = "get_all";

    /** 操作类型：移动 */
    public static final String GTASK_JSON_ACTION_TYPE_MOVE = "move";

    /** 操作类型：更新 */
    public static final String GTASK_JSON_ACTION_TYPE_UPDATE = "update";

    /* ================== 实体类型常量 ================== */

    /** 实体类型：分组 */
    public static final String GTASK_JSON_TYPE_GROUP = "GROUP";

    /** 实体类型：任务 */
    public static final String GTASK_JSON_TYPE_TASK = "TASK";

    /** JSON字段：用户 */
    public static final String GTASK_JSON_USER = "user";

    /* ================== 文件夹常量 ================== */

    /** MIUI笔记文件夹前缀 */
    public static final String MIUI_FOLDER_PREFIX = "[MIUI_Notes]";  // 修正：PREFFIX → PREFIX

    /** 默认文件夹名称 */
    public static final String FOLDER_DEFAULT = "Default";

    /** 通话记录文件夹名称 */
    public static final String FOLDER_CALL_NOTE = "Call_Note";

    /** 元数据文件夹名称 */
    public static final String FOLDER_META = "METADATA";

    /* ================== 元数据常量 ================== */

    /** 元数据头：Google Tasks ID */
    public static final String META_HEAD_GTASK_ID = "meta_gid";

    /** 元数据头：笔记信息 */
    public static final String META_HEAD_NOTE = "meta_note";

    /** 元数据头：数据信息 */
    public static final String META_HEAD_DATA = "meta_data";

    /** 元数据笔记名称（警告用户不要修改） */
    public static final String META_NOTE_NAME = "[META INFO] DON'T UPDATE AND DELETE";

    /* ================== 增强功能：操作类型枚举 ================== */

    /**
     * Google Tasks操作类型枚举（增强类型安全性）
     */
    public enum GTaskActionType {
        CREATE(GTASK_JSON_ACTION_TYPE_CREATE),
        GET_ALL(GTASK_JSON_ACTION_TYPE_GETALL),
        MOVE(GTASK_JSON_ACTION_TYPE_MOVE),
        UPDATE(GTASK_JSON_ACTION_TYPE_UPDATE);

        private final String actionValue;

        GTaskActionType(String value) {
            this.actionValue = value;
        }

        public String getValue() {
            return actionValue;
        }

        /**
         * 从字符串值获取枚举实例
         * @param value 操作类型字符串
         * @return 对应的枚举实例，找不到时返回null
         */
        public static GTaskActionType fromValue(String value) {
            for (GTaskActionType type : values()) {
                if (type.actionValue.equals(value)) {
                    return type;
                }
            }
            return null;
        }
    }

    /* ================== 增强功能：实体类型枚举 ================== */

    /**
     * Google Tasks实体类型枚举
     */
    public enum GTaskEntityType {
        GROUP(GTASK_JSON_TYPE_GROUP),
        TASK(GTASK_JSON_TYPE_TASK);

        private final String typeValue;

        GTaskEntityType(String value) {
            this.typeValue = value;
        }

        public String getValue() {
            return typeValue;
        }

        public static GTaskEntityType fromValue(String value) {
            for (GTaskEntityType type : values()) {
                if (type.typeValue.equals(value)) {
                    return type;
                }
            }
            return null;
        }
    }

    /* ================== 新功能：文件夹工具方法 ================== */

    /**
     * 生成同步文件夹名（添加MIUI前缀）
     * @param folderName 原始文件夹名
     * @return 格式化后的带前缀文件夹名
     */
    public static String generateSyncFolderName(String folderName) {
        return MIUI_FOLDER_PREFIX + folderName;
    }

    /**
     * 检查文件夹是否为MIUI同步文件夹
     * @param folderName 文件夹名称
     * @return 如果是MIUI同步文件夹返回true
     */
    public static boolean isMiuiSyncFolder(String folderName) {
        return folderName != null && folderName.startsWith(MIUI_FOLDER_PREFIX);
    }

    /**
     * 从MIUI同步文件夹名中提取原始文件夹名
     * @param miuiFolderName MIUI格式文件夹名
     * @return 原始文件夹名，如果不是MIUI格式返回null
     */
    public static String extractOriginalFolderName(String miuiFolderName) {
        if (isMiuiSyncFolder(miuiFolderName)) {
            return miuiFolderName.substring(MIUI_FOLDER_PREFIX.length());
        }
        return null;
    }
}