'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.calculatePosition = exports.isOccupy = exports.calculateHitBit = undefined;

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

var _Bit2 = _interopRequireDefault(_Bit);

var _constant = require('../../constant');

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.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                */


var calculateHitBit = exports.calculateHitBit = function calculateHitBit() {
  var cards = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];

  var hitArr = [];
  cards.forEach(function (_ref) {
    var layout = _ref.layout;
    var x = layout.x,
        y = layout.y,
        width = layout.width,
        height = layout.height;

    for (var i = x; i < x + width; i++) {
      for (var j = y; j < y + height; j++) {
        hitArr[i + j * _constant.COLUMN_COUNT] = true;
      }
    }
  });
  return hitArr;
};

var isOccupy = exports.isOccupy = function isOccupy(x, y, width, height, hitArr) {
  if (x < 0 || y < 0) {
    return true;
  }
  if (x + width > _constant.COLUMN_COUNT || y + height > _constant.LINE_COUNT) return true;
  for (var i = y; i < y + height; i++) {
    for (var j = x; j < x + width; j++) {
      if (hitArr[j + i * _constant.COLUMN_COUNT]) {
        return true;
      }
    }
  }
  return false;
};

var calculatePosition = exports.calculatePosition = function calculatePosition(width, height, cards) {
  var hitArr = calculateHitBit(cards);
  for (var y = 0; y < _constant.LINE_COUNT; y++) {
    for (var x = 0; x < _constant.COLUMN_COUNT; x++) {
      if (!isOccupy(x, y, width, height, hitArr)) {
        return { x: x, y: y };
      }
    }
  }
  throw new Error('没有空白区域了！');
};

var Board = function (_React$Component) {
  _inherits(Board, _React$Component);

  function Board() {
    var _ref2;

    var _temp, _this, _ret;

    _classCallCheck(this, Board);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref2 = Board.__proto__ || Object.getPrototypeOf(Board)).call.apply(_ref2, [this].concat(args))), _this), _this.renderBit = function () {
      var hitArr = calculateHitBit(_this.props.cards);
      var ret = [];
      for (var i = 0; i < _constant.LINE_COUNT * _constant.COLUMN_COUNT; i++) {
        ret.push(_react2.default.createElement(_Bit2.default, { index: i, isHit: !!hitArr[i] }));
      }
      return ret;
    }, _this.calculateLayout = function () {
      var _this$props = _this.props,
          cards = _this$props.cards,
          model = _this$props.model;

      if (model !== 'view') {
        return { height: _constant.LINE_COUNT, width: _constant.COLUMN_COUNT };
      }
      var gHeight = 0,
          gWidth = 0;
      if (cards) {
        cards.forEach(function (card) {
          if (!card) return;
          var layout = card.layout;
          if (!layout) return;
          var x = layout.x,
              y = layout.y,
              width = layout.width,
              height = layout.height;

          if (x == undefined || y === undefined || width === undefined || height === undefined) return;
          gHeight = Math.max(gHeight, y + height);
          gWidth = Math.max(gWidth, x + width);
        });
      }
      return { height: Math.min(gHeight, _constant.LINE_COUNT), width: Math.min(gWidth, _constant.COLUMN_COUNT) };
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  _createClass(Board, [{
    key: 'render',
    value: function render() {
      var _calculateLayout = this.calculateLayout(),
          height = _calculateLayout.height,
          width = _calculateLayout.width;

      return _react2.default.createElement(
        'div',
        { className: 'dash-board-wrapper' },
        _react2.default.createElement(
          'div',
          { className: 'dash-board', style: { width: _constant.BIT * width, height: Math.max(window.innerHeight - 80, _constant.BIT * height + 20) } },
          this.props.model === 'edit' && this.renderBit(),
          this.props.children
        )
      );
    }
  }]);

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

exports.default = Board;