"use strict";
cc._RF.push(module, 'e1d8bdYeklMZbNx/+khC3nb', 'FT_06');
// FT_06/scene/FT_06.js

"use strict";

var _utils = require("../script/utils");

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

var defaultData = require('../script/defaultData');

cc.Class({
  "extends": cc.Component,
  properties: {},
  ctor: function ctor() {
    this._imageResList = [];
    this._audioResList = [];
    this._onLoadFinishFunc = null;
  },
  start: function start() {
    var _this = this;

    cc.debug.setDisplayStats(false);
    this.data = defaultData;
    this.getData(function (data) {
      if (data) {
        _this.data = data;
      }

      _this.preloadItem();
    });
  },
  getData: function getData(func) {
    if (window && window.courseware) {
      window.courseware.getData(func, 'Z_05_nxuan1');
    } else {
      func(defaultData);
    }
  },
  _imageResList: null,
  _audioResList: null,
  preloadItem: function preloadItem() {
    this.addPreloadImage();
    this.addPreloadAudio();
    this.preload();
    this.onLoadFinish();
  },
  addPreloadImage: function addPreloadImage() {// this._audioResList.push(this.data.tittleImg);
  },
  addPreloadAudio: function addPreloadAudio() {
    this._audioResList.push({
      url: this.data.tittleAudio
    });
  },
  preload: function preload() {
    var _this2 = this;

    var preloadArr = this._imageResList.concat(this._audioResList);

    cc.assetManager.loadAny(preloadArr, null, null, function (err, data) {
      _this2.hideAirClassLoading();
    });

    if (preloadArr.length == 0) {
      this.hideAirClassLoading();
    }
  },
  hideAirClassLoading: function hideAirClassLoading() {
    if (window && window["air"]) {
      window["air"].hideAirClassLoading();
      console.log('hideAirClassLoading');
    }
  },
  onLoadFinish: function onLoadFinish() {
    this.initStatus();
    this.initNodes();
    this.initListeners();
    this.updateButtons();
    this.updateBgSize();

    if (this._onLoadFinishFunc && typeof this._onLoadFinishFunc == 'function') {
      this._onLoadFinishFunc();
    }

    this.showQuestion(true);
  },
  initStatus: function initStatus() {
    this.status = {
      currentQuestionIdx: 0
    };
  },
  initNodes: function initNodes() {
    var tittleLabelNode = cc.find('Canvas/bgFrount/leef/TittleLabelNode');
    tittleLabelNode.getComponent(cc.Label).string = this.data.tittleTxt;
    var canvas = cc.find('Canvas');
    var optionPosListBase = cc.find('Canvas/OptionPosListBase');
    optionPosListBase.scaleX = canvas.width / optionPosListBase.width;
    optionPosListBase.scaleY = canvas.height / optionPosListBase.height;
  },
  updateButtons: function updateButtons() {
    var btnBefore = cc.find('Canvas/BtnNode/BtnBefore');
    btnBefore.color = cc.color(255, 255, 255);

    if (this.status.currentQuestionIdx <= 0) {
      btnBefore.color = cc.color(150, 150, 150);
    }

    var btnNext = cc.find('Canvas/BtnNode/BtnNext');
    btnNext.color = cc.color(255, 255, 255);

    if (this.status.currentQuestionIdx + 1 >= this.data.questionList.length) {
      btnNext.color = cc.color(150, 150, 150);
    }
  },
  initListeners: function initListeners() {
    var _this3 = this;

    this.node.on('scale-changed', function () {
      console.log('scale-changed');

      _this3.initNodes();

      _this3.showQuestion();

      _this3.updateButtons();
    });
    this.node.on('size-changed', function () {
      console.log('size-changed');

      _this3.initNodes();

      _this3.showQuestion();

      _this3.updateButtons();
    });
    (0, _utils.addButtonListener)(cc.find('Canvas/BtnNode/BtnBefore'), function () {
      if (_this3.status.currentQuestionIdx <= 0) {
        return false;
      }

      _this3.status.currentQuestionIdx--;

      _this3.showQuestion();

      _this3.updateButtons();

      return true;
    });
    (0, _utils.addButtonListener)(cc.find('Canvas/BtnNode/BtnNext'), function () {
      if (_this3.status.currentQuestionIdx + 1 >= _this3.data.questionList.length) {
        return false;
      }

      _this3.status.currentQuestionIdx++;

      _this3.showQuestion();

      _this3.updateButtons();

      return true;
    });
    (0, _utils.addButtonListener)(cc.find('Canvas/BtnNode/BtnRefresh'), function () {
      _this3.showQuestion();

      return true;
    });
    cc.find('Canvas/bgFrount/leef/TittleLabelNode').on('click', function () {
      _this3.playIncomeAudio();
    });
    cc.find('Canvas/QuestionLabelBg/QuestionLabelNode').on('click', function () {
      _this3.playQuestionAudio();
    });
  },
  updateBgSize: function updateBgSize() {},
  showQuestion: function showQuestion(isFirstIncome) {
    var _this4 = this;

    return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              if (isFirstIncome === void 0) {
                isFirstIncome = false;
              }

              _this4.resetQuestionBoard();

              _this4.showOptions();

              _this4.questionBoardDown();

              if (!isFirstIncome) {
                _context.next = 9;
                break;
              }

              _context.next = 7;
              return _this4.playIncomeAudio();

            case 7:
              _context.next = 9;
              return (0, _utils.asyncDelay)(1);

            case 9:
              _this4.playQuestionAudio();

            case 10:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }))();
  },
  playIncomeAudio: function playIncomeAudio() {
    var _this5 = this;

    return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
      return regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              _context2.next = 2;
              return (0, _utils.asyncPlayEffectByUrl)(_this5.data.tittleAudio);

            case 2:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    }))();
  },
  playQuestionAudio: function playQuestionAudio() {
    var _this6 = this;

    return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
      var audioUrl;
      return regeneratorRuntime.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              audioUrl = _this6.data.questionList[_this6.status.currentQuestionIdx].questionAudio;
              _context3.next = 3;
              return (0, _utils.asyncPlayEffectByUrl)(audioUrl);

            case 3:
            case "end":
              return _context3.stop();
          }
        }
      }, _callee3);
    }))();
  },
  showOptions: function showOptions() {
    var _this7 = this;

    for (var i = 0; i < 4; i++) {
      var optionNode = cc.find("Canvas/OptionPosListBase/optionNode_" + i);

      if (optionNode) {
        optionNode.removeFromParent();
      }
    }

    var optionList = this.data.questionList[this.status.currentQuestionIdx].optionList;
    optionList.forEach( /*#__PURE__*/function () {
      var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5(data, idx) {
        var optionNode, optionPosListBase, posNode, btnFlower, btnFlowerP, optionSpeakerBtn, animeNode;
        return regeneratorRuntime.wrap(function _callee5$(_context5) {
          while (1) {
            switch (_context5.prev = _context5.next) {
              case 0:
                optionNode = cc.instantiate(cc.find("OptionNodeBase_" + optionList.length));
                optionNode.name = "optionNode_" + idx;
                optionNode.parent = cc.find('Canvas/OptionPosListBase');
                optionNode.addComponent(cc.Button);
                optionNode.canClick = true;
                optionNode.on('click', function () {
                  _this7.onOptionClicked(optionNode, idx);
                });
                optionPosListBase = cc.find('Canvas/OptionPosListBase');
                optionNode.scaleX = Math.min(1, optionPosListBase.scaleX, optionPosListBase.scaleY) / optionPosListBase.scaleX;
                optionNode.scaleY = Math.min(1, optionPosListBase.scaleX, optionPosListBase.scaleY) / optionPosListBase.scaleY;
                posNode = cc.find("Canvas/OptionPosListBase/Option_" + optionList.length + "_" + idx);
                optionNode.x = posNode.x;
                optionNode.y = posNode.y;
                btnFlower = cc.find('OptionSpeakerBtn/BtnFlower', optionNode);
                btnFlowerP = cc.find('OptionSpeakerBtn/BtnFlower_P', optionNode);
                optionSpeakerBtn = cc.find('OptionSpeakerBtn', optionNode);
                optionSpeakerBtn.on('click', /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() {
                  var animeNode;
                  return regeneratorRuntime.wrap(function _callee4$(_context4) {
                    while (1) {
                      switch (_context4.prev = _context4.next) {
                        case 0:
                          if (optionNode.canClick) {
                            _context4.next = 2;
                            break;
                          }

                          return _context4.abrupt("return");

                        case 2:
                          if (!optionSpeakerBtn.canNotClick) {
                            _context4.next = 4;
                            break;
                          }

                          return _context4.abrupt("return");

                        case 4:
                          optionSpeakerBtn.canNotClick = true;
                          (0, _utils.playEffect)('按钮');
                          _context4.next = 8;
                          return (0, _utils.jelly)(optionSpeakerBtn);

                        case 8:
                          (0, _utils.blinkNode)(optionNode, btnFlowerP, btnFlower);
                          _context4.next = 11;
                          return (0, _utils.asyncPlayEffectByUrl)(data.optionAudio);

                        case 11:
                          _context4.next = 13;
                          return (0, _utils.asyncLoadDragonBoneAnime)(cc.find('OptionDragonBoneBaseNode', optionNode), data.optionDragonBone);

                        case 13:
                          animeNode = _context4.sent;

                          if (animeNode) {
                            (0, _utils.asyncPlayDragonBoneAnimation)(animeNode, 'normal', 1);
                          }

                          (0, _utils.stopBlinkNode)(optionNode, btnFlowerP, btnFlower);
                          optionSpeakerBtn.canNotClick = false;

                        case 17:
                        case "end":
                          return _context4.stop();
                      }
                    }
                  }, _callee4);
                })));
                _context5.next = 18;
                return (0, _utils.asyncLoadDragonBoneAnime)(cc.find('OptionDragonBoneBaseNode', optionNode), data.optionDragonBone);

              case 18:
                animeNode = _context5.sent;

                if (animeNode) {
                  _context5.next = 21;
                  break;
                }

                return _context5.abrupt("return");

              case 21:
                animeNode.scale = Math.min(260 / animeNode.width, 360 / animeNode.height, 1);
                animeNode.name = 'animeNode';
                animeNode.addComponent(cc.Button);
                animeNode.on('click', function () {
                  _this7.onOptionClicked(optionNode, idx);
                });

              case 25:
              case "end":
                return _context5.stop();
            }
          }
        }, _callee5);
      }));

      return function (_x, _x2) {
        return _ref.apply(this, arguments);
      };
    }());
  },
  onOptionClicked: function onOptionClicked(optionNode, idx) {
    var _this8 = this;

    return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6() {
      return regeneratorRuntime.wrap(function _callee6$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              if (optionNode.canClick) {
                _context6.next = 2;
                break;
              }

              return _context6.abrupt("return");

            case 2:
              optionNode.canClick = false;

              if (!(_this8.data.questionList[_this8.status.currentQuestionIdx].rightOptionIdx == idx)) {
                _context6.next = 6;
                break;
              }

              _this8.onOptionRight(optionNode, idx);

              return _context6.abrupt("return");

            case 6:
              _this8.onOptionWrong(optionNode);

            case 7:
            case "end":
              return _context6.stop();
          }
        }
      }, _callee6);
    }))();
  },
  onOptionWrong: function onOptionWrong(optionNode) {
    var animeNode = cc.find('OptionDragonBoneBaseNode/animeNode', optionNode);

    if (animeNode) {
      animeNode.color = cc.color(150, 150, 150, 255);
    }

    (0, _utils.playEffect)('wrong');
  },
  onOptionRight: function onOptionRight(optionNode, idx) {
    var optionList = this.data.questionList[this.status.currentQuestionIdx].optionList;
    var posNode = cc.find("Canvas/OptionPosListBase/OptionTarget_" + optionList.length + "_" + idx);
    (0, _utils.playEffect)('right');
    cc.tween(optionNode).to(1, {
      x: posNode.x
    }, {
      easing: 'quadIn'
    }).start();
    cc.tween(optionNode).to(1, {
      y: posNode.y,
      scale: 0.3
    }).to(0.5, {
      opacity: 0
    }).start();
    optionList.forEach( /*#__PURE__*/function () {
      var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee7(data, idx) {
        var option, animeNode;
        return regeneratorRuntime.wrap(function _callee7$(_context7) {
          while (1) {
            switch (_context7.prev = _context7.next) {
              case 0:
                option = cc.find("Canvas/OptionPosListBase/optionNode_" + idx);
                option.canClick = false;

                if (!(option != optionNode)) {
                  _context7.next = 7;
                  break;
                }

                _context7.next = 5;
                return (0, _utils.asyncLoadDragonBoneAnime)(cc.find('OptionDragonBoneBaseNode', option), data.optionDragonBone);

              case 5:
                animeNode = _context7.sent;

                if (animeNode) {
                  animeNode.color = cc.color(255, 255, 255, 255);
                  (0, _utils.asyncPlayDragonBoneAnimation)(animeNode, 'normal', -1);
                }

              case 7:
              case "end":
                return _context7.stop();
            }
          }
        }, _callee7);
      }));

      return function (_x3, _x4) {
        return _ref3.apply(this, arguments);
      };
    }());
  },
  resetQuestionBoard: function resetQuestionBoard() {
    var labelBgNode = cc.find('Canvas/QuestionLabelBg');
    labelBgNode.stopAllActions();
    labelBgNode.y = 463;
  },
  questionBoardDown: function questionBoardDown() {
    var _this9 = this;

    return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee8() {
      var questionStr, questionLabel, labelBgNode;
      return regeneratorRuntime.wrap(function _callee8$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              questionStr = _this9.data.questionList[_this9.status.currentQuestionIdx].questionTxt;
              questionLabel = cc.find('Canvas/QuestionLabelBg/QuestionLabelNode');
              questionLabel.getComponent(cc.Label).string = questionStr;
              labelBgNode = cc.find('Canvas/QuestionLabelBg');
              _context8.next = 6;
              return (0, _utils.asyncTweenTo)(labelBgNode, 1, {
                y: 263
              });

            case 6:
              _context8.next = 8;
              return (0, _utils.asyncDelay)(1);

            case 8:
            case "end":
              return _context8.stop();
          }
        }
      }, _callee8);
    }))();
  },
  questionBoardUp: function questionBoardUp() {
    return _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee9() {
      var labelBgNode;
      return regeneratorRuntime.wrap(function _callee9$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              labelBgNode = cc.find('Canvas/QuestionLabelBg');
              _context9.next = 3;
              return (0, _utils.asyncTweenTo)(labelBgNode, 1, {
                y: 463
              });

            case 3:
            case "end":
              return _context9.stop();
          }
        }
      }, _callee9);
    }))();
  }
});

cc._RF.pop();