/**
 * 微信表情包 TypeScript 模块
 * 提供类型安全的表情访问和图片路径获取功能
 */

/**
 * 表情类别枚举
 */
export enum EmojiCategory {
  /** 人脸表情 */
  FACE = "face",
  /** 手势表情 */
  GESTURE = "gesture",
  /** 动物表情 */
  ANIMAL = "animal",
  /** 祝福表情 */
  BLESSING = "blessing",
  /** 其他表情 */
  OTHER = "other",
}

/**
 * 表情信息接口
 */
export interface EmojiInfo {
  /** 表情名称 */
  name: string;
  /** 表情类别 */
  category: EmojiCategory;
  /** 图片文件路径 */
  path: string;
  /** 英文名称（可选） */
  englishName?: string;
}

/**
 * 表情名称类型
 */
export type EmojiName =
  // 人脸表情
  | "微笑"
  | "撇嘴"
  | "色"
  | "发呆"
  | "得意"
  | "流泪"
  | "害羞"
  | "闭嘴"
  | "睡"
  | "大哭"
  | "尴尬"
  | "发怒"
  | "调皮"
  | "呲牙"
  | "惊讶"
  | "难过"
  | "囧"
  | "抓狂"
  | "吐"
  | "偷笑"
  | "愉快"
  | "白眼"
  | "傲慢"
  | "困"
  | "惊恐"
  | "憨笑"
  | "悠闲"
  | "咒骂"
  | "疑问"
  | "嘘"
  | "晕"
  | "衰"
  | "骷髅"
  | "敲打"
  | "再见"
  | "擦汗"
  | "抠鼻"
  | "鼓掌"
  | "坏笑"
  | "右哼哼"
  | "鄙视"
  | "委屈"
  | "快哭了"
  | "阴险"
  | "亲亲"
  | "可怜"
  | "笑脸"
  | "生病"
  | "脸红"
  | "破涕为笑"
  | "恐惧"
  | "失望"
  | "无语"
  | "嘿哈"
  | "捂脸"
  | "机智"
  | "皱眉"
  | "耶"
  | "吃瓜"
  | "加油"
  | "汗"
  | "天啊"
  | "Emm"
  | "社会社会"
  | "旺柴"
  | "好的"
  | "打脸"
  | "哇"
  | "翻白眼"
  | "666"
  | "让我看看"
  | "叹气"
  | "苦涩"
  | "裂开"
  | "奸笑"
  // 手势表情
  | "握手"
  | "胜利"
  | "抱拳"
  | "勾引"
  | "拳头"
  | "OK"
  | "合十"
  | "强"
  | "拥抱"
  | "弱"
  // 动物表情
  | "猪头"
  | "跳跳"
  | "发抖"
  | "转圈"
  // 祝福表情
  | "庆祝"
  | "礼物"
  | "红包"
  | "發"
  | "福"
  | "烟花"
  | "爆竹"
  // 其他表情
  | "嘴唇"
  | "爱心"
  | "心碎"
  | "啤酒"
  | "咖啡"
  | "蛋糕"
  | "凋谢"
  | "菜刀"
  | "炸弹"
  | "便便"
  | "太阳"
  | "月亮"
  | "玫瑰";

/**
 * 表情数据映射
 * 将表情名称映射到完整的表情信息
 */
