// 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: {
    collectedCount: 0,
    layout: null,
    bgHeight: {
      set: function (bgHeight) {
        this._bgHeight = bgHeight;
      },
    },

    count: {
      set: function (count) {
        this._count = count;
        this.setStarCount(count);
      },
    },
  },

  onLoad() {
    this.layout = cc.find("layout", this.node);
    this.initAudio();
  },

  starCountClip: null,
  bigStarClip: null,
  initAudio() {    
		this.starCountClip = cc.find('audio/star_count', this.node).getComponent(cc.AudioSource).clip;
		this.bigStarClip = cc.find('audio/big_star', this.node).getComponent(cc.AudioSource).clip;
  },

  setStarCount(count) {
    console.log(' in setStarCount')
    console.log(' this._count: ', this._count);
    console.log(' count: ', count);
    this.layout.height = this._bgHeight - 100;



    const _starBase = this.createStarBase(this.layout);
    for (let i = 0; i < count; i++) {
      const starBase = cc.instantiate(_starBase);
      starBase.parent = this.layout;
    }
    this.currentStarIdx = 0;
  },

  createStarBase(layout) {
    const paddingY = layout.getComponent(cc.Layout).spacingY;
    const starBase = this.getSprNode("img/star_bg", this.node);
    const star = this.getSprNode("img/star", this.node);
    star.name = "Star";
    const starBig = this.getSprNode("img/icon_bigstar", this.node);
    starBig.name = "StarBig";
    star.addChild(starBig);
    star.active = false;
    starBase.addChild(star);

    starBase.scale = this.between(
      0.5,
      (layout.height / this._count - paddingY) / starBase.height,
      1
    );
    return starBase;
  },

  bigStarArr: null,
  add() {
    this.collectedCount++;

    if (this.collectedCount > this._count) {
      return;
    }

    const audioId = cc.audioEngine.playEffect(this.bigStarClip, false, 0.8);

    const starBase = this.layout.getChildren()[this.collectedCount - 1];
    const star = starBase.getChildByName("Star");
    const starBig = star.getChildByName("StarBig");
    starBig.angle = -90;
    starBig.scale = 1;
    star.active = true;
    star.scaleX = 0.7 / starBase.scale;
    star.scaleY = 1 / starBase.scale;
    star.angle = 90;
    const canvas = cc.find("Canvas");
    const startPos = this.exchangeNodePos(
      star.parent,
      canvas,
      cc.v2(0, -canvas.height / 2)
    );
    const middlePos = this.exchangeNodePos(
      star.parent,
      canvas,
      cc.v2(0, -canvas.height / 4)
    );
    star.x = startPos.x;
    star.y = startPos.y - starBig.height;

    console.log("middlePos = " + JSON.stringify(middlePos));
    const time = 1;
    cc.tween(star)
      .to(0.3, { y: middlePos.y + 80 }, { easing: "quadOut" })
      .to(
        0.1,
        {
          y: middlePos.y + 40,
          scaleX: 1.2 / starBase.scale,
          scaleY: 0.8 / starBase.scale,
        },
        { easing: "quadOut" }
      )
      .to(
        0.1,
        {
          y: middlePos.y,
          scaleX: 1 / starBase.scale,
          scaleY: 1 / starBase.scale,
        },
        { easing: "quadOut" }
      )
      .delay(0.1)
      .to(0.8, { angle: -720, scale: 1 })
      .start();

    cc.tween(star).delay(0.6).to(0.8, { x: 0 }, { easing: "quadIn" }).start();
    cc.tween(star).delay(0.6).to(0.8, { y: 0 }, { easing: "quadOut" }).start();

    cc.tween(starBig)
      .delay(0.6)
      .to(0.8, { scale: 0 }, { easing: "quadOut" })
      .call(() => {
        // this.checkGameEnd();
        const audioId = cc.audioEngine.playEffect(this.starCountClip, false, 0.8);

      })
      .start();

    this.currentStarIdx++;
  },

  exchangeNodePos(targetNode, baseNode, basePos) {
    return targetNode.convertToNodeSpaceAR(
      baseNode.convertToWorldSpaceAR(cc.v2(basePos.x, basePos.y))
    );
  },

  between(a, b, c) {
    return [a, b, c].sort()[1];
  },


  getSprNode(resName, root=null) {
    let resNode;
    if (root) {
      resNode = cc.find(resName, root);
    } else {
      resNode = cc.find("Canvas/res/img/" + resName);
    }
    const node = new cc.Node();
    node.addComponent(cc.Sprite).spriteFrame = resNode.getComponent(
      cc.Sprite
    ).spriteFrame;
    return node;
  }
  

  // update (dt) {},
});
