'use strict';

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

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

var _reactAddons = require('react/addons');

var _reactAddons2 = _interopRequireDefault(_reactAddons);

var _fluxEditorActions = require('../flux/EditorActions');

var _fluxEditorActions2 = _interopRequireDefault(_fluxEditorActions);

var _fluxEditorStore = require('../flux/EditorStore');

var _fluxEditorStore2 = _interopRequireDefault(_fluxEditorStore);

var _coreRichText = require('../core/RichText');

var _coreEditorCommon = require('../core/EditorCommon');

var _coreUtils = require('../core/utils');

var T = _reactAddons2['default'].PropTypes;

exports['default'] = _reactAddons2['default'].createClass({
  displayName: 'DebugEditor',

  propTypes: {
    editorState: T.object,
    replica: T.object,
    searchLinesWithSelection: T.func
  },

  componentWillReceiveProps: function componentWillReceiveProps(nextProps) {
    this.edState = nextProps.editorState;
    this.replica = nextProps.replica;
  },

  _dumpState: function _dumpState() {
    console.debug('Current state contents (Use React Devtools 0.14+ for real-time state view/edit):');
    console.dir(this.edState);
    _fluxEditorActions2['default'].focusInput();
  },

  _dumpReplica: function _dumpReplica() {
    var text = this.replica.getTextRange(_coreRichText.BASE_CHAR);
    console.debug('Current replica text: [' + text.map(function (c) {
      return c.char;
    }).join('') + ']');
    console.debug('Current replica contents:');
    console.dir(text);
    _fluxEditorActions2['default'].focusInput();
  },

  _dumpPosition: function _dumpPosition() {
    if (this.edState.position) {
      console.debug('Current position:', this.edState.position, 'positionEolStart:', this.edState.positionEolStart);
    } else {
      console.debug('No active position');
    }
    _fluxEditorActions2['default'].focusInput();
  },

  _dumpCurrentLine: function _dumpCurrentLine() {
    var _this = this;

    (0, _coreUtils.logInGroup)('Line debug', function () {
      if (_this.edState.lines) {
        (function () {
          var printLine = function printLine(l) {
            return console.debug(l.toString());
          };

          var currentLine = (0, _coreEditorCommon.lineContainingChar)(_this.edState.lines, _this.edState.position, _this.edState.positionEolStart);
          if (!currentLine) {
            console.log(null);
          } else {
            if (currentLine.index > 0) {
              (0, _coreUtils.logInGroup)('Before', function () {
                printLine(_this.edState.lines[currentLine.index - 1]);
              });
            }
            (0, _coreUtils.logInGroup)('Current', function () {
              console.debug('index', currentLine.index, 'endOfLine', currentLine.endOfLine);
              printLine(currentLine.line);
            });
            if (currentLine.index < _this.edState.lines.length - 1) {
              (0, _coreUtils.logInGroup)('After', function () {
                printLine(_this.edState.lines[currentLine.index + 1]);
              });
            }
          }
        })();
      } else {
        console.debug('No lines');
      }
    });
    _fluxEditorActions2['default'].focusInput();
  },

  _dumpLines: function _dumpLines() {
    if (this.edState.lines) {
      console.debug('Lines as Objects:', this.edState.lines);
      this._dumpLinesFormatted('Lines', this.edState.lines);
    } else {
      console.debug('No lines');
    }
    _fluxEditorActions2['default'].focusInput();
  },

  _dumpSelection: function _dumpSelection() {
    if (this.edState.selectionActive) {
      console.debug('Current selection contents (rich chunks): [' + JSON.stringify(_fluxEditorStore2['default'].getSelectionRich()) + ']');
      console.debug('Current selection contents (plain): [' + _fluxEditorStore2['default'].getSelectionText() + ']');
      console.debug('Current selection contents (html): [' + _fluxEditorStore2['default'].getSelectionHtml() + ']');
      console.debug('Left=', this.edState.selectionLeftChar);
      console.debug('Right=', this.edState.selectionRightChar);
      console.debug('Anchor=', this.edState.selectionAnchorChar);
      console.debug('Chars=', _fluxEditorStore2['default'].getSelection());
    } else {
      console.debug('No active selection');
    }
    _fluxEditorActions2['default'].focusInput();
  },

  _dumpLinesWithSelection: function _dumpLinesWithSelection() {
    var linesWithSelection = this.props.searchLinesWithSelection();
    if (linesWithSelection) {
      var lines = this.edState.lines.slice(linesWithSelection.left, linesWithSelection.right + 1);
      console.debug('Lines with selection as Objects:', lines);
      this._dumpLinesFormatted('Lines with selection', lines);
    } else {
      console.debug('No selected lines');
    }
    _fluxEditorActions2['default'].focusInput();
  },

  _dumpLinesFormatted: function _dumpLinesFormatted(logText, lines) {
    (0, _coreUtils.logInGroup)(logText, function () {
      var _loop = function (i) {
        (0, _coreUtils.logInGroup)('Index ' + i, function () {
          // eslint-disable-line no-loop-func
          console.debug(lines[i].toString());
        });
      };

      for (var i = 0; i < lines.length; i++) {
        _loop(i);
      }
    });
  },

  _forceFlow: function _forceFlow() {
    _fluxEditorActions2['default'].replicaUpdated();
    _fluxEditorActions2['default'].focusInput();
  },

  _forceRender: function _forceRender() {
    this.forceUpdate(function () {
      return console.debug('Render done.');
    });
    _fluxEditorActions2['default'].focusInput();
  },

  _togglePositionEolStart: function _togglePositionEolStart() {
    // state should only be set from the store, but for debugging this is fine
    this.setState(function (previousState) {
      var previous = previousState.positionEolStart;
      console.debug('Toggling positionEolStart from ' + previous + ' to ' + !previous);
      return { positionEolStart: !previous };
    });
    _fluxEditorActions2['default'].focusInput();
  },

  render: function render() {
    return _reactAddons2['default'].createElement(
      'div',
      { style: { position: 'relative', zIndex: 100, paddingTop: 30 } },
      _reactAddons2['default'].createElement(
        'span',
        null,
        'Dump: '
      ),
      _reactAddons2['default'].createElement(
        'button',
        { onClick: this._dumpState },
        'State'
      ),
      ' ',
      _reactAddons2['default'].createElement(
        'button',
        { onClick: this._dumpReplica },
        'Replica'
      ),
      ' ',
      _reactAddons2['default'].createElement(
        'button',
        { onClick: this._dumpPosition },
        'Position'
      ),
      ' ',
      _reactAddons2['default'].createElement(
        'button',
        { onClick: this._dumpCurrentLine },
        'Line'
      ),
      ' ',
      _reactAddons2['default'].createElement(
        'button',
        { onClick: this._dumpLines },
        'All Lines'
      ),
      ' ',
      _reactAddons2['default'].createElement(
        'button',
        { onClick: this._dumpSelection },
        'Selection'
      ),
      ' ',
      _reactAddons2['default'].createElement(
        'button',
        { onClick: this._dumpLinesWithSelection },
        'Lines with Selection'
      ),
      _reactAddons2['default'].createElement('br', null),
      _reactAddons2['default'].createElement(
        'span',
        null,
        'Force: '
      ),
      _reactAddons2['default'].createElement(
        'button',
        { onClick: this._forceRender },
        'Render'
      ),
      ' ',
      _reactAddons2['default'].createElement(
        'button',
        { onClick: this._forceFlow },
        'Flow'
      ),
      _reactAddons2['default'].createElement('br', null),
      _reactAddons2['default'].createElement(
        'span',
        null,
        'Action: '
      ),
      _reactAddons2['default'].createElement(
        'button',
        { onClick: this._togglePositionEolStart },
        'Toggle Position EOL Start'
      ),
      _reactAddons2['default'].createElement('br', null)
    );
  }

});
module.exports = exports['default'];