const EMOJI_DATA: Record<EmojiName, EmojiInfo> = {
  // 人脸表情
  微笑: {
    name: "微笑",
    category: EmojiCategory.FACE,
    path: "/assets/face/smile.png",
  },
  撇嘴: {
    name: "撇嘴",
    category: EmojiCategory.FACE,
    path: "/assets/face/pout.png",
  },
  色: {
    name: "色",
    category: EmojiCategory.FACE,
    path: "/assets/face/lustful.png",
  },
  发呆: {
    name: "发呆",
    category: EmojiCategory.FACE,
    path: "/assets/face/daze.png",
  },
  得意: {
    name: "得意",
    category: EmojiCategory.FACE,
    path: "/assets/face/smug.png",
  },
  流泪: {
    name: "流泪",
    category: EmojiCategory.FACE,
    path: "/assets/face/crying.png",
  },
  害羞: {
    name: "害羞",
    category: EmojiCategory.FACE,
    path: "/assets/face/shy.png",
  },
  闭嘴: {
    name: "闭嘴",
    category: EmojiCategory.FACE,
    path: "/assets/face/shut-up.png",
  },
  睡: {
    name: "睡",
    category: EmojiCategory.FACE,
    path: "/assets/face/sleep.png",
  },
  大哭: {
    name: "大哭",
    category: EmojiCategory.FACE,
    path: "/assets/face/wail.png",
  },
  尴尬: {
    name: "尴尬",
    category: EmojiCategory.FACE,
    path: "/assets/face/awkward.png",
  },
  发怒: {
    name: "发怒",
    category: EmojiCategory.FACE,
    path: "/assets/face/angry.png",
  },
  调皮: {
    name: "调皮",
    category: EmojiCategory.FACE,
    path: "/assets/face/naughty.png",
  },
  呲牙: {
    name: "呲牙",
    category: EmojiCategory.FACE,
    path: "/assets/face/grin.png",
  },
  惊讶: {
    name: "惊讶",
    category: EmojiCategory.FACE,
    path: "/assets/face/surprised.png",
  },
  难过: {
    name: "难过",
    category: EmojiCategory.FACE,
    path: "/assets/face/sad.png",
  },
  囧: {
    name: "囧",
    category: EmojiCategory.FACE,
    path: "/assets/face/embarrassed.png",
  },
  抓狂: {
    name: "抓狂",
    category: EmojiCategory.FACE,
    path: "/assets/face/crazy.png",
  },
  吐: {
    name: "吐",
    category: EmojiCategory.FACE,
    path: "/assets/face/vomit.png",
  },
  偷笑: {
    name: "偷笑",
    category: EmojiCategory.FACE,
    path: "/assets/face/snicker.png",
  },
  愉快: {
    name: "愉快",
    category: EmojiCategory.FACE,
    path: "/assets/face/happy.png",
  },
  白眼: {
    name: "白眼",
    category: EmojiCategory.FACE,
    path: "/assets/face/roll-eyes.png",
  },
  傲慢: {
    name: "傲慢",
    category: EmojiCategory.FACE,
    path: "/assets/face/arrogant.png",
  },
  困: {
    name: "困",
    category: EmojiCategory.FACE,
    path: "/assets/face/sleepy.png",
  },
  惊恐: {
    name: "惊恐",
    category: EmojiCategory.FACE,
    path: "/assets/face/panic.png",
  },
  憨笑: {
    name: "憨笑",
    category: EmojiCategory.FACE,
    path: "/assets/face/silly-smile.png",
  },
  悠闲: {
    name: "悠闲",
    category: EmojiCategory.FACE,
    path: "/assets/face/leisurely.png",
  },
  咒骂: {
    name: "咒骂",
    category: EmojiCategory.FACE,
    path: "/assets/face/curse.png",
  },
  疑问: {
    name: "疑问",
    category: EmojiCategory.FACE,
    path: "/assets/face/question.png",
  },
  嘘: {
    name: "嘘",
    category: EmojiCategory.FACE,
    path: "/assets/face/shush.png",
  },
  晕: {
    name: "晕",
    category: EmojiCategory.FACE,
    path: "/assets/face/dizzy.png",
  },
  衰: {
    name: "衰",
    category: EmojiCategory.FACE,
    path: "/assets/face/unlucky.png",
  },
  骷髅: {
    name: "骷髅",
    category: EmojiCategory.FACE,
    path: "/assets/face/skull.png",
  },
  敲打: {
    name: "敲打",
    category: EmojiCategory.FACE,
    path: "/assets/face/knock.png",
  },
  再见: {
    name: "再见",
    category: EmojiCategory.FACE,
    path: "/assets/face/goodbye.png",
  },
  擦汗: {
    name: "擦汗",
    category: EmojiCategory.FACE,
    path: "/assets/face/wipe-sweat.png",
  },
  抠鼻: {
    name: "抠鼻",
    category: EmojiCategory.FACE,
    path: "/assets/face/pick-nose.png",
  },
  鼓掌: {
    name: "鼓掌",
    category: EmojiCategory.FACE,
    path: "/assets/face/clap.png",
  },
  坏笑: {
    name: "坏笑",
    category: EmojiCategory.FACE,
    path: "/assets/face/evil-smile.png",
  },
  右哼哼: {
    name: "右哼哼",
    category: EmojiCategory.FACE,
    path: "/assets/face/right-hum.png",
  },
  鄙视: {
    name: "鄙视",
    category: EmojiCategory.FACE,
    path: "/assets/face/despise.png",
  },
  委屈: {
    name: "委屈",
    category: EmojiCategory.FACE,
    path: "/assets/face/wronged.png",
  },
  快哭了: {
    name: "快哭了",
    category: EmojiCategory.FACE,
    path: "/assets/face/about-to-cry.png",
  },
  阴险: {
    name: "阴险",
    category: EmojiCategory.FACE,
    path: "/assets/face/sinister.png",
  },
  亲亲: {
    name: "亲亲",
    category: EmojiCategory.FACE,
    path: "/assets/face/kiss.png",
  },
  可怜: {
    name: "可怜",
    category: EmojiCategory.FACE,
    path: "/assets/face/pitiful.png",
  },
  笑脸: {
    name: "笑脸",
    category: EmojiCategory.FACE,
    path: "/assets/face/smiley.png",
  },
  生病: {
    name: "生病",
    category: EmojiCategory.FACE,
    path: "/assets/face/sick.png",
  },
  脸红: {
    name: "脸红",
    category: EmojiCategory.FACE,
    path: "/assets/face/blush.png",
  },
  破涕为笑: {
    name: "破涕为笑",
    category: EmojiCategory.FACE,
    path: "/assets/face/tears-to-smile.png",
  },
  恐惧: {
    name: "恐惧",
    category: EmojiCategory.FACE,
    path: "/assets/face/fear.png",
  },
  失望: {
    name: "失望",
    category: EmojiCategory.FACE,
    path: "/assets/face/disappointed.png",
  },
  无语: {
    name: "无语",
    category: EmojiCategory.FACE,
    path: "/assets/face/speechless.png",
  },
  嘿哈: {
    name: "嘿哈",
    category: EmojiCategory.FACE,
    path: "/assets/face/hey-ha.png",
  },
  捂脸: {
    name: "捂脸",
    category: EmojiCategory.FACE,
    path: "/assets/face/facepalm.png",
  },
  机智: {
    name: "机智",
    category: EmojiCategory.FACE,
    path: "/assets/face/smart.png",
  },
  皱眉: {
    name: "皱眉",
    category: EmojiCategory.FACE,
    path: "/assets/face/frown.png",
  },
  耶: {
    name: "耶",
    category: EmojiCategory.FACE,
    path: "/assets/face/yeah.png",
  },
  吃瓜: {
    name: "吃瓜",
    category: EmojiCategory.FACE,
    path: "/assets/face/eat-melon.png",
  },
  加油: {
    name: "加油",
    category: EmojiCategory.FACE,
    path: "/assets/face/cheer-up.png",
  },

  汗: {
    name: "汗",
    category: EmojiCategory.FACE,
    path: "/assets/face/sweat.png",
  },
  天啊: {
    name: "天啊",
    category: EmojiCategory.FACE,
    path: "/assets/face/oh-my.png",
  },
  Emm: {
    name: "Emm",
    category: EmojiCategory.FACE,
    path: "/assets/face/Emm.png",
  },
  社会社会: {
    name: "社会社会",
    category: EmojiCategory.FACE,
    path: "/assets/face/social.png",
  },
  旺柴: {
    name: "旺柴",
    category: EmojiCategory.FACE,
    path: "/assets/face/doge.png",
  },
  好的: {
    name: "好的",
    category: EmojiCategory.FACE,
    path: "/assets/face/good.png",
  },
  打脸: {
    name: "打脸",
    category: EmojiCategory.FACE,
    path: "/assets/face/slap-face.png",
  },
  哇: {
    name: "哇",
    category: EmojiCategory.FACE,
    path: "/assets/face/wow.png",
  },
  翻白眼: {
    name: "翻白眼",
    category: EmojiCategory.FACE,
    path: "/assets/face/eye-roll.png",
  },
  "666": {
    name: "666",
    category: EmojiCategory.FACE,
    path: "/assets/face/666.png",
  },
  让我看看: {
    name: "让我看看",
    category: EmojiCategory.FACE,
    path: "/assets/face/let-me-see.png",
  },
  叹气: {
    name: "叹气",
    category: EmojiCategory.FACE,
    path: "/assets/face/sigh.png",
  },
  苦涩: {
    name: "苦涩",
    category: EmojiCategory.FACE,
    path: "/assets/face/bitter.png",
  },
  裂开: {
    name: "裂开",
    category: EmojiCategory.FACE,
    path: "/assets/face/crack.png",
  },
  奸笑: {
    name: "奸笑",
    category: EmojiCategory.FACE,
    path: "/assets/face/sly-smile.png",
  },

  // 手势表情
  握手: {
    name: "握手",
    category: EmojiCategory.GESTURE,
    path: "/assets/gesture/handshake.png",
  },
  胜利: {
    name: "胜利",
    category: EmojiCategory.GESTURE,
    path: "/assets/gesture/victory.png",
  },
  抱拳: {
    name: "抱拳",
    category: EmojiCategory.GESTURE,
    path: "/assets/gesture/fist-salute.png",
  },
  勾引: {
    name: "勾引",
    category: EmojiCategory.GESTURE,
    path: "/assets/gesture/beckon.png",
  },
  拳头: {
    name: "拳头",
    category: EmojiCategory.GESTURE,
    path: "/assets/gesture/fist.png",
  },
  OK: {
    name: "OK",
    category: EmojiCategory.GESTURE,
    path: "/assets/gesture/OK.png",
  },
  合十: {
    name: "合十",
    category: EmojiCategory.GESTURE,
    path: "/assets/gesture/pray.png",
  },
  强: {
    name: "强",
    category: EmojiCategory.GESTURE,
    path: "/assets/gesture/strong.png",
  },
  拥抱: {
    name: "拥抱",
    category: EmojiCategory.GESTURE,
    path: "/assets/gesture/hug.png",
  },
  弱: {
    name: "弱",
    category: EmojiCategory.GESTURE,
    path: "/assets/gesture/weak.png",
  },

  // 动物表情
  猪头: {
    name: "猪头",
    category: EmojiCategory.ANIMAL,
    path: "/assets/animal/pig.png",
  },
  跳跳: {
    name: "跳跳",
    category: EmojiCategory.ANIMAL,
    path: "/assets/animal/jump.png",
  },
  发抖: {
    name: "发抖",
    category: EmojiCategory.ANIMAL,
    path: "/assets/animal/tremble.png",
  },
  转圈: {
    name: "转圈",
    category: EmojiCategory.ANIMAL,
    path: "/assets/animal/circle.png",
  },

  // 祝福表情
  庆祝: {
    name: "庆祝",
    category: EmojiCategory.BLESSING,
    path: "/assets/blessing/celebrate.png",
  },
  礼物: {
    name: "礼物",
    category: EmojiCategory.BLESSING,
    path: "/assets/blessing/gift.png",
  },
  红包: {
    name: "红包",
    category: EmojiCategory.BLESSING,
    path: "/assets/blessing/red-envelope.png",
  },
  發: {
    name: "發",
    category: EmojiCategory.BLESSING,
    path: "/assets/blessing/get-rich.png",
  },
  福: {
    name: "福",
    category: EmojiCategory.BLESSING,
    path: "/assets/blessing/fortune.png",
  },
  烟花: {
    name: "烟花",
    category: EmojiCategory.BLESSING,
    path: "/assets/blessing/fireworks.png",
  },
  爆竹: {
    name: "爆竹",
    category: EmojiCategory.BLESSING,
    path: "/assets/blessing/firecrackers.png",
  },

  // 其他表情
  嘴唇: {
    name: "嘴唇",
    category: EmojiCategory.OTHER,
    path: "/assets/other/lips.png",
  },
  爱心: {
    name: "爱心",
    category: EmojiCategory.OTHER,
    path: "/assets/other/heart.png",
  },
  心碎: {
    name: "心碎",
    category: EmojiCategory.OTHER,
    path: "/assets/other/broken-heart.png",
  },
  啤酒: {
    name: "啤酒",
    category: EmojiCategory.OTHER,
    path: "/assets/other/beer.png",
  },
  咖啡: {
    name: "咖啡",
    category: EmojiCategory.OTHER,
    path: "/assets/other/coffee.png",
  },
  蛋糕: {
    name: "蛋糕",
    category: EmojiCategory.OTHER,
    path: "/assets/other/cake.png",
  },
  凋谢: {
    name: "凋谢",
    category: EmojiCategory.OTHER,
    path: "/assets/other/wither.png",
  },
  菜刀: {
    name: "菜刀",
    category: EmojiCategory.OTHER,
    path: "/assets/other/knife.png",
  },
  炸弹: {
    name: "炸弹",
    category: EmojiCategory.OTHER,
    path: "/assets/other/bomb.png",
  },
  便便: {
    name: "便便",
    category: EmojiCategory.OTHER,
    path: "/assets/other/poop.png",
  },
  太阳: {
    name: "太阳",
    category: EmojiCategory.OTHER,
    path: "/assets/other/sun.png",
  },
  月亮: {
    name: "月亮",
    category: EmojiCategory.OTHER,
    path: "/assets/other/moon.png",
  },
  玫瑰: {
    name: "玫瑰",
    category: EmojiCategory.OTHER,
    path: "/assets/other/rose.png",
  },
};

