'use strict';

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

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

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 _constant = require('../constant');

var _common = require('./common');

var _board = require('../components/DashBoard/board');

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; } /**
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                * Created by baoyinghai on 5/26/17.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                */


exports.default = function (Comp) {
  var MoveAble = function (_React$Component) {
    _inherits(MoveAble, _React$Component);

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

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

      _this._rmPX = function (str) {
        if (str && str.length > 2 && str.endsWith('px')) {
          return parseInt(str.substring(0, str.length - 2), 10);
        }
        return 0;
      };

      _this.addBodyClass = function () {
        var body = document.querySelector('body');
        _this.userSelect = body.style.userSelect;
        body.style.userSelect = 'none';
      };

      _this.removeBodyClass = function () {
        var body = document.querySelector('body');
        body.style.userSelect = _this.userSelect;
      };

      _this.refStr = 'dash-board-move-able-' + _this.props.index + '-' + new Date().getTime();
      return _this;
    }

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

        var _props = this.props,
            layout = _props.layout,
            updateLayout = _props.updateLayout,
            index = _props.index,
            calculateHitBitBeside = _props.calculateHitBitBeside;

        var style = (0, _common.calculateStyle)(layout);
        var event = {
          onMouseDown: function onMouseDown(e) {
            e.stopPropagation();
            _this2.addBodyClass();
            var hitArr = calculateHitBitBeside(index);
            var startClientX = e.clientX;
            var startClientY = e.clientY;
            var wrapper = document.querySelector('.' + _this2.refStr + '.move-container');
            var cloneWrapper = (0, _common.createShadow)(style);
            wrapper.parentNode.appendChild(cloneWrapper);
            var left = _this2._rmPX(wrapper.style.left);
            var top = _this2._rmPX(wrapper.style.top);
            wrapper.style.zIndex = 10;
            _this2.offsetX = 0;
            _this2.offsetY = 0;
            _this2.tempOffsetX = 0;
            _this2.tempOffsetY = 0;
            _this2.xFn = Math.floor;
            _this2.yFn = Math.floor;
            var mouseMoveHandler = function mouseMoveHandler(moveEvent) {
              moveEvent.stopPropagation();
              var changeLeft = true;
              var changeTop = true;
              _this2.isMove = true;
              var newLeft = left + (moveEvent.clientX - startClientX);
              var newTop = top + (moveEvent.clientY - startClientY);
              if (newLeft < 0 || newLeft > (_constant.COLUMN_COUNT - layout.width) * _constant.BIT) {
                changeLeft = false;
              }
              if (newTop < 0 || newTop > (_constant.LINE_COUNT - layout.height) * _constant.BIT) {
                changeTop = false;
              }
              if (changeLeft) {
                var oldLeft = _this2._rmPX(wrapper.style.left);
                if (newLeft < oldLeft - 2) {
                  _this2.xFn = Math.floor;
                }
                if (newLeft > oldLeft + 2) {
                  _this2.xFn = Math.ceil;
                }
                wrapper.style.left = newLeft + 'px';
                _this2.tempOffsetX = _this2.xFn((moveEvent.clientX - startClientX) / _constant.BIT);
              }
              if (changeTop) {
                var oldTop = _this2._rmPX(wrapper.style.top);
                if (newTop < oldTop - 2) {
                  _this2.yFn = Math.floor;
                }
                if (newTop > oldTop + 2) {
                  _this2.yFn = Math.ceil;
                }
                wrapper.style.top = newTop + 'px';
                _this2.tempOffsetY = _this2.yFn((moveEvent.clientY - startClientY) / _constant.BIT);
              }
              if (!(0, _board.isOccupy)(layout.x + _this2.tempOffsetX, layout.y + _this2.tempOffsetY, layout.width, layout.height, hitArr)) {
                if (_this2.tempOffsetX !== _this2.offsetX) {
                  _this2.offsetX = _this2.tempOffsetX;
                  cloneWrapper.style.left = _this2.offsetX * _constant.BIT + left + 'px';
                }
                if (_this2.tempOffsetY !== _this2.offsetY) {
                  _this2.offsetY = _this2.tempOffsetY;
                  cloneWrapper.style.top = _this2.offsetY * _constant.BIT + top + 'px';
                }
              }
            };

            var mouseUpHandler = function mouseUpHandler(upEvent) {
              console.log('upper');
              upEvent.stopPropagation();
              _this2.removeBodyClass();
              wrapper.style.zIndex = 0;
              wrapper.parentNode.removeChild(cloneWrapper);
              window.removeEventListener('mousemove', mouseMoveHandler);
              window.removeEventListener('mouseup', mouseUpHandler);
              if (_this2.isMove) {
                updateLayout(index, _extends({}, layout, { x: layout.x + _this2.offsetX, y: layout.y + _this2.offsetY }));
                _this2.isMove = false;
                _this2.offsetX = 0;
                _this2.offsetY = 0;
              }
            };

            window.addEventListener('mousemove', mouseMoveHandler);
            window.addEventListener('mouseup', mouseUpHandler);
          }
        };
        return _react2.default.createElement(
          'div',
          _extends({}, event, { className: this.refStr + " move-container", style: style }),
          _react2.default.createElement(Comp, _extends({ refStr: this.refStr }, this.props, { moveAble: true, moveStyle: style }))
        );
      }
    }]);

    return MoveAble;
  }(_react2.default.Component);

  ;

  return MoveAble;
};