"use strict";
cc._RF.push(module, '7f42357OrpEersTkWS/ZJPG', 'ItemPage');
// OP13/script/ItemPage.js

"use strict";

var _require = require('./utils'),
    loadSpine = _require.loadSpine;

var colorMap = {
  "A": ["#1f356b", "#ffffff", "#bd2c37"],
  "B": ["#fff8d0", "#000000", "#ffb93b"]
};
cc.Class({
  "extends": require('BaseUI'),
  properties: {
    dragonBone: dragonBones.ArmatureDisplay,
    skeleton: sp.Skeleton
  },
  onLoad: function onLoad() {
    this._super();

    this.sprImage = this.nodeDict.sprImage.getComponent(cc.Sprite);
    this.labText = this.nodeDict.labText.getComponent(cc.RichText);
    this.labTextTitle = this.nodeDict.labTextTitle.getComponent(cc.RichText);
  },
  init: function init(data, isCover, pageIndex) {
    var _this = this;

    return new Promise(function (resolve, reject) {
      _this.data = data;
      _this.data.pageType = "spine";
      _this.pageIndex = pageIndex;
      _this.isCover = isCover;
      _this.labText.string = '';
      _this.labTextTitle.string = '';

      if (_this.data.pageDragonBone.animType == 'spine') {
        _this.loadSpineAnim().then(function () {
          resolve('');
        });
      } else {
        _this.loadDragonBoneAnim().then(function () {
          resolve('');
        });
      }
    });
  },
  playSpine: function playSpine() {
    var _this2 = this;

    return new Promise(function (resolve, reject) {
      if (_this2.data.pageDragonBone.animType == 'spine') {
        var skeleton = _this2.skeleton;
        skeleton.setToSetupPose();
        setTimeout(function () {
          skeleton.setAnimation(0, _this2.animationName, false);
          skeleton.setCompleteListener(function () {
            GameData.isPlaying = false;
            resolve('');
          });
        }, 1);
      } else {
        _this2.dragonBone.playAnimation(_this2.animationName, 1);

        _this2.dragonBone.on(dragonBones.EventObject.COMPLETE, function (evt) {
          GameData.isPlaying = false;
          resolve('');
        });
      }
    });
  },
  playAudio: function playAudio() {
    var _this3 = this;

    if (this.data.pageAudio) {
      cc.assetManager.loadRemote(this.data.pageAudio, null, function (err, clip) {
        _this3.audioID = cc.audioEngine.play(clip, false, 1);
        _this3.audioDuration = cc.audioEngine.getDuration(_this3.audioID);
      });
    }
  },
  stopAudio: function stopAudio() {
    cc.audioEngine.stop(this.audioID);
    this.unschedule(this.chengeTextColor);
    GameData.isPlaying = false;
  },
  loadBg: function loadBg() {
    var _this4 = this;

    var url = this.data.pageImage;
    if (!url) return;
    cc.assetManager.loadRemote(url, function (error, texture) {
      if (error) {
        console.log(error);
      }

      _this4.sprImage.spriteFrame = new cc.SpriteFrame(texture);

      _this4.setBgScale();
    });
  },
  loadSpineAnim: function loadSpineAnim() {
    var _this5 = this;

    return new Promise(function (resolve, reject) {
      var tex = _this5.data.pageDragonBone.texPngData.url;
      var atlas = _this5.data.pageDragonBone.texJsonData.url;
      var ske = _this5.data.pageDragonBone.skeJsonData.url;
      loadSpine(_this5.skeleton, _this5.data.pageDragonBone.texPngData.name, {
        tex: tex,
        atlas: atlas,
        ske: ske
      }).then(function (_ref) {
        var width = _ref.width,
            height = _ref.height;
        _this5.skeleton.node.width = width;
        _this5.skeleton.node.height = height;
        resolve('');
        _this5.animationName = _this5.skeleton.animationNames[0];
        console.log('width: ', _this5.skeleton.node.width);
        console.log('height~~~: ', _this5.skeleton.node.height);
        console.log('this.skeleton.node: ', _this5.skeleton.node);

        _this5.setSpineScale(_this5.skeleton.node, _this5.skeleton.node);
      });
    });
  },
  loadDragonBoneAnim: function loadDragonBoneAnim() {
    var _this6 = this;

    return new Promise(function (resolve, reject) {
      var imageUrl = _this6.data.pageDragonBone.texPngData.url;
      var skeUrl = _this6.data.pageDragonBone.skeJsonData.url;
      var atlasUrl = _this6.data.pageDragonBone.texJsonData.url;
      cc.assetManager.loadAny([{
        url: atlasUrl,
        ext: '.txt'
      }, {
        url: skeUrl,
        ext: '.txt'
      }], function (error, assets) {
        if (error) return resolve('');
        if (imageUrl == '') console.log(_this6.data);
        cc.assetManager.loadRemote(imageUrl, function (error, texture) {
          if (error) return resolve('');
          var atlas = new dragonBones.DragonBonesAtlasAsset();
          atlas._uuid = atlasUrl;
          atlas.atlasJson = assets[0];
          atlas.texture = texture;
          var asset = new dragonBones.DragonBonesAsset();
          asset._uuid = skeUrl;
          asset.dragonBonesJson = assets[1];
          _this6.dragonBone.dragonAtlasAsset = atlas;
          _this6.dragonBone.dragonAsset = asset;
          var data = asset._dragonBonesJsonData.armature[0];
          if (!data) return resolve('');
          _this6.dragonBone.armatureName = data.name;
          _this6.animationName = data.animation[0].name;
          _this6.dragonBone.animationName = _this6.animationName; // this.dragonBone.premultipliedAlpha = true;
          // atlas.texture.setPremultiplyAlpha(true);

          console.log('data.canvas:', data.canvas);

          _this6.setSpineScale(data.canvas, _this6.dragonBone.node);

          return resolve('');
        });
      });
    });
  },
  // 缩放
  setBgScale: function setBgScale() {
    var bgWidth = 382;
    var bgHeight = 540;

    if (this.nodeDict.sprImage.width != bgWidth || this.nodeDict.sprImage.height != bgHeight) {
      var designScale = bgWidth / bgHeight;
      var scale = this.nodeDict.sprImage.width / this.nodeDict.sprImage.height;

      if (scale > designScale) {
        this.nodeDict.sprImage.scale = bgWidth / this.nodeDict.sprImage.width;
      } else {
        this.nodeDict.sprImage.scale = bgHeight / this.nodeDict.sprImage.height;
      }
    }
  },
  setSpineScale: function setSpineScale(size, node) {
    // return;
    if (!size) {
      return;
    }

    var bgWidth = 482;
    var bgHeight = 489;

    if (size.width != bgWidth || size.height != bgHeight) {
      var designScale = bgWidth / bgHeight;
      var scale = size.width / size.height;

      if (scale > designScale) {
        node.scale = bgWidth / size.width;
      } else {
        node.scale = bgHeight / size.height;
      }
    }
  } // update (dt) {},

});

cc._RF.pop();