/**
 * 获取所有表情数据的辅助函数
 */
function getAllEmojiData(): EmojiInfo[] {
  const result: EmojiInfo[] = [];
  for (const key in EMOJI_DATA) {
    if (Object.prototype.hasOwnProperty.call(EMOJI_DATA, key)) {
      result.push(EMOJI_DATA[key as EmojiName]);
    }
  }
  return result;
}

/**
 * 按类别分组的表情数据
 */
export const EMOJI_CATEGORIES = {
  [EmojiCategory.FACE]: getAllEmojiData().filter(
    emoji => emoji.category === EmojiCategory.FACE,
  ),
  [EmojiCategory.GESTURE]: getAllEmojiData().filter(
    emoji => emoji.category === EmojiCategory.GESTURE,
  ),
  [EmojiCategory.ANIMAL]: getAllEmojiData().filter(
    emoji => emoji.category === EmojiCategory.ANIMAL,
  ),
  [EmojiCategory.BLESSING]: getAllEmojiData().filter(
    emoji => emoji.category === EmojiCategory.BLESSING,
  ),
  [EmojiCategory.OTHER]: getAllEmojiData().filter(
    emoji => emoji.category === EmojiCategory.OTHER,
  ),
} as const;

/**
 * 获取表情图片路径
 * @param name 表情名称
 * @returns 图片路径，如果表情不存在则返回 null
 *
 * @example
 * ```typescript
 * const path = getEmojiPath('微笑'); // 'assets/face/微笑.png'
 * const invalidPath = getEmojiPath('不存在'); // null
 * ```
 */
