import React from 'react';
import classNames from 'classnames';
import { connect } from 'react-redux';
import { TimestampTag } from 'weaveworks-ui-components';

import { trackAnalyticsEvent } from '../utils/tracking-utils';
import { pauseTimeAtNow, resumeTime } from '../actions/request-actions';
import { isPausedSelector, timeTravelSupportedSelector } from '../selectors/time-travel';

import moment from 'moment'
const className = isSelected => (
  classNames('time-control-action', { 'time-control-action-selected': isSelected })
);

class TimeControl extends React.Component {
  getTrackingMetadata = (data = {}) => {
    const { currentTopology } = this.props;
    return {
      layout: this.props.topologyViewMode,
      parentTopologyId: currentTopology && currentTopology.get('parentId'),
      topologyId: currentTopology && currentTopology.get('id'),
      ...data,
    };
  }

  handleNowClick = () => {
    trackAnalyticsEvent('scope.time.resume.click', this.getTrackingMetadata());
    this.props.resumeTime();
  }

  handlePauseClick = () => {
    trackAnalyticsEvent('scope.time.pause.click', this.getTrackingMetadata());
    this.props.pauseTimeAtNow();
  }

  render() {
    const { isPaused, pausedAt, topologiesLoaded } = this.props;

    // If Time Travel is supported, show an empty placeholder div instead
    // of this control, since time will be controlled through the timeline.
    // We return <div /> instead of null so that selector controls would
    // be aligned the same way between WC Explore and Scope standalone.
    if (this.props.timeTravelSupported) return <div />;

    return (
      <div className="time-control">
        <div className="time-control-controls">
          <div className="time-control-wrapper">
          {/* title="Show live state of the system"> */}
            <span
              className={className(!isPaused)}
              onClick={this.handleNowClick}
              disabled={!topologiesLoaded}>
              {!isPaused && <i className="fa fa-play" />}
              <span className="label" style={{fontSize:"14px"}}>实时</span>
            </span>
            {/* title="Pause updates (freezes the nodes in their current layout)"> */}
            <span
              className={className(isPaused)}
              onClick={this.handlePauseClick}
              disabled={!topologiesLoaded}
              >
              {isPaused && <i className="fa fa-pause" />}
              {/* <span className="label">{isPaused ? 'Paused' : 'Pause'}</span> */}
              <span className="label" style={{fontSize:"14px"}}>{isPaused ? '已暂停' : '暂停'}</span>
            </span>
          </div>
        </div>
        {isPaused
          && (
          <span className="time-control-info" style={{fontSize:"12px"}}>
            状态暂停于
            {' '}
            {moment().format("YYYY-MM-DD HH:mm:ss")}
            {/* <TimestampTag inheritStyles relative timestamp={pausedAt} /> */}
          </span>
          )
        }
      </div>
    );
  }
}

function mapStateToProps(state) {
  return {
    currentTopology: state.get('currentTopology'),
    isPaused: isPausedSelector(state),
    pausedAt: state.get('pausedAt'),
    timeTravelSupported: timeTravelSupportedSelector(state),
    topologiesLoaded: state.get('topologiesLoaded'),
    topologyViewMode: state.get('topologyViewMode'),
  };
}

export default connect(
  mapStateToProps,
  {
    pauseTimeAtNow,
    resumeTime,
  }
)(TimeControl);
