import { Service } from 'egg';
import * as pubUtils from '@pubUtils/util';
// import { HeroUpdate } from '@db/Hero';
// import { SkinUpdate } from '@db/Skin';
// import { getInitRoleInfo } from '@pubUtils/roleUtil';
const csprng = require('csprng');

/**
 * Utils Service
 */
export default class Utils extends Service {
  
  constructor(args) {
    super(args);

    // this.setInitRole();
  }

  // private initHeroes: Map<number, HeroUpdate> = new Map(); // hid => hero
  // private initSkins: Map<number, SkinUpdate> = new Map(); // hid => skin

  // public setInitRole() {
  //   let result = getInitRoleInfo();
  //   let { heroes, skins } = result;
  //   for(let hero of heroes) {
  //       this.initHeroes.set(hero.hid, hero);
  //   }
  //   for(let skin of skins) {
  //       this.initSkins.set(skin.hid, skin);
  //   }
  // }

  // public getInitHeroById(hid: number) {
  //     return {
  //         heroInfo: this.initHeroes.get(hid),
  //         skinInfo: this.initSkins.get(hid)
  //     }
  // }

  /**
   * 生成 len 长度的随机字符串
   * @param len 长度
   * @param radix 基数
   */
  public generateStr(len: number, radix = 36) {
    return `${csprng(len, radix)}`;
  }

  public genCode(len: number) {
    return pubUtils.genCode(len)
  }

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

  public checkActivityData(data: any, type: number) {
    return { data, type }
  }

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

    console.log(message);
    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}`);
    }
  }

  public async transmit(env: string, route: string, stream: any) {
    const { ctx } = this;
    console.log('****transmit', stream);
    const result = await ctx.curl(`http://127.0.0.1:7002${route}`, {
      method: 'POST',
      files: stream,
      
      headers: {
        token: ctx.request.headers.token
      }
    });
    console.log(env, result)
  }
}
