'use strict';

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

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reactFontawesome = require('react-fontawesome');

var _reactFontawesome2 = _interopRequireDefault(_reactFontawesome);

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

var _reactRemarkable = require('react-remarkable');

var _reactRemarkable2 = _interopRequireDefault(_reactRemarkable);

var _checkbox = require('../components/checkbox');

var _checkbox2 = _interopRequireDefault(_checkbox);

var _pluginManager = require('views/services/plugin-manager');

var _pluginManager2 = _interopRequireDefault(_pluginManager);

var _pluginSettingWrapper = require('./plugin-setting-wrapper');

var _pluginSettingWrapper2 = _interopRequireDefault(_pluginSettingWrapper);

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

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

class InstalledPlugin extends _react.PureComponent {
  constructor(...args) {
    var _temp;

    return _temp = super(...args), this.state = {
      settingOpen: false
    }, this.toggleSettingPop = () => {
      this.setState({ settingOpen: !this.state.settingOpen });
    }, _temp;
  }

  render() {
    const plugin = this.props.plugin;
    const outdatedLabelbsStyle = !plugin.latestVersion.includes('beta') ? 'primary' : 'warning';
    const outdatedLabelFAname = (0, _classnames2.default)({
      'spinner': plugin.isUpdating,
      'cloud-download': !plugin.isUpdating && plugin.isOutdated,
      'check': !plugin.isUpdating && !plugin.isOutdated
    });
    const outdatedLabelText = plugin.isUpdating ? `${__('Updating')}` : plugin.isOutdated ? `Version ${plugin.latestVersion}` : `${__('Latest')}`;
    let enableBtnText, enableBtnFAname;
    switch (_pluginManager2.default.getStatusOfPlugin(plugin)) {
      case _pluginManager2.default.VALID:
        enableBtnText = `${__('Disable')}`;
        enableBtnFAname = 'pause';
        break;
      case _pluginManager2.default.DISABLED:
        enableBtnText = `${__('Enable')}`;
        enableBtnFAname = 'play';
        break;
      case _pluginManager2.default.NEEDUPDATE:
        enableBtnText = `${__('Outdated')}`;
        enableBtnFAname = 'ban';
        break;
      case _pluginManager2.default.BROKEN:
        enableBtnText = `${__('Error')}`;
        enableBtnFAname = 'close';
        break;
      default:
        enableBtnText = '';
        enableBtnFAname = '';
    }
    const removeBtnText = plugin.isUninstalling ? `${__('Removing')}` : `${__('Remove')}`;
    const removeBtnFAname = plugin.isInstalled ? 'trash' : 'trash-o';
    const panelClass = (0, _classnames2.default)('plugin-content', {
      'plugin-content-disabled': _pluginManager2.default.getStatusOfPlugin(plugin) !== _pluginManager2.default.VALID
    });
    const outdatedLabelClass = (0, _classnames2.default)('update-label', {
      'hidden': !plugin.isOutdated
    });
    const settingAvailable = plugin.settingsClass || plugin.switchPluginPath || !plugin.multiWindow && plugin.windowURL;
    const btnGroupClass = (0, _classnames2.default)('plugin-buttongroup', {
      'btn-xs-12': settingAvailable,
      'btn-xs-8': !settingAvailable
    });
    const btnClass = (0, _classnames2.default)('plugin-control-button', {
      'btn-xs-4': settingAvailable,
      'btn-xs-6': !settingAvailable
    });
    return _react2.default.createElement(
      _reactBootstrap.Row,
      { className: 'plugin-wrapper' },
      _react2.default.createElement(
        _reactBootstrap.Col,
        { xs: 12 },
        _react2.default.createElement(
          _reactBootstrap.Panel,
          { className: panelClass },
          _react2.default.createElement(
            _reactBootstrap.Panel.Body,
            null,
            _react2.default.createElement(
              _reactBootstrap.Row,
              null,
              _react2.default.createElement(
                _reactBootstrap.Col,
                { xs: 12, className: 'div-row' },
                _react2.default.createElement(
                  'span',
                  { className: 'plugin-name' },
                  plugin.displayName
                ),
                _react2.default.createElement(
                  'div',
                  { className: 'author-wrapper' },
                  '@',
                  _react2.default.createElement(
                    'a',
                    { className: 'author-link',
                      href: plugin.link },
                    plugin.author
                  )
                ),
                _react2.default.createElement(
                  'div',
                  { className: 'update-wrapper' },
                  _react2.default.createElement(
                    'div',
                    null,
                    _react2.default.createElement(
                      _reactBootstrap.Label,
                      { bsStyle: outdatedLabelbsStyle,
                        className: outdatedLabelClass,
                        onClick: this.props.handleUpdate },
                      _react2.default.createElement(_reactFontawesome2.default, { name: outdatedLabelFAname,
                        pulse: plugin.isUpdating }),
                      outdatedLabelText
                    )
                  ),
                  _react2.default.createElement(
                    'div',
                    null,
                    _react2.default.createElement(
                      'span',
                      null,
                      plugin.linkedPlugin && _react2.default.createElement(_reactFontawesome2.default, { name: 'link' })
                    ),
                    `Ver. ${plugin.version || '1.0.0'}`
                  )
                )
              )
            ),
            _react2.default.createElement(
              _reactBootstrap.Row,
              null,
              _react2.default.createElement(
                _reactBootstrap.Col,
                { className: 'plugin-description', xs: 7 },
                _react2.default.createElement(_reactRemarkable2.default, { source: plugin.description })
              ),
              _react2.default.createElement(
                _reactBootstrap.Col,
                { className: 'plugin-option', xs: 5 },
                _react2.default.createElement(
                  _reactBootstrap.ButtonGroup,
                  { bsSize: 'small', className: btnGroupClass },
                  settingAvailable ? _react2.default.createElement(
                    _reactBootstrap.OverlayTrigger,
                    { placement: 'top', overlay: _react2.default.createElement(
                        _reactBootstrap.Tooltip,
                        { id: `${plugin.id}-set-btn` },
                        __('Settings')
                      ) },
                    _react2.default.createElement(
                      _reactBootstrap.Button,
                      {
                        bsStyle: 'primary', bsSize: 'xs',
                        onClick: this.toggleSettingPop,
                        className: 'plugin-control-button btn-xs-4' },
                      _react2.default.createElement(_reactFontawesome2.default, { name: 'gear' })
                    )
                  ) : null,
                  _react2.default.createElement(
                    _reactBootstrap.OverlayTrigger,
                    { placement: 'top', overlay: _react2.default.createElement(
                        _reactBootstrap.Tooltip,
                        { id: `${plugin.id}-enb-btn` },
                        enableBtnText
                      ) },
                    _react2.default.createElement(
                      _reactBootstrap.Button,
                      { bsStyle: 'info',
                        disabled: _pluginManager2.default.getStatusOfPlugin(plugin) == _pluginManager2.default.NEEDUPDATE,
                        onClick: this.props.handleEnable,
                        className: btnClass },
                      _react2.default.createElement(_reactFontawesome2.default, { name: enableBtnFAname })
                    )
                  ),
                  _react2.default.createElement(
                    _reactBootstrap.OverlayTrigger,
                    { placement: 'top', overlay: _react2.default.createElement(
                        _reactBootstrap.Tooltip,
                        { id: `${plugin.id}-rm-btn` },
                        removeBtnText
                      ) },
                    _react2.default.createElement(
                      _reactBootstrap.Button,
                      { bsStyle: 'danger',
                        onClick: this.props.handleRemove,
                        disabled: !plugin.isInstalled,
                        className: btnClass },
                      _react2.default.createElement(_reactFontawesome2.default, { name: removeBtnFAname })
                    )
                  )
                )
              )
            ),
            _react2.default.createElement(
              _reactBootstrap.Row,
              null,
              settingAvailable ? _react2.default.createElement(
                _reactBootstrap.Collapse,
                { 'in': this.state.settingOpen, className: 'plugin-setting-wrapper' },
                _react2.default.createElement(
                  _reactBootstrap.Col,
                  { xs: 12 },
                  _react2.default.createElement(
                    _reactBootstrap.Well,
                    null,
                    !!plugin.switchPluginPath && _react2.default.createElement(
                      'div',
                      null,
                      _react2.default.createElement(_checkbox2.default, {
                        label: __('Enable auto switch'),
                        configName: `poi.autoswitch.${plugin.id}`,
                        defaultVal: true })
                    ),
                    !plugin.multiWindow && plugin.windowURL && _react2.default.createElement(
                      'div',
                      null,
                      _react2.default.createElement(_checkbox2.default, {
                        label: __('Keep plugin process running in background (re-enable to apply changes)'),
                        configName: `poi.backgroundProcess.${plugin.id}`,
                        defaultVal: !plugin.realClose })
                    ),
                    !!plugin.settingsClass && _react2.default.createElement(
                      'div',
                      null,
                      _react2.default.createElement(_pluginSettingWrapper2.default, { plugin: plugin })
                    )
                  )
                )
              ) : null
            )
          )
        )
      )
    );
  }
}

InstalledPlugin.propTypes = {
  plugin: _propTypes2.default.object,
  handleUpdate: _propTypes2.default.func,
  handleEnable: _propTypes2.default.func,
  handleRemove: _propTypes2.default.func
};
exports.default = InstalledPlugin;
module.exports = exports['default'];