'use strict';

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

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

require('./index.less');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

var timeFun = null;

var CountDown = function (_React$Component) {
    (0, _inherits3['default'])(CountDown, _React$Component);

    function CountDown(props) {
        (0, _classCallCheck3['default'])(this, CountDown);

        var _this = (0, _possibleConstructorReturn3['default'])(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;
    }

    (0, _createClass3['default'])(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 _react2['default'].createElement(
                _react.Fragment,
                null,
                _react2['default'].createElement(
                    'div',
                    { className: 'timer' },
                    _react2['default'].createElement(
                        'div',
                        { className: 'minutes' },
                        _react2['default'].createElement(
                            'span',
                            { style: timeStyle },
                            hour
                        )
                    ),
                    _react2['default'].createElement(
                        'span',
                        { className: 'dot' },
                        ':'
                    ),
                    _react2['default'].createElement(
                        'div',
                        { className: 'minutes' },
                        _react2['default'].createElement(
                            'span',
                            { style: timeStyle },
                            minute
                        )
                    ),
                    _react2['default'].createElement(
                        'span',
                        { className: 'dot' },
                        ':'
                    ),
                    _react2['default'].createElement(
                        'div',
                        { className: 'seconds' },
                        _react2['default'].createElement(
                            'span',
                            { style: timeStyle },
                            second
                        )
                    )
                )
            );
        }
    }]);
    return CountDown;
}(_react2['default'].Component);

;
exports['default'] = CountDown;
module.exports = exports['default'];