export function getEmojiPath(name: EmojiName): string | null {
  const emoji = EMOJI_DATA[name];
  return emoji ? emoji.path : null;
}

/**
 * 获取表情信息
 * @param name 表情名称
 * @returns 表情信息对象，如果表情不存在则返回 null
 *
 * @example
 * ```typescript
 * const emoji = getEmojiInfo('微笑');
 * // { name: '微笑', category: EmojiCategory.FACE, path: 'assets/face/微笑.png' }
 * ```
 */
export function getEmojiInfo(name: EmojiName): EmojiInfo | null {
  return EMOJI_DATA[name] || null;
}

/**
 * 根据类别获取表情列表
 * @param category 表情类别
 * @returns 该类别下的所有表情信息
 *
 * @example
 * ```typescript
 * const faceEmojis = getEmojisByCategory(EmojiCategory.FACE);
 * ```
 */
export function getEmojisByCategory(category: EmojiCategory): EmojiInfo[] {
  return EMOJI_CATEGORIES[category];
}

/**
 * 获取所有表情信息
 * @returns 所有表情的信息数组
 *
 * @example
 * ```typescript
 * const allEmojis = getAllEmojis();
 * console.log(`总共有 ${allEmojis.length} 个表情`);
 * ```
 */
export function getAllEmojis(): EmojiInfo[] {
  return getAllEmojiData();
}

