'use strict';

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _electron = require('electron');

var _reactBootstrap = require('react-bootstrap');

var _lodash = require('lodash');

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

const { i18n } = window;
const __ = i18n.setting.__.bind(i18n.setting);

function _ref(metric) {
  return metric.cpu.percentCPUUsage;
}

function _ref2(str) {
  return _react2.default.createElement(
    'span',
    { key: str, title: str },
    str
  );
}

class AppMetrics extends _react.PureComponent {
  constructor(props) {
    super(props);

    this.collect = () => {
      const metrics = this.getAppMetrics();

      const total = {};

      const pidmap = {};['workingSetSize', 'peakWorkingSetSize'].map(prop => total[prop] = (0, _lodash.round)((0, _lodash.sumBy)(metrics, metric => metric.memory[prop]) / 1000, 2));

      total.percentCPUUsage = (0, _lodash.round)((0, _lodash.sumBy)(metrics, _ref), 2);

      this.getAllWindows().map(win => {
        const pid = win.webContents.getOSProcessId();
        const title = win.getTitle();
        pidmap[pid] = title;
      });
      this.setState({
        metrics: (0, _lodash.sortBy)(JSON.parse(JSON.stringify(metrics)), 'pid'),
        total,
        pidmap
      });
    };

    this.handleClick = () => {
      const { active } = this.state;
      if (active) {
        clearInterval(this.cycle);
      } else {
        this.collect();
        this.cycle = setInterval(this.collect.bind(this), 5 * 1000);
      }

      this.setState({
        active: !active
      });
    };

    this.getAppMetrics = _electron.remote.require('electron').app.getAppMetrics;

    this.getAllWindows = _electron.remote.require('electron').BrowserWindow.getAllWindows;

    this.state = {
      metrics: [],
      total: {},
      pidmap: {},
      active: false
    };
  }

  componentWillUnmount() {
    if (this.cycle) {
      clearInterval(this.cycle);
    }
  }

  render() {
    const { metrics, active, total, pidmap } = this.state;
    return _react2.default.createElement(
      'div',
      null,
      _react2.default.createElement(
        'div',
        null,
        _react2.default.createElement(
          _reactBootstrap.Button,
          { onClick: this.handleClick, bsStyle: active ? 'success' : 'default' },
          active ? _react2.default.createElement(
            'span',
            null,
            __('Monitor on')
          ) : _react2.default.createElement(
            'span',
            null,
            __('Monitor off')
          )
        )
      ),
      active && _react2.default.createElement(
        'div',
        { className: 'metric-table' },
        _react2.default.createElement(
          'div',
          { className: 'metric-row metric-haeder' },
          _react2.default.createElement(
            'span',
            null,
            'PID'
          ),
          ['type', 'working/MB', 'peak/MB', 'private/MB', 'shared/MB', 'CPU/%', 'wakeup'].map(_ref2)
        ),
        metrics.map(metric => _react2.default.createElement(
          'div',
          { className: 'metric-row', key: metric.pid },
          _react2.default.createElement(
            'span',
            null,
            metric.pid
          ),
          _react2.default.createElement(
            'span',
            { title: pidmap[metric.pid] || metric.type },
            pidmap[metric.pid] || metric.type
          ),
          ['workingSetSize', 'peakWorkingSetSize', 'privateBytes', 'sharedBytes'].map(prop => _react2.default.createElement(
            'span',
            { key: prop },
            (0, _lodash.round)((metric.memory || [])[prop] / 1000, 2)
          )),
          ['percentCPUUsage', 'idleWakeupsPerSecond'].map(prop => _react2.default.createElement(
            'span',
            { key: prop },
            (0, _lodash.round)((metric.cpu || [])[prop], 1)
          ))
        )),
        _react2.default.createElement(
          'div',
          { className: 'metric-row metric-total' },
          _react2.default.createElement(
            'span',
            null,
            __('TOTAL')
          ),
          _react2.default.createElement('span', null),
          _react2.default.createElement(
            'span',
            null,
            total.workingSetSize
          ),
          _react2.default.createElement(
            'span',
            null,
            total.peakWorkingSetSize
          ),
          _react2.default.createElement('span', null),
          _react2.default.createElement('span', null),
          _react2.default.createElement(
            'span',
            null,
            total.percentCPUUsage
          ),
          _react2.default.createElement('span', null)
        )
      )
    );
  }
}

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