import { asyncCallNetworkApiGet, asyncDelay, jumpToCourseWare, onHomeworkFinish } from "../script/util";
import { MyCocosSceneComponent } from "../script/MyCocosSceneComponent";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SceneComponent extends MyCocosSceneComponent {

  addPreloadImage() {
    this._imageResList.push({ url: this.data.havePic });
    this.data.pageArr.forEach(page => {
      this._imageResList.push({ url: page.bgItem.url });
      this._imageResList.push({ url: page.startBg });
    });
  }

  async onLoadEnd() {
    await this.initData();
    this.updateView();
    this.initListener();

    await this.startStep1();
    await this.startStep2();
  }

  async startStep1() {
    await this.asyncPlayAudioByUrl(this.data.pageArr[this.currentPageIdx].startAudio);
    await asyncDelay(0.3);
    const circle = cc.find('Canvas/step1/pad/circle');
    circle.active = true;
    const progress = cc.find('Canvas/step1/pad/circle/progress');
    await this.asyncCountDown(progress, 3);
    await asyncDelay(0.3);
    await this.checkPage();
  }
  async startStep2() {
    await this.asyncPlayAudioByUrl(this.data.pageArr[this.currentPageIdx].hotZoneItemArr[this.currentPartIdx].audio_url);
    await asyncDelay(0.3);
    await this.checkPart();
    // jumpToCourseWare
  }

  async checkPage() {

  }

  async checkPart() {

  }

  rows: Array<any>;
  step = 0;
  currentPageIdx = 0;
  currentPartIdx = 0;
  async initData() {
    const middleLayer = cc.find('middleLayer');
    let folderId = 20650;
    if (middleLayer) {
      const middleLayerComponent = middleLayer.getComponent('middleLayer');
      folderId = middleLayerComponent.getData('NJ_game_select').folderId;
    }
    console.log('folderId = ' + folderId);

    const resStr = await asyncCallNetworkApiGet('/api/syllabus/v1/list', {
      pid: folderId
    });
    console.log('resStr = ' + resStr);
    const res = JSON.parse(resStr);
    this.rows = res.rows.filter(row => row.name != '指读索引');
  }

  asyncCountDown(node: cc.Node, time: number) {
    return new Promise<void>((resolve, reject) => {
      this.countDown(node, time, () => {
        resolve();
      });
    });
  }
  countDown(node: cc.Node, time: number, callback = null) {
    const progressComponent = node.getComponent(cc.ProgressBar);
    cc.tween(progressComponent)
      .to(time, { progress: 0 })
      .call(() => {
        callback && callback();
      })
      .start();
  }

  updateView() {
    const page = cc.find('Canvas/step1/pad/mask/page');
    const pageUrl = this.data.pageArr[this.currentPageIdx].bgItem.url;
    this.setNodeSpirteFrameByUrl(pageUrl, page, () => {
      page.scale = page.parent.height / page.height;
    });

    const step1 = cc.find('Canvas/step1');
    const step2 = cc.find('Canvas/step2');
    if (this.step == 0) {
      step1.active = true;
      step2.active = false;
    } else {
      step1.active = false;
      step2.active = true;
    }

    const partImg = cc.find('Canvas/step2/pad/mask/page');
    this.setNodeSpirteFrameByUrl(pageUrl, partImg, () => {
      // partImg.scale = partImg.parent.height / partImg.height;
    });

    const book_left = cc.find('Canvas/step1/book_left');
    const book_right = cc.find('Canvas/step1/book_right');
    if (this.currentPageIdx % 2 == 0) {
      book_left.active = true;
      book_right.active = false;
    } else {
      book_left.active = false;
      book_right.active = true;
    }

    const bgNode = cc.find('Canvas/bg');
    bgNode.color = cc.color(255, 255, 255);
    const startBg = this.data.pageArr[this.currentPageIdx].startBg;
    this.setNodeSpirteFrameByUrl(startBg, bgNode);
  }

  initListener() {

  }

  asyncPlayAudioByUrl(url: string): Promise<void> {
    return new Promise((resolve, reject) => {
      this.playAudioByUrl(url, () => {
        resolve();
      });
    });
  }

  setNodeSpirteFrameByUrl(url: string, node: cc.Node, callBack = null) {
    cc.assetManager.loadRemote(url, (err, tex) => {
      node.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(tex);
      callBack && callBack();
    });
  }
}
