/**
 * 数据字典工具类
 */

export interface DictDataType {
  dictType: string;
  label: string;
  value: string | number | boolean;
  colorType?: string;
  cssClass?: string;
}

export interface NumberDictDataType extends DictDataType {
  value: number;
}

export interface StringDictDataType extends DictDataType {
  value: string;
}

// 字典数据缓存（简化版，实际项目中可以从 API 获取）
const dictCache: Map<string, DictDataType[]> = new Map();

/**
 * 获取 dictType 对应的数据字典数组
 * @param dictType 数据类型
 * @returns 数据字典数组
 */
export const getDictOptions = (dictType: string): DictDataType[] => {
  // 如果缓存中有数据，直接返回
  if (dictCache.has(dictType)) {
    return dictCache.get(dictType) || [];
  }

  // 如果没有缓存，返回默认数据（用于开发测试）
  if (dictType === DICT_TYPE.COMMON_STATUS) {
    // 后端约定：0 禁用，1 启用
    return [
      { dictType, label: '禁用', value: 0, colorType: 'danger', cssClass: '' },
      { dictType, label: '启用', value: 1, colorType: 'success', cssClass: '' },
    ];
  }

  // 是否在线状态
  if (dictType === DICT_TYPE.ISONLINE_STATUS) {
    return [
      { dictType, label: '离线', value: '0', colorType: 'danger', cssClass: '' },
      { dictType, label: '在线', value: '1', colorType: 'success', cssClass: '' },
    ];
  }

  // 机柜状态
  if (dictType === DICT_TYPE.CANBINET_STATUS) {
    return [
      { dictType, label: '异常', value: '0', colorType: 'danger', cssClass: '' },
      { dictType, label: '正常', value: '1', colorType: 'success', cssClass: '' },
    ];
  }
  // 仓位类型
  if (dictType === DICT_TYPE.SLOT_TYPE) {
    return [
      { dictType, label: '锁柜', value: '0', colorType: 'primary', cssClass: '' },
      { dictType, label: '挂锁', value: '1', colorType: 'info', cssClass: '' },
    ];
  }
  // 仓位状态
  if (dictType === DICT_TYPE.SLOT_STATUS) {
    return [
      { dictType, label: '正常', value: '0', colorType: 'success', cssClass: '' },
      { dictType, label: '异常', value: '1', colorType: 'danger', cssClass: '' },
    ];
  }
  // 是否被占用
  if (dictType === DICT_TYPE.ISOCCUPIED_STATUS) {
    return [
      { dictType, label: '未占用', value: '0', colorType: 'default', cssClass: '' },
      { dictType, label: '已占用', value: '1', colorType: 'warning', cssClass: '' },
    ];
  }

  // 钥匙状态
  if (dictType === DICT_TYPE.KEY_STATUS) {
    return [
      { dictType, label: '禁用', value: '0', colorType: 'danger', cssClass: '' },
      { dictType, label: '启用', value: '1', colorType: 'success', cssClass: '' },
    ];
  }

  // 站内信模板类型
  if (dictType === DICT_TYPE.SYSTEM_NOTIFY_TEMPLATE_TYPE) {
    return [
      { dictType, label: '通知公告', value: 1, colorType: 'blue', cssClass: '' },
      { dictType, label: '系统消息', value: 2, colorType: 'green', cssClass: '' },
      { dictType, label: '通知消息', value: 3, colorType: 'orange', cssClass: '' },
    ];
  }

  // 用户性别
  if (dictType === DICT_TYPE.SYSTEM_USER_SEX) {
    return [
      { dictType, label: '男', value: 1, colorType: 'blue', cssClass: '' },
      { dictType, label: '女', value: 2, colorType: 'pink', cssClass: '' },
    ];
  }

  // 其他类型返回空数组
  // 实际项目中可以在这里调用 API 获取字典数据
  return [];
};

/**
 * 设置字典数据（用于从 API 获取后缓存）
 * @param dictType 字典类型
 * @param data 字典数据
 */
export const setDictOptions = (dictType: string, data: DictDataType[]): void => {
  dictCache.set(dictType, data);
};

/**
 * 获取整数类型的字典选项
 * @param dictType 字典类型
 * @returns 整数类型的字典选项数组
 */
