import {
  Game,
  TouchSprite,
  RandomInt,
  Between,
  MyLabel,
  blinkItem,
  stopBlinkItem,
  asyncDelayTime,
  asyncTweenChange,
} from './Game';

import {
  Label,
  jelly,
  showPopParticle,
  tweenChange,
  rotateItem,
  scaleItem,
  delayCall,
  hideItem,
  showItem,
  moveItem,
  shake,
  ShapeRect,
} from './../Unit';

import TWEEN from '@tweenjs/tween.js';

import { defaultData } from './DefaultData';


export class MyGame extends Game {

  images = null;

  data = null;
  status = null;

  initData(data) {
    this.images = data.images;

    this.status = {};

    if (!data.data || Object.is(data.data, {}) || !data.data.tittle) {
      this.data = defaultData;
    } else {
      this.data = data.data;

      let imgUrlList = [];
      let audioUrlList = [];

      // imgUrlList.push(...this.data.bottomLineCards.map(card => card.img));
      // audioUrlList.push(this.data.tittle.audio);

      imgUrlList = imgUrlList.filter((data) => data != null && data != '');
      audioUrlList = audioUrlList.filter((data) => data != null && data != '');

      this.preLoadData(imgUrlList, audioUrlList);
    }
  }

  preLoadData(imgUrlList, audioUrlList) {
    imgUrlList.forEach((data) => {
      this._parent.addUrlToImages(data);
    });
    audioUrlList.forEach((data) => {
      this._parent.addUrlToAudioObj(data);
    });
  }

  mapScale;
  initView() {
    this.mapScale = this._parent.mapScale;

    // 初始化背景
    this.initBg();

    // 初始化标题
    this.initTittle();

    // 初始化中间部分
    this.initMiddle();
  }

  bg;
  initBg() {
    const bg = new TouchSprite();
    bg.init(this.images.get('img_grey_bg'));
    bg.x = this._parent.canvasWidth / 2;
    bg.y = this._parent.canvasHeight / 2;
    bg.setScaleXY(this.mapScale);
    bg.alpha = 0.5;
    this.addChild(bg);
    this.bg = bg;

  }

  initTittle() {
    const titleBg = new TouchSprite();
    titleBg.init(this.images.get('img_title_bg'));
    titleBg.x = titleBg.width / 2 * this.mapScale;
    titleBg.y = titleBg.height / 2 * this.mapScale;
    titleBg.setScaleXY(this.mapScale);
    this.addChild(titleBg);

    const titleLetter = new MyLabel();
    titleLetter.x = titleBg.width * 1 / 8;
    titleLetter.y = 0;
    titleLetter.fontSize = 48;
    titleLetter.fontColor = "#facf46";
    titleLetter.fontName = 'BerlinSansFBDemi';
    titleLetter.anchorX = 0;
    titleLetter.anchorY = 0.5;
    titleLetter.text = this.data.titleLetter;
    titleBg.addChild(titleLetter);

    const titleLabel = new Label();
    titleLabel.x = titleBg.width * 5 / 8;
    titleLabel.y = 0;
    titleLabel.fontSize = 36;
    titleLabel.fontColor = "#000000";
    titleLabel.fontName = 'FuturaStd';
    titleLabel.anchorX = 0;
    titleLabel.anchorY = 0.5;
    titleLabel.text = this.data.title;
    titleBg.addChild(titleLabel);
  }

  initMiddle() {
  }
}
