import { RES } from "../module/RES";
import { layers } from "../module/views/layers";
import Tween = FYGE.Tween;
import Ease = FYGE.Ease;

/**
 * 定制，记录一些全局量和通用方法
 */
export class Tools {
  public static gameData: {
    needGuide?: boolean;
  }

  /**
   * 缓存key
   */
  public static cacheKey: string = "guideThreeSquirrels";

  public static isMember: boolean;

  /**
   * 全局数据，重要数据
   */
  public static globalData: {
    avatar: string;
    nickName: string;
    activityId: string;
    openId: string;
    inviteId: string;
    isFollow: boolean;
  };

  public static gameInfo: {
    gameCounts?: number;
    isEnd?: boolean;
  };
  /**
   * 通过res里的名字生成一个sprite
   * @param imageName
   * @param x
   * @param y
   */
  public static getSprite(
    imageName: string,
    x: number = 0,
    y: number = 0
  ): FYGE.Sprite {
    var sprite = new FYGE.Sprite(RES.getRes(imageName));
    sprite.x = x;
    sprite.y = y;
    return sprite;
  }

  /**
   *
   * @param txt 内容
   * @param size 字号
   * @param color 颜色 #打头
   * @param textWidth 文本宽度
   * @param align 文本对齐方式
   * @param x
   * @param y
   */
  public static getText(
    txt: string,
    size: number,
    color: string = "#000000",
    align: FYGE.TEXT_ALIGN = FYGE.TEXT_ALIGN.LEFT,
    textWidth: number = 0,
    x: number = 0,
    y: number = 0
  ): FYGE.TextField {
    var text = new FYGE.TextField();
    text.fillColor = color;
    text.size = size;
    text.textWidth = textWidth;
    text.textAlign = align;
    text.x = x;
    text.y = y;
    text.text = txt;
    return text;
  }

  /**
   * 已左上角作为原点
   * @param width
   * @param height
   * @param color
   * @param alpha
   * @param radius
   * @param x
   * @param y
   */
  public static getRect(
    width: number,
    height: number,
    color: number = 0xff0000,
    alpha: number = 1,
    radius: number = 0,
    x: number = 0,
    y: number = 0
  ): FYGE.Graphics {
    var g = new FYGE.Graphics();
    g.beginFill(color);
    g.alpha = alpha;
    if (!radius) {
      g.drawRect(0, 0, width, height);
    } else {
      g.drawRoundedRect(0, 0, width, height, radius);
    }
    g.endFill();
    g.position.set(x, y);
    return g;
  }
  /**
   * 中心原点
   * @param radius
   * @param color
   * @param alpha
   * @param x
   * @param y
   */
  public static getCircle(
    radius: number = 10,
    color: number = 0xff0000,
    alpha: number = 1,
    x: number = 0,
    y: number = 0
  ): FYGE.Graphics {
    var g = new FYGE.Graphics();
    g.beginFill(color);
    g.alpha = alpha;
    g.drawCircle(0, 0, radius);
    g.endFill();
    g.position.set(x, y);
    return g;
  }

  /**
   * 高度适配
   * 遮挡上面的情况
   * 顶部部ui要适配
   * @param maxDis 最大距离
   * @param minDis 最小距离
   * @param type 页面适配类型
   * @return y值
   */
  public static getAdjustHeight(
    maxDis: number,
    minDis: number,
    type: "top" | "mid" | "bot" = "bot"
  ): number {
    //图层偏移量
    var offsetY: number = (() => {
      //顶部适配。layers会被置顶，返回0
      if (type == "top") return 0;
      //底部适配。layers会置底，返回两倍的
      if (type == "bot") return layers.stageOffsetY * 2;
      return layers.stageOffsetY;
    })();
    //小于1206时固定距离顶部高度
    if (layers.stageHeight <= 1206) return minDis + offsetY;
    //大于1206时取插值
    return (
      ((layers.stageHeight - 1206) / (1624 - 1206)) * (maxDis - minDis) +
      minDis +
      offsetY
    );
  }

  /**
   * 修改皮肤上的文本对齐方式，原先默认是左的，多汗行的时候计算有误，待查
   * @param text
   * @param align
   * @param textWidth
   */
  public static changeTextAlign(
    text: FYGE.TextField,
    align: FYGE.TEXT_ALIGN,
    textWidth?: number
  ) {
    if (align == FYGE.TEXT_ALIGN.LEFT) return;
    text.textAlign = align;
    //没有就原先的
    textWidth = textWidth || text.textWidth;
    //修改位置
    if (align == FYGE.TEXT_ALIGN.CENTER)
      text.x -= (textWidth - text.textWidth) / 2;
    if (align == FYGE.TEXT_ALIGN.RIGHT) text.x -= textWidth - text.textWidth;
    text.textWidth = textWidth;
  }

  public static customToast(
    target: FYGE.DisplayObject,
    call: Function = () => 0,
    time: number = 2000,
    showTime: number = 300
  ) {
    Tween.get(target)
      .set({ alpha: 0, visible: true })
      .to({ alpha: 1 }, showTime, Ease.quadIn)
      .wait(time)
      .to({ alpha: 0 }, showTime, Ease.quadOut)
      .set({ alpha: 0, visible: false })
      .call(() => {
        call();
      });
  }

  /**
   * 延时防连点
   * @param target
   * @param {number} delay 默认2000毫秒
   */
  public static btnDelay(target: FYGE.Container, delay: number = 2000) {
    target.mouseEnable = false;
    target.mouseChildren = false;
    setTimeout(() => {
      target.mouseEnable = true;
      target.mouseChildren = true;
    }, delay);
  }

  /**
   * 根据名字获取0到9的贴图
   * 位图字每次都写太烦了
   * @param name
   */
  public static getNumTextures(name: string): { [key: number]: FYGE.Texture } {
    var arr = {};
    for (var i = 0; i <= 9; i++) arr[i] = RES.getRes(name + i + ".png");
    return arr;
  }

  /**
   * 获得距离底部的百分比高度，还要减个自身的高度，自行处理
   * @param {number} percent  百分比
   * @returns {number}
   */
  static getAdjustBottomHeight(percent: number): number {
    return (
      layers.stageHeight - layers.stageHeight * percent + layers.stageOffsetY
    ); //stageOffsetY加不加取决于页面适配类型
  }

  /**
   * 获得距离顶部的百分比高度
   * @param {number} percent  百分比
   * @returns {number}
   */
  static getAdjustTopHeight(percent: number): number {
    return layers.stageHeight * percent + layers.stageOffsetY; //stageOffsetY加不加取决于页面适配类型
  }
}