/**
 * 搜索表情
 * @param keyword 搜索关键词
 * @returns 匹配的表情信息数组
 *
 * @example
 * ```typescript
 * const results = searchEmojis('笑');
 * // 返回包含 '微笑', '偷笑', '坏笑' 等的表情
 * ```
 */
export function searchEmojis(keyword: string): EmojiInfo[] {
  return getAllEmojiData().filter(emoji => emoji.name.indexOf(keyword) !== -1);
}

/**
 * 检查表情是否存在
 * @param name 表情名称
 * @returns 是否存在该表情
 *
 * @example
 * ```typescript
 * const exists = hasEmoji('微笑'); // true
 * const notExists = hasEmoji('不存在的表情'); // false
 * ```
 */
export function hasEmoji(name: EmojiName): boolean {
  return name in EMOJI_DATA;
}

/**
 * 获取表情名称列表
 * @param category 可选的类别筛选
 * @returns 表情名称数组
 *
 * @example
 * ```typescript
 * const allNames = getEmojiNames();
 * const faceNames = getEmojiNames(EmojiCategory.FACE);
 * ```
 */
export function getEmojiNames(category?: EmojiCategory): string[] {
  if (category) {
    return getEmojisByCategory(category).map(emoji => emoji.name);
  }
  const names: string[] = [];
  for (const key in EMOJI_DATA) {
    if (Object.prototype.hasOwnProperty.call(EMOJI_DATA, key)) {
      names.push(key);
    }
  }
  return names;
}

/**
 * 随机获取表情
 * @param category 可选的类别筛选
 * @returns 随机表情信息
 *
 * @example
 * ```typescript
 * const randomEmoji = getRandomEmoji();
 * const randomFaceEmoji = getRandomEmoji(EmojiCategory.FACE);
 * ```
 */
export function getRandomEmoji(category?: EmojiCategory): EmojiInfo {
  const emojis = category ? getEmojisByCategory(category) : getAllEmojis();
  const randomIndex = Math.floor(Math.random() * emojis.length);
  return emojis[randomIndex];
}

/**
 * 默认导出对象，包含所有主要功能
 */
const WeChatEmojis = {
  // 枚举和类型
  EmojiCategory,

  // 数据
  EMOJI_CATEGORIES,

  // 工具函数
  getEmojiPath,
  getEmojiInfo,
  getEmojisByCategory,
  getAllEmojis,
  searchEmojis,
  hasEmoji,
  getEmojiNames,
  getRandomEmoji,
} as const;

export default WeChatEmojis;
