import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/esm/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/esm/getPrototypeOf";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";

/**
 * timeOptions.jsx
 *
 * @Author: apple
 * @Date: 2018/6/14 01:31
 */
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import moment from 'moment';
import Button from '../button';
import slideTo from '../util/slideTo';
import easyToMap from '../util/easyToMap';
/**
 * TimePanel
 * */

var TimePanel =
/*#__PURE__*/
function (_PureComponent) {
  _inherits(TimePanel, _PureComponent);

  function TimePanel() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, TimePanel);

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

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(TimePanel)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_this), "setBasicPosition", function (value) {
      _this.elmScrollTop(_this.hourElement, value ? value.hour() : 0);

      _this.elmScrollTop(_this.minuteElement, value ? value.minute() : 0);

      _this.elmScrollTop(_this.secondElement, value ? value.second() : 0);
    });

    _defineProperty(_assertThisInitialized(_this), "selectTime", function (type, time, disabled) {
      if (!disabled) {
        var _this$props = _this.props,
            onValueChange = _this$props.onValueChange,
            _this$props$value = _this$props.value,
            value = _this$props$value === void 0 ? moment() : _this$props$value;
        var newValue = moment(value)[type](time);

        _this.elmScrollTop(_this["".concat(type, "Element")], time);

        onValueChange && onValueChange(newValue, {
          type: type,
          time: time
        });
      }
    });

    _defineProperty(_assertThisInitialized(_this), "elmScrollTop", function (element, time) {
      if (element) {
        var itemHeight = _this.props.itemHeight;
        var targetPosition = itemHeight * time;
        var scrollTop = targetPosition > 0 ? targetPosition : 0;
        var maxScrollTop = element.scrollHeight - element.clientHeight;

        if (scrollTop >= maxScrollTop) {
          scrollTop = maxScrollTop;
        }

        slideTo({
          beginValue: element.scrollTop,
          endValue: scrollTop,
          time: 300,
          tweenName: 'easeInSine',
          stepCallback: function stepCallback(value) {
            element.scrollTop = value;
          } // endCallback: callback,

        });
        return scrollTop;
      }
    });

    _defineProperty(_assertThisInitialized(_this), "getHourElement", function (el) {
      _this.hourElement = el;
    });

    _defineProperty(_assertThisInitialized(_this), "getMinuteElement", function (el) {
      _this.minuteElement = el;
    });

    _defineProperty(_assertThisInitialized(_this), "getSecondElement", function (el) {
      _this.secondElement = el;
    });

    _defineProperty(_assertThisInitialized(_this), "disabledHoursCondition", function (hour) {
      var _this$props2 = _this.props,
          disabledHours = _this$props2.disabledHours,
          _this$props2$value = _this$props2.value,
          value = _this$props2$value === void 0 ? moment() : _this$props2$value;
      return disabledHours && disabledHours(value, hour);
    });

    _defineProperty(_assertThisInitialized(_this), "disabledMinutesCondition", function (minute) {
      var _this$props3 = _this.props,
          disabledMinutes = _this$props3.disabledMinutes,
          _this$props3$value = _this$props3.value,
          value = _this$props3$value === void 0 ? moment() : _this$props3$value;
      return disabledMinutes && disabledMinutes(value, value.hour(), minute);
    });

    _defineProperty(_assertThisInitialized(_this), "disabledSecondsCondition", function (second) {
      var _this$props4 = _this.props,
          disabledSeconds = _this$props4.disabledSeconds,
          _this$props4$value = _this$props4.value,
          value = _this$props4$value === void 0 ? moment() : _this$props4$value;
      return disabledSeconds && disabledSeconds(value, value.hour(), value.minute(), second);
    });

    return _this;
  }

  _createClass(TimePanel, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var value = this.props.value;
      this.setBasicPosition(value);
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      var _this$props5 = this.props,
          isOpen = _this$props5.isOpen,
          value = _this$props5.value;

      if (prevProps.value !== value || isOpen && !prevProps.isOpen) {
        this.setBasicPosition(value);
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props6 = this.props,
          value = _this$props6.value,
          prefixCls = _this$props6.prefixCls;
      return React.createElement("div", {
        className: "".concat(prefixCls, "-body")
      }, [{
        ref: this.getHourElement,
        checked: value ? value.hour() : null,
        type: 'hour',
        optionsList: easyToMap(24),
        disabledFunc: this.disabledHoursCondition
      }, {
        ref: this.getMinuteElement,
        checked: value ? value.minute() : null,
        type: 'minute',
        optionsList: easyToMap(60),
        disabledFunc: this.disabledMinutesCondition
      }, {
        ref: this.getSecondElement,
        checked: value ? value.second() : null,
        type: 'second',
        optionsList: easyToMap(60),
        disabledFunc: this.disabledSecondsCondition
      }].map(function (renderOption) {
        var ref = renderOption.ref,
            checked = renderOption.checked,
            type = renderOption.type,
            optionsList = renderOption.optionsList,
            disabledFunc = renderOption.disabledFunc;
        return React.createElement("div", {
          className: "".concat(prefixCls, "-cell"),
          ref: ref,
          key: type
        }, optionsList.map(function (item) {
          var classes = classNames("".concat(prefixCls, "-item"), _defineProperty({}, "".concat(prefixCls, "-item-checked"), item === checked));
          var disabled = false;

          if (disabledFunc(item)) {
            disabled = true;
          }

          return React.createElement(Button, {
            variant: "text",
            className: classes,
            key: item,
            disabled: disabled,
            onClick: function onClick(element) {
              return _this2.selectTime(type, item, disabled, element);
            }
          }, item);
        }), React.createElement("div", {
          className: "".concat(prefixCls, "-cell-padding")
        }));
      }));
    }
  }]);

  return TimePanel;
}(PureComponent);

_defineProperty(TimePanel, "propTypes", {
  /**
   * 更新时间回调函数
   * @params time: 所选时间
   * @return
   * */
  onValueChange: PropTypes.func,

  /**
   * 禁用小时
   * @return true为禁用 false为不禁用
   * */
  disabledHours: PropTypes.func,

  /**
   * 禁用分钟
   * @params selectHour:当前渲染小时
   * @return true为禁用 false为不禁用
   * */
  disabledMinutes: PropTypes.func,

  /**
   * 禁用秒
   * @params selectHour:当前所渲染小时, selectMinute:当前所渲染分钟
   * @return true为禁用 false为不禁用
   * */
  disabledSeconds: PropTypes.func,
  itemHeight: PropTypes.number
});

_defineProperty(TimePanel, "defaultProps", {
  prefixCls: 'ne-time-panel',
  itemHeight: 32
});

export default TimePanel;