'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 _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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 Option = function (_Component) {
  _inherits(Option, _Component);

  function Option(props, context) {
    _classCallCheck(this, Option);

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

    _this.state = {
      style: {},
      animateInterval: null,
      currOpacity: 1
    };
    _this._optionClick = _this._optionClick.bind(_this);
    return _this;
  }

  _createClass(Option, [{
    key: '_optionClick',
    value: function _optionClick() {
      var _this2 = this;

      var animateInterval = this.state.animateInterval;

      if (!animateInterval) {
        animateInterval = setInterval(function () {
          var _state = _this2.state;
          var animateInterval = _state.animateInterval;
          var currOpacity = _state.currOpacity;
          var style = _state.style;

          var thisOpacity = currOpacity;
          if (thisOpacity > 0) {
            thisOpacity = thisOpacity - 0.02;
            style = { background: 'linear-gradient(to right, rgba(16, 95, 166,0), rgba(16, 95, 166, ' + (thisOpacity - 0.2) + '), rgba(16, 95, 166, ' + thisOpacity + '), rgba(16, 95, 166, ' + (thisOpacity - 0.2) + '), rgba(16, 95, 166,0))' };
          } else {
            clearInterval(animateInterval);
            animateInterval = null;
            style = {};
          }
          _this2.setState({
            style: style,
            animateInterval: animateInterval,
            currOpacity: thisOpacity
          });
        }, 10);
      }
      this.setState({
        animateInterval: animateInterval,
        currOpacity: 1
      });
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      clearInterval(this.state.animateInterval);
    }
  }, {
    key: 'render',
    value: function render() {
      var child = _lodash2.default.get(this.props, ['children']);
      var designatedAnsId = _lodash2.default.get(child, ['props', 'data-designatedAnsId'], null);
      var ans = _lodash2.default.get(child, ['props', 'data-ans'], null);
      var optionId = _lodash2.default.get(child, ['props', 'data-qId'], null);
      // const className = (designatedAnsId === optionId)? 'option option--right' : ((ans === optionId) ? 'option option--wrong' : 'option option--left')
      var className = designatedAnsId === optionId ? 'option' : ans === optionId ? 'option' : 'option';
      return _react2.default.createElement(
        'div',
        { className: className, onClick: designatedAnsId === optionId || ans === optionId ? null : this._optionClick, style: this.state.style },
        this.props.children
      );
    }
  }]);

  return Option;
}(_react.Component);

exports.default = Option;