'use strict';

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

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactTabs = require('react-tabs');

require('react-tabs/style/react-tabs.css');

var _TestRunner = require('../TestRunner/TestRunner');

var _TestRunner2 = _interopRequireDefault(_TestRunner);

var _Markdown = require('../Markdown/Markdown');

var _Markdown2 = _interopRequireDefault(_Markdown);

var _LoadingIcon = require('../LoadingIcon/LoadingIcon');

var _LoadingIcon2 = _interopRequireDefault(_LoadingIcon);

var _reactBootstrapToggle = require('react-bootstrap-toggle');

var _reactBootstrapToggle2 = _interopRequireDefault(_reactBootstrapToggle);

var _helpers = require('../../helpers');

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var InstructionsPanel = function (_Component) {
  _inherits(InstructionsPanel, _Component);

  function InstructionsPanel(props) {
    _classCallCheck(this, InstructionsPanel);

    var _this = _possibleConstructorReturn(this, (InstructionsPanel.__proto__ || Object.getPrototypeOf(InstructionsPanel)).call(this, props));

    _this.state = {
      autoRun: props.autoRun,
      commitMessage: ''
    };
    _this.toggleAutoRun = _this.toggleAutoRun.bind(_this);
    _this.handleCommitButton = _this.handleCommitButton.bind(_this);
    _this.onCommitMessage = _this.onCommitMessage.bind(_this);
    return _this;
  }

  _createClass(InstructionsPanel, [{
    key: 'toggleAutoRun',
    value: function toggleAutoRun() {
      this.setState(function (_ref) {
        var autoRun = _ref.autoRun;
        return { autoRun: !autoRun };
      });
    }
  }, {
    key: 'onCommitMessage',
    value: function onCommitMessage(e) {
      var commitMessage = e.target.value;
      this.setState({ commitMessage: commitMessage });
    }
  }, {
    key: 'handleCommitButton',
    value: function handleCommitButton(e) {
      e.preventDefault();
      var handleGithubSubmit = this.props.handleGithubSubmit;
      var commitMessage = this.state.commitMessage;

      handleGithubSubmit(commitMessage);
      this.setState({ commitMessage: '' });
    }
  }, {
    key: 'render',
    value: function render() {
      var _props = this.props,
          instructions = _props.instructions,
          markdownOptions = _props.markdownOptions,
          testSpec = _props.testSpec,
          lintedCode = _props.lintedCode,
          handleConsoleLog = _props.handleConsoleLog,
          iFrameUrl = _props.iFrameUrl,
          testHandlerUrl = _props.testHandlerUrl,
          onError = _props.onError,
          infiniteLoopTimeout = _props.infiniteLoopTimeout,
          submitEnabled = _props.submitEnabled,
          onTestRunnerSubmitClick = _props.onTestRunnerSubmitClick,
          editMode = _props.editMode,
          handleReset = _props.handleReset,
          onTestRunnerRef = _props.onTestRunnerRef,
          tabIndex = _props.tabIndex,
          onTabSelect = _props.onTabSelect,
          handleUrlInput = _props.handleUrlInput,
          onTestResults = _props.onTestResults,
          committing = _props.committing;
      var _state = this.state,
          autoRun = _state.autoRun,
          commitMessage = _state.commitMessage;

      return _react2.default.createElement(
        'div',
        { className: 'left-control' },
        _react2.default.createElement(
          _reactTabs.Tabs,
          { selectedIndex: tabIndex, onSelect: onTabSelect, className: 'instructions-panel' },
          _react2.default.createElement(
            _reactTabs.TabList,
            { className: 'selection-panels' },
            _react2.default.createElement(
              _reactTabs.Tab,
              { className: 'selection-panel' },
              'Instructions'
            ),
            _react2.default.createElement(
              _reactTabs.Tab,
              { className: 'selection-panel' },
              'Tests'
            ),
            (0, _helpers.renderIf)(editMode)(_react2.default.createElement(
              _reactTabs.Tab,
              { className: 'selection-panel' },
              'Edit'
            ))
          ),
          _react2.default.createElement(
            _reactTabs.TabPanel,
            { id: 'code-tester-instructions', className: 'code-tester-instructions' },
            (0, _helpers.renderIf)(editMode)(_react2.default.createElement(
              'div',
              null,
              _react2.default.createElement(
                'h4',
                null,
                'Enter CodeQuiz Url'
              ),
              _react2.default.createElement(
                'form',
                { className: 'flex form-group', onSubmit: handleUrlInput },
                _react2.default.createElement('input', { className: 'form-control', name: 'code-quiz-url', size: '50' }),
                _react2.default.createElement(
                  'button',
                  { className: 'btn btn-info pull-right' },
                  'Preview CodeQuiz'
                )
              )
            )),
            _react2.default.createElement(_Markdown2.default, { markdown: instructions, options: markdownOptions })
          ),
          _react2.default.createElement(
            _reactTabs.TabPanel,
            { id: 'code-tester-test-runner', className: 'code-tester-test-runner' },
            _react2.default.createElement(_TestRunner2.default, {
              testSpec: testSpec,
              codeToTest: lintedCode,
              onTestResults: onTestResults,
              onConsoleLog: handleConsoleLog,
              autoRun: autoRun,
              toggleAutoRun: this.toggleAutoRun,
              iFrameUrl: iFrameUrl,
              testHandlerUrl: testHandlerUrl,
              onError: onError,
              infiniteLoopTimeout: infiniteLoopTimeout,
              submitEnabled: submitEnabled,
              onSubmitClick: onTestRunnerSubmitClick,
              onTestRunnerRef: onTestRunnerRef,
              onRunClick: true
            })
          ),
          (0, _helpers.renderIf)(editMode)(_react2.default.createElement(
            _reactTabs.TabPanel,
            { id: 'code-tester-edit-panel', className: 'code-tester-edit-panel' },
            _react2.default.createElement(
              'h4',
              null,
              'Enter Commit Message'
            ),
            _react2.default.createElement(
              'form',
              { className: 'flex form-group', onSubmit: this.handleCommitButton },
              _react2.default.createElement('input', { className: 'form-control', name: 'code-quiz-url', size: '50', value: commitMessage, onChange: this.onCommitMessage }),
              _react2.default.createElement(
                'button',
                { className: 'btn btn-success pull-right' },
                committing ? _react2.default.createElement(_LoadingIcon2.default, null) : _react2.default.createElement(
                  'span',
                  null,
                  'Commit ',
                  _react2.default.createElement('i', { className: 'fa fa-github fa-lg', 'aria-hidden': 'true' })
                )
              )
            )
          ))
        ),
        _react2.default.createElement(
          'div',
          { className: 'settings-panel' },
          _react2.default.createElement(
            'div',
            { className: 'settings-panel-group' },
            _react2.default.createElement(
              'span',
              { style: { color: 'white', paddingRight: '15px' } },
              'Auto-Run'
            ),
            _react2.default.createElement(_reactBootstrapToggle2.default, {
              onClick: this.toggleAutoRun,
              className: 'left-panel-switch',
              on: 'ON',
              off: 'OFF',
              offstyle: 'danger',
              active: this.state.autoRun
            })
          ),
          _react2.default.createElement(
            'button',
            { className: 'btn btn-sm btn-warning', onClick: handleReset },
            'Reset'
          )
        )
      );
    }
  }]);

  return InstructionsPanel;
}(_react.Component);

InstructionsPanel.propTypes = {
  instructions: _propTypes2.default.string,
  markdownOptions: _propTypes2.default.object,
  autoRun: _propTypes2.default.bool.isRequired,
  testSpec: _propTypes2.default.string.isRequired,
  lintedCode: _propTypes2.default.string,
  onTestResults: _propTypes2.default.func,
  iFrameUrl: _propTypes2.default.string.isRequired,
  testHandlerUrl: _propTypes2.default.string.isRequired,
  onError: _propTypes2.default.func,
  infiniteLoopTimeout: _propTypes2.default.number,
  submitEnabled: _propTypes2.default.bool,
  onTestRunnerSubmitClick: _propTypes2.default.func,
  handleConsoleLog: _propTypes2.default.func,
  editMode: _propTypes2.default.bool,
  handleReset: _propTypes2.default.func,
  onTestRunnerRef: _propTypes2.default.func,
  handleUrlInput: _propTypes2.default.func,
  handleGithubSubmit: _propTypes2.default.func,
  onTabSelect: _propTypes2.default.func,
  tabIndex: _propTypes2.default.number,
  committing: _propTypes2.default.bool
};

exports.default = InstructionsPanel;