import { Service } from 'egg';
import { resResult as pubResult } from '../pubUtils/util';
import { unlockFigure } from 'app/pubUtils/itemUtils';
import { RoleType } from '@db/Role';
const csprng = require('csprng');
/**
 * Utils Service
 */
export default class Utils extends Service {
  /**
   * 生成 len 长度的随机字符串
   * @param len 长度
   * @param radix 基数
   */
  public generateStr(len: number, radix = 36) {
    return `${csprng(len, radix)}`;
  }

  public genCode(len) {
    const chars = '123456789ABCDEFGHJKLMNPQRSTWXYZabcdefghijklmnopqrstuvwxyz';
    const charArr = chars.split('');
    let code = '';
    for (let i = 0; i < len; i++) {
      code += charArr[Math.floor(Math.random() * charArr.length)];
    }
    return code;
  }

  /**
   * 生成指定长度的随机数
   * @param len 随机数长度
   */
  public generateNum(len: number) {
    let code = '';
    for (let i = 0; i < len; i++) {
      code += parseInt(`${Math.random() * 10}`);
    }
    return code;
  }

  public resResult(status: {code: number, simStr: string}, data?, customMsg?: string) {
    return pubResult(status, data, customMsg);
  }

  public unlockFigure(roleId: string, conditions: {type: number, paramHid?: number, paramFavourLv?: number, paramSkinId?: number}[], role?: RoleType) {
    return unlockFigure(roleId, conditions, role);
  }

  public log(level: 'DEBUG'|'INFO'|'WARN'| 'ERROR', message: string) {
    const log = this.app.loggers.get('linkLogger');

    if(level == 'DEBUG') {
      log.error(`${message}`);
    } else if (level == 'INFO') {
      log.info(`${message}`);
    } else if (level == 'WARN') {
      log.warn(`${message}`);
    } else if (level == "ERROR") {
      log.error(`${message}`);
    }
  }
}
