'use strict';

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

var _class, _temp2;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactRedux = require('react-redux');

var _lodash = require('lodash');

var _electron = require('electron');

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

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

const { session } = _electron.remote.require('electron');

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

function _ref() {
  toggleModal(__('Delete cookies'), __('Success!'));
}

function _ref2(e) {
  _electron.remote.getCurrentWebContents().session.clearStorageData({ storages: ['cookies'] }, _ref);
}

function _ref3() {
  toggleModal(__('Delete cache'), __('Success!'));
}

function _ref4(e) {
  _electron.remote.getCurrentWebContents().session.clearCache(_ref3);
}

function _ref5(e) {
  config.set('poi.cacheSize', parseInt(e.target.value));
}

const ClearDataConfig = (0, _reactRedux.connect)(state => ({
  cacheSize: (0, _lodash.get)(state.config, 'poi.cacheSize', 320)
}))((_temp2 = _class = class ClearDataConfig extends _react.Component {
  constructor(...args) {
    var _temp;

    return _temp = super(...args), this.state = {
      cacheSize: 0
    }, this.handleClearCookie = _ref2, this.handleClearCache = _ref4, this.handleValueChange = _ref5, this.handleUpdateCacheSize = () => {
      session.defaultSession.getCacheSize(cacheSize => this.setState({ cacheSize }));
    }, this.componentDidMount = () => {
      this.handleUpdateCacheSize();
      this.cycle = setInterval(this.handleUpdateCacheSize, 6000000);
    }, this.componentWillUnmount = () => {
      if (this.cycle) {
        clearInterval(this.cycle);
      }
    }, _temp;
  }

  render() {
    return _react2.default.createElement(
      _reactBootstrap.Grid,
      null,
      _react2.default.createElement(
        _reactBootstrap.Col,
        { xs: 6 },
        _react2.default.createElement(
          _reactBootstrap.FormGroup,
          null,
          _react2.default.createElement(
            _reactBootstrap.ControlLabel,
            null,
            __('Current cache size')
          ),
          _react2.default.createElement(
            _reactBootstrap.InputGroup,
            null,
            _react2.default.createElement(
              _reactBootstrap.InputGroup.Button,
              null,
              _react2.default.createElement(
                _reactBootstrap.Button,
                { onClick: this.handleUpdateCacheSize },
                __('Update')
              )
            ),
            _react2.default.createElement(_reactBootstrap.FormControl, { type: 'number',
              disabled: true,
              value: Math.round(this.state.cacheSize / 1048576),
              className: '' }),
            _react2.default.createElement(
              _reactBootstrap.InputGroup.Addon,
              null,
              'MB'
            )
          )
        )
      ),
      _react2.default.createElement(
        _reactBootstrap.Col,
        { xs: 6 },
        _react2.default.createElement(
          _reactBootstrap.FormGroup,
          null,
          _react2.default.createElement(
            _reactBootstrap.ControlLabel,
            null,
            __('Maximum cache size')
          ),
          _react2.default.createElement(
            _reactBootstrap.InputGroup,
            null,
            _react2.default.createElement(_reactBootstrap.FormControl, { type: 'number',
              onChange: this.handleValueChange,
              value: this.props.cacheSize,
              className: '' }),
            _react2.default.createElement(
              _reactBootstrap.InputGroup.Addon,
              null,
              'MB'
            )
          )
        )
      ),
      _react2.default.createElement(
        _reactBootstrap.Col,
        { xs: 6 },
        _react2.default.createElement(
          _reactBootstrap.Button,
          { bsStyle: 'danger', onClick: this.handleClearCookie, style: { width: '100%' } },
          __('Delete cookies')
        )
      ),
      _react2.default.createElement(
        _reactBootstrap.Col,
        { xs: 6 },
        _react2.default.createElement(
          _reactBootstrap.Button,
          { bsStyle: 'danger', onClick: this.handleClearCache, style: { width: '100%' } },
          __('Delete cache')
        )
      ),
      _react2.default.createElement(
        _reactBootstrap.Col,
        { xs: 12 },
        _react2.default.createElement(
          _reactBootstrap.Alert,
          { bsStyle: 'warning', style: { marginTop: '10px' } },
          __('If connection error occurs frequently, delete both of them.')
        )
      )
    );
  }
}, _class.propTypes = {
  cacheSize: _propTypes2.default.number
}, _temp2));

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