// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
  extends: cc.Component,

  properties: {
    // foo: {
    //     // ATTRIBUTES:
    //     default: null,        // The default value will be used only when the component attaching
    //                           // to a node for the first time
    //     type: cc.SpriteFrame, // optional, default is typeof default
    //     serializable: true,   // optional, default is true
    // },
    // bar: {
    //     get () {
    //         return this._bar;
    //     },
    //     set (value) {
    //         this._bar = value;
    //     }
    // },

    // 调试模式
    isDebug: false,
    // 是否可以点击
    canClick: true,
    // 是否可以转动
    isRolling: false,
    // 是否正在闪烁
    isBlinking: false,
    // 结果状态 转完之后禁止点击
    isLock: false,
  },

  // LIFE-CYCLE CALLBACKS:

  // onLoad () {},

  start() {
    this.initEventLinister();
  },

  

  // 初始化转盘
  initTurntable(debugMode = false) {
    this.isDebug = debugMode;
  },
  
  initEventLinister() {
    // console.log("初始化事件监听");
    cc.find("dice", this.node).on(cc.Node.EventType.TOUCH_START, () => {
      if(this.canClick && !this.isRolling && !this.isLock) {
        this.node.emit("Rolling_Start")
      } else {
        // cc.log(`已屏蔽转盘点击 canClick:${this.canClick} isRolling:${this.isRolling} isLock:${this.isLock}`)
      }
    })
  },

  // 转动到制定数字
  rollNum(num) {
    const dice = cc.find("/dice", this.node);
    if (this.isRolling) {
      return;
    }
    return new Promise((resolve, reject) => {
      const targetAngle = Math.ceil(dice.angle / 360 + 1) * 360;
      const time = (targetAngle - 360 - dice.angle) / 360;
      this.isRolling = true;

      if (!this.isDebug) {
        cc.tween(dice)
          .by(1.5, { angle: 360 }, { easing: "cubicIn" })
          .to(time, { angle: targetAngle })
          .by(0.5, { angle: 360 })
          .by(0.5, { angle: 360 })
          .by(0.5, { angle: 360 })
          .by(0.5, { angle: 360 })
          .by((0.5 / 6) * num, { angle: 60 * num })
          .by(1.5, { angle: 360 }, { easing: "cubicOut" })
          .call(() => {
            this.isRolling = false;
            this.isLock = true;
            this.node.emit("Rolling_End");
            resolve(null);
          })
          .start();
      } else {
        //TODO:调试加速
        cc.tween(dice)
          .by(0.3, { angle: 360 }, { easing: "cubicIn" })
          .to(time, { angle: targetAngle })
          .by(0.1, { angle: 360 })
          .by(0.1, { angle: 360 })
          .by(0.1, { angle: 360 })
          .by(0.1, { angle: 360 })
          .by((1 / 6) * num, { angle: 60 * num })
          .by(0.3, { angle: 360 }, { easing: "cubicOut" })
          .call(() => {
            this.isRolling = false;
            this.isLock = true;
            this.node.emit("Rolling_End");
            resolve(null);
          })
          .start();
      }
    });
  },

  // 解锁
  reset() {
    this.isLock = false;
  },
  
  // 放大提示
  blink() {
    this.isBlinking = true;
    const dice = cc.find("hatBlink", this.node);
    cc.tween(dice)
      .set({ opacity: 0 })
      .to(0.2, { opacity: 255 })
      .to(0.2, { opacity: 0 })
      .union()
      .repeatForever()
      .start();

      this.node.children.forEach((node) => {
      cc.tween(node)
        .set({ scale: 1 })
        .to(0.2, { scale: 1.1 })
        .to(0.2, { scale: 1.0 })
        .union()
        .repeatForever()
        .start();
    });

    const dice2 = cc.find("dice", this.node);
  },

  // 停止放大提示
  stopBlink() {
    this.isBlinking = false;
    const dice = cc.find("hatBlink", this.node);
    dice.stopAllActions();
    this.node.children.forEach((node) => {
      node.stopAllActions();
      node.scale = 1;
      node.opacity = 255;
    });
    const dice2 = cc.find("diceBg/dice", this.node);
  },

  // 禁用
  disable() {
    this.canClick = false;
    this.node.children.forEach((node) => {
      node.color = cc.color(150, 150, 150);
    });
  },

  // 启用
  enable() {
    this.canClick = true;
    this.node.children.forEach((node) => {
      node.color = cc.color(255, 255, 255);
    });
  }
  // update (dt) {},
});