export const getIntDictOptions = (dictType: string): NumberDictDataType[] => {
  const dictOptions: DictDataType[] = getDictOptions(dictType);
  const dictOption: NumberDictDataType[] = [];
  dictOptions.forEach((dict: DictDataType) => {
    dictOption.push({
      ...dict,
      value: parseInt(String(dict.value)),
    });
  });
  return dictOption;
};

/**
 * 获取字符串类型的字典选项
 * @param dictType 字典类型
 * @returns 字符串类型的字典选项数组
 */
export const getStrDictOptions = (dictType: string): StringDictDataType[] => {
  const dictOptions: DictDataType[] = getDictOptions(dictType);
  const dictOption: StringDictDataType[] = [];
  dictOptions.forEach((dict: DictDataType) => {
    dictOption.push({
      ...dict,
      value: String(dict.value),
    });
  });
  return dictOption;
};

/**
 * 获取布尔类型的字典选项
 * @param dictType 字典类型
 * @returns 布尔类型的字典选项数组
 */
export const getBoolDictOptions = (dictType: string): DictDataType[] => {
  const dictOption: DictDataType[] = [];
  const dictOptions: DictDataType[] = getDictOptions(dictType);
  dictOptions.forEach((dict: DictDataType) => {
    dictOption.push({
      ...dict,
      value: String(dict.value) === 'true',
    });
  });
  return dictOption;
};

/**
 * 获取指定字典类型的指定值对应的字典对象
 * @param dictType 字典类型
 * @param value 字典值
 * @returns 字典对象
 */
export const getDictObj = (dictType: string, value: any): DictDataType | undefined => {
  const dictOptions: DictDataType[] = getDictOptions(dictType);
  for (const dict of dictOptions) {
    if (String(dict.value) === String(value)) {
      return dict;
    }
  }
  return undefined;
};

/**
 * 获得字典数据的文本展示
 * @param dictType 字典类型
 * @param value 字典数据的值
 * @returns 字典名称
 */
export const getDictLabel = (dictType: string, value: any): string => {
  const dictOptions: DictDataType[] = getDictOptions(dictType);
  for (const dict of dictOptions) {
    if (String(dict.value) === String(value)) {
      return dict.label;
    }
  }
  return '';
};

/**
 * 字典类型枚举
 */
export enum DICT_TYPE {
  USER_TYPE = 'user_type',
  COMMON_STATUS = 'common_status',
  TERMINAL = 'terminal', // 终端
  DATE_INTERVAL = 'date_interval', // 数据间隔

  // ========== SYSTEM 模块 ==========
  SYSTEM_USER_SEX = 'system_user_sex',
  SYSTEM_MENU_TYPE = 'system_menu_type',
  SYSTEM_ROLE_TYPE = 'system_role_type',
  SYSTEM_DATA_SCOPE = 'system_data_scope',
  SYSTEM_NOTICE_TYPE = 'system_notice_type',
  SYSTEM_LOGIN_TYPE = 'system_login_type',
  SYSTEM_LOGIN_RESULT = 'system_login_result',
  SYSTEM_SMS_CHANNEL_CODE = 'system_sms_channel_code',
  SYSTEM_SMS_TEMPLATE_TYPE = 'system_sms_template_type',
  SYSTEM_SMS_SEND_STATUS = 'system_sms_send_status',
  SYSTEM_SMS_RECEIVE_STATUS = 'system_sms_receive_status',
  SYSTEM_OAUTH2_GRANT_TYPE = 'system_oauth2_grant_type',
  SYSTEM_MAIL_SEND_STATUS = 'system_mail_send_status',
  SYSTEM_NOTIFY_TEMPLATE_TYPE = 'system_notify_template_type',
  SYSTEM_SOCIAL_TYPE = 'system_social_type',

  // ========== 机柜管理 ==========
  ISONLINE_STATUS = 'isonline_status', // 是否在线
  CANBINET_STATUS = 'canbinet_status', // 机柜状态
  SLOT_TYPE = 'slot_type', // 仓位类型
  SLOT_STATUS = 'slot_status', // 仓位状态
  ISOCCUPIED_STATUS = 'isoccupied_status', // 是否被占用
  
  // ========== ISCS 模块 ==========
  POWER_TYPE = 'power_type', // 能量源（旧）
  POWER_SOURCE = 'power_source', // 能量源
  KEY_STATUS = 'key_status', // 钥匙状态
  POINT_GROUP = 'point_group', // 点位分组
}

