"use strict";
cc._RF.push(module, 'b01b30rB85Exb1aIQxobbR8', 'result');
// Lwd/script/game/result.js

"use strict";

/**
 * result
 */
var Color_texts = ['#BE1AC1', '#2E6EEE', '#1A1DC1', '#770060'];
var result = cc.Class({
  "extends": cc.Component,
  properties: {
    Picture: {
      "default": null,
      type: cc.Node,
      displayName: "图片"
    },
    frame_tips: {
      "default": null,
      type: cc.Node,
      displayName: "标签框"
    },
    lb_tips: {
      "default": null,
      type: cc.Label,
      displayName: "标签"
    },
    Armature: {
      "default": null,
      type: dragonBones.ArmatureDisplay,
      displayName: "动画"
    },
    layoutText: {
      "default": null,
      type: cc.Node,
      displayName: "文字布局"
    }
  },
  ctor: function ctor() {
    result.inst = this;
    lwd_g.result = result;
  },
  //更新界面ui
  updateUI: function updateUI(Info) {
    this.Picture.active = false;
    this.Armature.node.active = false;

    if (Info) {
      //当前数据
      this.itemInfo = Info; //显示图片

      if (Info.type == "Image") {
        //设置图片
        this.setPicture(Info.groupPic);
      } //显示骨骼动画


      if (Info.type == "Spine") {
        lwd_g.res_mgr.loadSpine(this.Armature, Info);
      }

      this.initRichText(Info.groupLabel);
      this.frame_tips.active = true;
    }
  },
  // 初始化文本
  initRichText: function initRichText(text) {
    this.layoutText.removeAllChildren();

    if (!text) {
      return;
    }

    text = text.replace(/ /g, " |");
    var arr = text.split('|');

    if (arr.length <= 0) {
      return;
    }

    this.layoutText.active = true;
    this.lb_tips.string = arr[0];

    this.lb_tips._forceUpdateRenderData(true);

    for (var i = 0; i < arr.length; i++) {
      if (arr[i + 1] == ' ') {
        continue;
      }

      var node = cc.instantiate(this.lb_tips.node);
      var label = node.getComponent(cc.Label);
      label.string = arr[i];
      node.parent = this.layoutText;

      label._forceUpdateRenderData(true);
    }

    this.layoutText.getComponent(cc.Layout).updateLayout();
    var width = this.layoutText.width;

    if (width > 850) {
      var scale = 850 / width;
      this.layoutText.scale = scale;
    }
  },
  //设置图片
  setPicture: function setPicture(url) {
    var _this = this;

    //图片资源
    if (!url) {
      return;
    }

    lwd_g.res_mgr.getSpriteFrimeByUrl(url, function (list) {
      _this.Picture.getComponent(cc.Sprite).spriteFrame = list;

      _this.photoScare(_this.Picture);

      _this.Picture.active = true;
    });
  },
  //图片适配
  photoScare: function photoScare(node) {
    var maxSize = Math.min(740 / node.height, 400 / node.width);

    if (node.perScale == undefined) {
      node.perScale = node.scaleX;
    } else {
      node.scaleX = node.perScale;
      node.scaleY = node.perScale;
    }

    node.scaleX *= maxSize;
    node.scaleY *= maxSize;
  },
  //播放龙骨动画
  playSpine: function playSpine() {
    this.Armature.playAnimation(lwd_g.data_mgr.dragonName, 1);
  },
  stopSpine: function stopSpine() {
    var _this2 = this;

    this.Armature.dragonAtlasAsset = null;
    this.Armature.dragonAsset = null;
    this.Armature.armatureName = '';
    this.scheduleOnce(function () {
      lwd_g.res_mgr.loadSpine(_this2.Armature, _this2.itemInfo);
    });
  },
  playAudio: function playAudio() {
    var _this3 = this;

    var test = this.itemInfo.group_audio_url; //var test = "http://staging-teach.cdn.ireadabc.com/495324991838775e49e3ca6593d432c9.mp3"

    cc.assetManager.loadRemote(test, null, function (err, clip) {
      _this3.audioID = cc.audioEngine.play(clip, false, 1);
      _this3.audioDuration = cc.audioEngine.getDuration(_this3.audioID);
      _this3.textDuration = _this3.audioDuration / _this3.layoutText.childrenCount;
      _this3.textIndex = 0;

      _this3.schedule(_this3.chengeTextColor, _this3.textDuration, _this3.layoutText.childrenCount, 0.01);
    });
  },
  stopAudio: function stopAudio() {
    cc.audioEngine.stop(this.audioID);
    this.unschedule(this.chengeTextColor);
    this.isPlaying = false; // this.stopSpine();
  },
  chengeTextColor: function chengeTextColor() {
    if (!this.layoutText.children[this.textIndex] || this.textIndex == this.layoutText.childrenCount) {
      if (lwd_g.data_mgr.temPlayAudio.length < 1) {
        if (lwd_g.data_mgr.pageId == lwd_g.data_mgr.data.contentObj.pageArr.length - 1) {
          //显示结算撒花
          lwd_g.eff_well.inst.showEff();
        }
      }

      this.isPlaying = false;
      this.unschedule(this.chengeTextColor);
    }

    this.layoutText.children[this.textIndex] && (this.layoutText.children[this.textIndex].color = new cc.Color().fromHEX(Color_texts[1]));
    this.layoutText.children[this.textIndex - 1] && (this.layoutText.children[this.textIndex - 1].color = new cc.Color().fromHEX(Color_texts[3]));
    this.textIndex++;
  },
  clearTextColor: function clearTextColor() {
    for (var i = 0; i < this.layoutText.children.length; i++) {
      this.layoutText.children[i].color = new cc.Color().fromHEX(Color_texts[3]);
    }
  },
  onBtnText: function onBtnText() {
    this.stopSpine();
    this.clearTextColor();
    this.stopAudio();
    this.playAudio();
    this.playSpine();
  },
  //点击关闭
  onBtnClose: function onBtnClose() {
    this.node.active = false;
    this.stopSpine();
    this.clearTextColor();
    this.stopAudio(); //如果播放列表少于1则显示

    if (this.End == false) {
      return;
    }

    if (lwd_g.data_mgr.temPlayAudio.length < 1) {
      // if (lwd_g.data_mgr.pageId == lwd_g.data_mgr.data.contentObj.pageArr.length - 1) {
      // 	//显示结算撒花
      // 	lwd_g.eff_well.inst.showEff();
      // }
      this.End = false;
    } else {
      //重新随机一组数据
      lwd_g.game.inst.setAudioInfo(1); //设置下一题的状态

      lwd_g.game.inst.allLightDark(1, 0);
      lwd_g.game.inst.allLightDark(2, 2);
    }
  }
});

cc._RF.pop();