'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 _axios = require('axios');

var _axios2 = _interopRequireDefault(_axios);

var _bluebird = require('bluebird');

var _bluebird2 = _interopRequireDefault(_bluebird);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _CodeTester = require('./components/CodeTester/CodeTester');

var _CodeTester2 = _interopRequireDefault(_CodeTester);

var _testCodeData = require('./testCodeData');

require('./App.css');

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; } /* eslint no-unused-vars: 0 */
/* eslint-disable react/no-unused-state */
/* eslint-disable react/jsx-boolean-value */


/**
 * <CodeTester />
 * React text editor component which auto run code against provided Mocha tests.
 * @param {String} code: The initial code in the text editor.
 * @param {Function} onValidCode: Function that is run if the text editor code passes
 * all linting rules.
 * @param {Function} onAnyCode: Function run on any input to the text editor.
 */

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

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

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

    _this.state = { testSpec: '', starterCode: '', loading: true, boilerplate: '', instructions: '' };
    _this.renderTester = _this.renderTester.bind(_this);
    _this.handleUrlInput = _this.handleUrlInput.bind(_this);
    _this.handleInstructionsChange = _this.handleInstructionsChange.bind(_this);
    _this.handleTestSpecChange = _this.handleTestSpecChange.bind(_this);
    _this.handleBoilerplateChange = _this.handleBoilerplateChange.bind(_this);
    _this.handleGithubSubmit = _this.handleGithubSubmit.bind(_this);
    _this.handleReset = _this.handleReset.bind(_this);
    return _this;
  }

  _createClass(App, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      var _process$env = process.env,
          NODE_ENV = _process$env.NODE_ENV,
          INITIAL_CODE_QUIZ = _process$env.INITIAL_CODE_QUIZ;

      if (NODE_ENV === 'development') {
        var fakeSubmitEvent = { preventDefault: function preventDefault() {}, target: { 'code-quiz-url': { value: INITIAL_CODE_QUIZ || 'http://127.0.0.1:8080/code-quiz-software-info/greeter/' } } };
        this.handleUrlInput(fakeSubmitEvent);
      }
    }
  }, {
    key: 'handleUrlInput',
    value: function handleUrlInput(event) {
      var _this2 = this;

      event.preventDefault();
      var url = event.target['code-quiz-url'].value;
      getPublicCodeQuiz(url).then(function (data) {
        _this2.setState({
          testSpec: data.spec.data,
          loading: false,
          boilerplate: data.boilerplate.data,
          instructions: data.instructions.data,
          error: null
        });
      }).catch(function (err) {
        console.error(err);
        _this2.setState({
          testSpec: '',
          loading: false,
          boilerplate: 'Error loading data...',
          error: err
        });
      });
    }
  }, {
    key: 'handleInstructionsChange',
    value: function handleInstructionsChange(instructions) {
      this.setState({ instructions: instructions });
    }
  }, {
    key: 'handleTestSpecChange',
    value: function handleTestSpecChange(testSpec) {
      this.setState({ testSpec: testSpec });
    }
  }, {
    key: 'handleBoilerplateChange',
    value: function handleBoilerplateChange(boilerplate) {
      this.setState({ boilerplate: boilerplate });
    }
  }, {
    key: 'handleGithubSubmit',
    value: function handleGithubSubmit(commitMessage) {}
  }, {
    key: 'handleReset',
    value: function handleReset() {}
  }, {
    key: 'renderTester',
    value: function renderTester() {
      var handleTestResults = function handleTestResults(passCount, failCount, runTime) {
        // console.log(passCount, failCount, runTime);
      };

      var handleLintedCode = function handleLintedCode(lintedCode) {};

      var handleAnyCode = function handleAnyCode(code) {};
      var _state = this.state,
          boilerplate = _state.boilerplate,
          instructions = _state.instructions,
          editMode = _state.editMode;

      var testSpec = '';
      if (this.state.testSpec.length) testSpec = this.state.testSpec;
      return _react2.default.createElement(_CodeTester2.default, {
        handleUrlInput: this.handleUrlInput,
        handleGithubSubmit: this.handleGithubSubmit,
        initialCode: boilerplate,
        instructions: instructions,
        iFrameUrl: '/test-runner.html',
        testHandlerUrl: '/get_results.js',
        onAnyCode: handleAnyCode,
        onLintedCode: handleLintedCode,
        onTestResults: handleTestResults,
        showLints: true,
        testSpec: testSpec,
        handleInstructionsChange: this.handleInstructionsChange,
        handleTestSpecChange: this.handleTestSpecChange,
        handleBoilerplateChange: this.handleBoilerplateChange,
        editMode: false,
        toggleEditMode: this.handleEditModeChange,
        showConsole: false,
        handleReset: this.handleReset,
        headerText: 'Header',
        subHeaderText: 'sub header'
      });
    }
  }, {
    key: 'renderErrorScreen',
    value: function renderErrorScreen(error) {
      return _react2.default.createElement(
        'div',
        { className: 'blackout' },
        _react2.default.createElement(
          'div',
          { className: 'blackout-content' },
          _react2.default.createElement(
            'h2',
            null,
            'There was an error.'
          ),
          _react2.default.createElement(
            'h1',
            null,
            'Grab your teacher.'
          ),
          _react2.default.createElement(
            'code',
            null,
            error.message
          )
        )
      );
    }
  }, {
    key: 'render',
    value: function render() {
      var error = this.state.error;

      return _react2.default.createElement(
        'div',
        { className: 'container-fluid' },
        error ? _react2.default.createElement(
          'div',
          { className: 'row' },
          this.renderErrorScreen(error)
        ) : _react2.default.createElement(
          'div',
          { className: 'row' },
          this.renderTester()
        )
      );
    }
  }]);

  return App;
}(_react.Component);

function getPublicCodeQuiz(codeQuizUrl) {
  var isTestMode = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;

  return _bluebird2.default.join(getPublicResource(codeQuizUrl, 'index.js', isTestMode), getPublicResource(codeQuizUrl, 'README.md', isTestMode), getPublicResource(codeQuizUrl, 'index.spec.js', isTestMode), function (boilerplate, instructions, spec) {
    return { boilerplate: boilerplate, instructions: instructions, spec: spec };
  });

  function getPublicResource(projectPath, filename, isTestMode) {
    var url = projectPath.replace(/\/+$/, '') + '/' + filename;
    return _axios2.default.get(url);
  }
}

exports.default = App;