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';
import React, { Fragment } from 'react';
import './index.less';
var timeFun = null;

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

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

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

        _this.addPrefix = function (num) {
            return num < 10 ? '0' + num : num;
        };
        _this.countFun = function () {
            var time = _this.state.time;

            if (time == 0) return;
            var sys_second = time * 1000;
            timeFun = setInterval(function () {
                //防止倒计时出现负数
                if (sys_second > 1000) {
                    sys_second -= 1000;
                    var hour = Math.floor(sys_second / 1000 / 3600 % 3600);
                    var minute = Math.floor(sys_second / 1000 / 60 % 60);
                    var second = Math.floor(sys_second / 1000 % 60);
                    _this.setState({
                        time: time,
                        second: _this.addPrefix(second),
                        minute: _this.addPrefix(minute),
                        hour: _this.addPrefix(hour)
                    });
                } else {
                    clearInterval(timeFun);
                }
            }, 1000);
        };
        var startTime = _this.props.startTime;

        var date = new Date().getTime();
        // time is second
        var time = new Date(startTime).getTime() - date > 0 ? Math.floor((new Date(startTime).getTime() - date) / parseFloat('1000')) : 0;
        var hourInit = (time - time % 3600) / 3600;
        var temp = time - hourInit * 3600;
        var minuteInit = (temp - temp % 60) / 60;
        var secondInit = '' + (temp - minuteInit * 60);
        _this.state = {
            second: time > 0 ? secondInit : '00',
            minute: time > 0 ? minuteInit : '00',
            hour: time > 0 ? hourInit : '00',
            time: time
        };
        return _this;
    }

    _createClass(CountDown, [{
        key: 'componentDidMount',
        value: function componentDidMount() {
            var time = this.state.time;

            if (time > 0) this.countFun();
        }
    }, {
        key: 'componentWillUnmount',
        value: function componentWillUnmount() {
            var time = this.state.time;

            if (time == 0) return;
            clearInterval(timeFun);
        }
    }, {
        key: 'render',
        value: function render() {
            var _state = this.state,
                hour = _state.hour,
                minute = _state.minute,
                second = _state.second;
            var timeStyle = this.props.timeStyle;

            return React.createElement(
                Fragment,
                null,
                React.createElement(
                    'div',
                    { className: 'timer' },
                    React.createElement(
                        'div',
                        { className: 'minutes' },
                        React.createElement(
                            'span',
                            { style: timeStyle },
                            hour
                        )
                    ),
                    React.createElement(
                        'span',
                        { className: 'dot' },
                        ':'
                    ),
                    React.createElement(
                        'div',
                        { className: 'minutes' },
                        React.createElement(
                            'span',
                            { style: timeStyle },
                            minute
                        )
                    ),
                    React.createElement(
                        'span',
                        { className: 'dot' },
                        ':'
                    ),
                    React.createElement(
                        'div',
                        { className: 'seconds' },
                        React.createElement(
                            'span',
                            { style: timeStyle },
                            second
                        )
                    )
                )
            );
        }
    }]);

    return CountDown;
}(React.Component);

;
export default CountDown;