/**
 * @description 成就相关枚举辅助工具
 * @author yslg
 * @since 2025-10-25
 */

import { AchievementType, AchievementConditionType, AchievementEventType } from './index';

/**
 * 成就类型描述映射
 */
export const AchievementTypeDescriptions: Record<AchievementType, string> = {
  [AchievementType.BADGE]: '勋章',
  [AchievementType.LEVEL]: '等级'
};

/**
 * 成就条件类型描述映射
 */
export const AchievementConditionTypeDescriptions: Record<AchievementConditionType, string> = {
  [AchievementConditionType.LEARNING_TIME]: '学习时长',
  [AchievementConditionType.RESOURCE_VIEW_COUNT]: '浏览资源数量',
  [AchievementConditionType.COURSE_COMPLETE_COUNT]: '完成课程数量',
  [AchievementConditionType.CONTINUOUS_LOGIN_DAYS]: '连续登录天数',
  [AchievementConditionType.RESOURCE_COLLECT_COUNT]: '收藏资源数量',
  [AchievementConditionType.TASK_COMPLETE_COUNT]: '完成任务数量',
  [AchievementConditionType.POINTS_EARNED]: '获得积分数量',
  [AchievementConditionType.COMMENT_COUNT]: '发表评论数量',
  [AchievementConditionType.CHAPTER_COMPLETE_COUNT]: '完成章节数量',
  [AchievementConditionType.TOTAL_LOGIN_DAYS]: '累计登录天数'
};

/**
 * 成就事件类型描述映射
 */
export const AchievementEventTypeDescriptions: Record<AchievementEventType, string> = {
  // 学习相关事件
  [AchievementEventType.LEARNING_TIME_UPDATED]: '学习时长更新',
  [AchievementEventType.COURSE_COMPLETED]: '课程完成',
  [AchievementEventType.COURSE_STARTED]: '开始学习课程',
  [AchievementEventType.CHAPTER_COMPLETED]: '章节完成',
  // 资源相关事件
  [AchievementEventType.RESOURCE_VIEWED]: '浏览资源',
  [AchievementEventType.RESOURCE_COLLECTED]: '收藏资源',
  [AchievementEventType.RESOURCE_SHARED]: '分享资源',
  // 任务相关事件
  [AchievementEventType.TASK_COMPLETED]: '任务完成',
  [AchievementEventType.TASK_ITEM_COMPLETED]: '任务项完成',
  // 互动相关事件
  [AchievementEventType.COMMENT_POSTED]: '发表评论',
  [AchievementEventType.LIKE_GIVEN]: '点赞',
  // 登录相关事件
  [AchievementEventType.USER_LOGIN]: '用户登录',
  [AchievementEventType.CONTINUOUS_LOGIN]: '连续登录',
  // 积分相关事件
  [AchievementEventType.POINTS_EARNED_EVENT]: '获得积分',
  // 测试相关事件
  [AchievementEventType.TEST_PASSED]: '测试通过',
  [AchievementEventType.TEST_PERFECT_SCORE]: '测试满分'
};

/**
 * 成就枚举辅助类
 */
export class AchievementEnumHelper {
  
  /**
   * 获取成就类型描述
   * @param type 成就类型
   * @returns 描述文本
   */
  static getAchievementTypeDescription(type: AchievementType): string {
    return AchievementTypeDescriptions[type] || '未知类型';
  }
  
  /**
   * 获取成就条件类型描述
   * @param type 条件类型
   * @returns 描述文本
   */
  static getConditionTypeDescription(type: AchievementConditionType): string {
    return AchievementConditionTypeDescriptions[type] || '未知条件';
  }
  
  /**
   * 获取成就事件类型描述
   * @param type 事件类型
   * @returns 描述文本
   */
  static getEventTypeDescription(type: AchievementEventType): string {
    return AchievementEventTypeDescriptions[type] || '未知事件';
  }
  
  /**
   * 根据code获取成就条件类型
   * @param code 条件类型代码
   * @returns 条件类型枚举值或null
   */
  static getConditionTypeFromCode(code: number): AchievementConditionType | null {
    const types = Object.values(AchievementConditionType).filter(
      (value): value is AchievementConditionType => typeof value === 'number'
    );
    return types.find(type => type === code) || null;
  }
  
  /**
   * 根据code获取成就事件类型
   * @param code 事件类型代码
   * @returns 事件类型枚举值或null
   */
  static getEventTypeFromCode(code: string): AchievementEventType | null {
    const types = Object.values(AchievementEventType).filter(
      (value): value is AchievementEventType => typeof value === 'string'
    );
    return types.find(type => type === code) || null;
  }
  
  /**
   * 获取所有成就条件类型选项（用于下拉框等）
   * @returns 选项数组
   */
  static getAllConditionTypeOptions(): Array<{ value: AchievementConditionType; label: string }> {
    return Object.entries(AchievementConditionTypeDescriptions).map(([value, label]) => ({
      value: Number(value) as AchievementConditionType,
      label
    }));
  }
  
  /**
   * 获取所有成就类型选项（用于下拉框等）
   * @returns 选项数组
   */
  static getAllAchievementTypeOptions(): Array<{ value: AchievementType; label: string }> {
    return Object.entries(AchievementTypeDescriptions).map(([value, label]) => ({
      value: Number(value) as AchievementType,
      label
    }));
  }
  
  /**
   * 获取所有成就事件类型选项（用于下拉框等）
   * @returns 选项数组
   */
  static getAllEventTypeOptions(): Array<{ value: AchievementEventType; label: string }> {
    return Object.entries(AchievementEventTypeDescriptions).map(([value, label]) => ({
      value: value as AchievementEventType,
      label
    }));
  }
  
  /**
   * 格式化条件值显示
   * @param conditionType 条件类型
   * @param conditionValue 条件值
   * @returns 格式化后的显示文本
   */
  static formatConditionValue(conditionType: AchievementConditionType, conditionValue: number): string {
    const typeDesc = this.getConditionTypeDescription(conditionType);
    
    switch (conditionType) {
      case AchievementConditionType.LEARNING_TIME:
        return `${typeDesc}达到${conditionValue/60}分钟`;
      case AchievementConditionType.RESOURCE_VIEW_COUNT:
        return `${typeDesc}达到${conditionValue}个`;
      case AchievementConditionType.COURSE_COMPLETE_COUNT:
        return `${typeDesc}达到${conditionValue}门`;
      case AchievementConditionType.CONTINUOUS_LOGIN_DAYS:
        return `${typeDesc}达到${conditionValue}天`;
      case AchievementConditionType.RESOURCE_COLLECT_COUNT:
        return `${typeDesc}达到${conditionValue}个`;
      case AchievementConditionType.TASK_COMPLETE_COUNT:
        return `${typeDesc}达到${conditionValue}个`;
      case AchievementConditionType.POINTS_EARNED:
        return `${typeDesc}达到${conditionValue}分`;
      case AchievementConditionType.COMMENT_COUNT:
        return `${typeDesc}达到${conditionValue}条`;
      case AchievementConditionType.CHAPTER_COMPLETE_COUNT:
        return `${typeDesc}达到${conditionValue}个`;
      case AchievementConditionType.TOTAL_LOGIN_DAYS:
        return `${typeDesc}达到${conditionValue}天`;
      default:
        return `${typeDesc}达到${conditionValue}`;
    }
  }
}

