'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.ExecutorPing = undefined;

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _promise = require('babel-runtime/core-js/promise');

var _promise2 = _interopRequireDefault(_promise);

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

require('source-map-support/register');

var _Executor2 = require('../agent/Executor');

var _AgentUtils = require('../agent/AgentUtils');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * Sample .githubTaskManager.json task config
 *
 * see: https://github.com/zotoio/github-task-manager/wiki/Structure-of-.githubTaskManager.json
 *
  {
    "executor": "Ping",
    "context": "diagnostic",
    "options": {
      "count": 3
    }
  }
 */

var ExecutorPing = exports.ExecutorPing = function (_Executor) {
    (0, _inherits3.default)(ExecutorPing, _Executor);

    function ExecutorPing(eventData, log) {
        (0, _classCallCheck3.default)(this, ExecutorPing);

        var _this = (0, _possibleConstructorReturn3.default)(this, (ExecutorPing.__proto__ || (0, _getPrototypeOf2.default)(ExecutorPing)).call(this, eventData, log));

        _this.log = log;
        _this.options = _this.getOptions();
        return _this;
    }

    (0, _createClass3.default)(ExecutorPing, [{
        key: 'executeTask',
        value: function () {
            var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(task) {
                var _this2 = this;

                var log, count, promises, _loop, i;

                return _regenerator2.default.wrap(function _callee$(_context2) {
                    while (1) {
                        switch (_context2.prev = _context2.next) {
                            case 0:
                                log = this.log;
                                count = parseInt(task.options.count);
                                promises = [];
                                _loop = /*#__PURE__*/_regenerator2.default.mark(function _loop(i) {
                                    var status;
                                    return _regenerator2.default.wrap(function _loop$(_context) {
                                        while (1) {
                                            switch (_context.prev = _context.next) {
                                                case 0:
                                                    status = _AgentUtils.AgentUtils.createPullRequestStatus(_this2.eventData, 'pending', task.executor + ': ' + task.context, 'got ping ' + i);


                                                    promises.push(_AgentUtils.AgentUtils.postResultsAndTrigger(status, 'Ping', log).then(function () {
                                                        log.info('sent ping ' + i);
                                                    }));

                                                    _context.next = 4;
                                                    return _AgentUtils.AgentUtils.timeout(3000);

                                                case 4:
                                                case 'end':
                                                    return _context.stop();
                                            }
                                        }
                                    }, _loop, _this2);
                                });
                                i = 1;

                            case 5:
                                if (!(i <= count)) {
                                    _context2.next = 10;
                                    break;
                                }

                                return _context2.delegateYield(_loop(i), 't0', 7);

                            case 7:
                                i++;
                                _context2.next = 5;
                                break;

                            case 10:
                                return _context2.abrupt('return', _promise2.default.all(promises).then(function () {
                                    task.results = {
                                        passed: true,
                                        url: 'https://ping.io/ping/' + task.options.count
                                    };
                                    return _promise2.default.resolve(task);
                                }));

                            case 11:
                            case 'end':
                                return _context2.stop();
                        }
                    }
                }, _callee, this);
            }));

            function executeTask(_x) {
                return _ref.apply(this, arguments);
            }

            return executeTask;
        }()
    }]);
    return ExecutorPing;
}(_Executor2.Executor);

_Executor2.Executor.register('Ping', ExecutorPing);