import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
/* tslint:disable:jsx-no-multiline-js */
import React from 'react';
import ReactDOM from 'react-dom';
import classnames from 'classnames';
import CustomKeyboard from './CustomKeyboard';
import { addClass, removeClass } from '../_util/class';

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

    function NumberInput() {
        _classCallCheck(this, NumberInput);

        var _this = _possibleConstructorReturn(this, (NumberInput.__proto__ || Object.getPrototypeOf(NumberInput)).apply(this, arguments));

        _this.state = {
            focus: false
        };
        _this.addBlurListener = function () {
            document.addEventListener('click', _this.doBlur, false);
        };
        _this.removeBlurListen = function () {
            document.removeEventListener('click', _this.doBlur, false);
        };
        _this.getComponent = function () {
            var _this$props = _this.props,
                keyboardPrefixCls = _this$props.keyboardPrefixCls,
                confirmLabel = _this$props.confirmLabel;

            return React.createElement(CustomKeyboard, { onClick: _this.onKeyboardClick, preixCls: keyboardPrefixCls, confirmLabel: confirmLabel });
        };
        _this.renderCustomKeyboard = function () {
            var container = document.querySelector('#' + _this.props.keyboardPrefixCls + '-container');
            if (!container) {
                container = document.createElement('div');
                container.setAttribute('id', _this.props.keyboardPrefixCls + '-container');
                document.body.appendChild(container);
                window.antmCustomKeyboard = ReactDOM.unstable_renderSubtreeIntoContainer(_this, _this.getComponent(), container);
            }
        };
        _this.doBlur = function (ev) {
            var value = _this.props.value;

            if (ev.target !== _this.inputRef) {
                _this.onInputBlur(value);
            }
        };
        _this.unLinkInput = function () {
            var antmCustomKeyboard = window.antmCustomKeyboard;
            if (antmCustomKeyboard.linkedInput === _this) {
                antmCustomKeyboard.linkedInput = null;
                addClass(antmCustomKeyboard.antmKeyboard, _this.props.keyboardPrefixCls + '-wrapper-hide');
                _this.removeBlurListen();
            }
        };
        _this.onInputBlur = function (value) {
            var focus = _this.state.focus;

            if (focus) {
                _this.setState({
                    focus: false
                });
                _this.props.onBlur(value);
                setTimeout(function () {
                    _this.unLinkInput();
                }, 50);
            }
        };
        _this.onInputFocus = function () {
            var value = _this.props.value;

            _this.props.onFocus(value);
            _this.setState({
                focus: true
            }, function () {
                var antmCustomKeyboard = window.antmCustomKeyboard;
                antmCustomKeyboard.linkedInput = _this;
                removeClass(antmCustomKeyboard.antmKeyboard, _this.props.keyboardPrefixCls + '-wrapper-hide');
                antmCustomKeyboard.confirmDisabled = value === '';
                if (value === '') {
                    addClass(antmCustomKeyboard.confirmKeyboardItem, _this.props.keyboardPrefixCls + '-item-disabled');
                } else {
                    removeClass(antmCustomKeyboard.confirmKeyboardItem, _this.props.keyboardPrefixCls + '-item-disabled');
                }
            });
        };
        _this.onKeyboardClick = function (KeyboardItemValue) {
            var _this$props2 = _this.props,
                value = _this$props2.value,
                onChange = _this$props2.onChange,
                maxLength = _this$props2.maxLength;

            var valueAfterChange = void 0;
            // 删除键
            if (KeyboardItemValue === 'delete') {
                valueAfterChange = value.substring(0, value.length - 1);
                onChange({ target: { value: valueAfterChange } });
                // 确认键
            } else if (KeyboardItemValue === 'confirm') {
                valueAfterChange = value;
                onChange({ target: { value: valueAfterChange } });
                _this.onInputBlur(value);
                // 收起键
            } else if (KeyboardItemValue === 'hide') {
                valueAfterChange = value;
                _this.onInputBlur(valueAfterChange);
            } else {
                if (maxLength !== undefined && +maxLength >= 0 && (value + KeyboardItemValue).length > maxLength) {
                    valueAfterChange = (value + KeyboardItemValue).substr(0, maxLength);
                    onChange({ target: { value: valueAfterChange } });
                } else {
                    valueAfterChange = value + KeyboardItemValue;
                    onChange({ target: { value: valueAfterChange } });
                }
            }
            var antmCustomKeyboard = window.antmCustomKeyboard;
            antmCustomKeyboard.confirmDisabled = valueAfterChange === '';
            if (valueAfterChange === '') {
                addClass(antmCustomKeyboard.confirmKeyboardItem, _this.props.keyboardPrefixCls + '-item-disabled');
            } else {
                removeClass(antmCustomKeyboard.confirmKeyboardItem, _this.props.keyboardPrefixCls + '-item-disabled');
            }
        };
        _this.onFakeInputClick = function () {
            _this.focus();
        };
        _this.focus = function () {
            // this focus may invocked by users page button click, so this click may trigger blurEventListener at the same time
            _this.removeBlurListen();
            var focus = _this.state.focus;

            if (!focus) {
                _this.onInputFocus();
            }
            setTimeout(function () {
                _this.addBlurListener();
            }, 50);
        };
        return _this;
    }

    _createClass(NumberInput, [{
        key: 'componentDidMount',
        value: function componentDidMount() {
            if (!window.antmCustomKeyboard) {
                this.renderCustomKeyboard();
            }
        }
    }, {
        key: 'componentWillUnmount',
        value: function componentWillUnmount() {
            this.unLinkInput();
        }
    }, {
        key: 'render',
        value: function render() {
            var _this2 = this;

            var _props = this.props,
                placeholder = _props.placeholder,
                value = _props.value,
                disabled = _props.disabled,
                editable = _props.editable;
            var focus = this.state.focus;

            var preventKeyboard = disabled || !editable;
            var fakeInputCls = classnames('fake-input', {
                focus: focus,
                'fake-input-disabled': disabled
            });
            return React.createElement(
                'div',
                { className: 'fake-input-container' },
                value === '' && React.createElement(
                    'div',
                    { className: 'fake-input-placeholder' },
                    placeholder
                ),
                React.createElement(
                    'div',
                    { className: fakeInputCls, ref: function ref(el) {
                            return _this2.inputRef = el;
                        }, onClick: preventKeyboard ? function () {} : this.onFakeInputClick },
                    value
                )
            );
        }
    }]);

    return NumberInput;
}(React.Component);

NumberInput.defaultProps = {
    onChange: function onChange() {},
    onFocus: function onFocus() {},
    onBlur: function onBlur() {},
    placeholder: '',
    value: '',
    disabled: false,
    editable: true,
    prefixCls: 'am-input',
    keyboardPrefixCls: 'am-number-keyboard'
};
export default NumberInput;