'use strict';

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

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

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

var _reactFontawesome2 = _interopRequireDefault(_reactFontawesome);

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _divider = require('./components/divider');

var _divider2 = _interopRequireDefault(_divider);

var _lodash = require('lodash');

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

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

const basic = {
  use: 'none',
  pacAddr: '',
  http: {
    host: '127.0.0.1',
    port: 8099,
    requirePassword: false,
    username: '',
    password: ''
  },
  socks5: {
    host: "127.0.0.1",
    port: 1080
  },
  retries: 0,
  port: 0,
  allowLAN: false,
  showAdvanced: false
};

function _ref(state, props) {
  const ret = (0, _lodash.get)(state, 'config.proxy') || {};
  for (const key of Object.keys(basic)) {
    if (ret[key] === undefined) {
      ret[key] = basic[key];
    }
  }
  return ret;
}

const NetworkConfig = (0, _reactRedux.connect)(() => _ref)(class netWorkConfig extends _react.Component {
  constructor(props) {
    super(props);

    this.handleChangeUse = e => {
      const use = e.target.value;
      this.setState({
        use
      });
    };

    this.handleSaveConfig = e => {
      const proxy = Object.clone(this.state);
      let retries = parseInt(this.state.retries);
      if (isNaN(retries) || retries < 0) {
        retries = 0;
        proxy.retries = 0;
      }
      let port = parseInt(this.state.port);
      if (isNaN(port) || port < 1024 || port > 65535) {
        port = 0;
        proxy.port = 0;
      }
      delete proxy.showAdvanced;
      config.set('proxy', proxy);
      this.setState({
        retries,
        port
      });
      toggleModal(__('Proxy setting'), __('Success! It will be available after a restart.'));
    };

    this.handleHttpHostChange = e => {
      const http = Object.clone(this.state.http);
      http.host = e.target.value;
      this.setState({
        http
      });
    };

    this.handleHttpPortChange = e => {
      const http = Object.clone(this.state.http);
      http.port = parseInt(e.target.value) || 0;
      this.setState({
        http
      });
    };

    this.handleSetHttpRequirePassword = e => {
      const http = Object.clone(this.state.http);
      http.requirePassword = !http.requirePassword;
      this.setState({
        http
      });
    };

    this.handleHttpUsernameChange = e => {
      const http = Object.clone(this.state.http);
      http.username = e.target.value;
      this.setState({
        http
      });
    };

    this.handleHttpPasswordChange = e => {
      const http = Object.clone(this.state.http);
      http.password = e.target.value;
      this.setState({
        http
      });
    };

    this.handleSocksHostChange = e => {
      const socks5 = Object.clone(this.state.socks5);
      socks5.host = e.target.value;
      this.setState({
        socks5
      });
    };

    this.handleSocksPortChange = e => {
      const socks5 = Object.clone(this.state.socks5);
      socks5.port = parseInt(e.target.value) || 0;
      this.setState({
        socks5
      });
    };

    this.handlePACAddrChange = e => {
      this.setState({
        pacAddr: e.target.value
      });
    };

    this.handleSetRetries = e => {
      this.setState({
        retries: parseInt(e.target.value) || 0
      });
    };

    this.handleSetPort = e => {
      this.setState({
        port: parseInt(e.target.value) || 0
      });
    };

    this.handleSetAllowLAN = e => {
      this.setState({
        allowLAN: !this.state.allowLAN
      });
    };

    this.handleShowAdvanced = e => {
      this.setState({
        showAdvanced: !this.state.showAdvanced
      });
    };

    this.state = Object.clone(props);
  }

  render() {
    return _react2.default.createElement(
      'form',
      null,
      _react2.default.createElement(_divider2.default, { text: __('Proxy server information') }),
      _react2.default.createElement(
        _reactBootstrap.Grid,
        null,
        _react2.default.createElement(
          _reactBootstrap.Col,
          { xs: 12 },
          _react2.default.createElement(
            _reactBootstrap.FormControl,
            { componentClass: 'select', value: this.state.use || "none", onChange: this.handleChangeUse },
            _react2.default.createElement(
              'option',
              { key: 0, value: 'http' },
              'HTTP ',
              __("proxy")
            ),
            _react2.default.createElement(
              'option',
              { key: 1, value: 'socks5' },
              'Socks5 ',
              __("proxy")
            ),
            _react2.default.createElement(
              'option',
              { key: 2, value: 'pac' },
              'PAC ',
              __("file"),
              ' (',
              __("Experimental"),
              ')'
            ),
            _react2.default.createElement(
              'option',
              { key: 3, value: 'none' },
              __("No proxy")
            )
          )
        )
      ),
      this.state.use === 'http' ? _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,
              __('Proxy server address')
            ),
            _react2.default.createElement(_reactBootstrap.FormControl, { type: 'text', placeholder: __('Proxy server address'), value: this.state.http.host, onChange: this.handleHttpHostChange })
          )
        ),
        _react2.default.createElement(
          _reactBootstrap.Col,
          { xs: 6 },
          _react2.default.createElement(
            _reactBootstrap.FormGroup,
            null,
            _react2.default.createElement(
              _reactBootstrap.ControlLabel,
              null,
              __('Proxy server port')
            ),
            _react2.default.createElement(_reactBootstrap.FormControl, { type: 'text', placeholder: __('Proxy server port'), value: this.state.http.port, onChange: this.handleHttpPortChange })
          )
        ),
        _react2.default.createElement(
          _reactBootstrap.Col,
          { xs: 12 },
          _react2.default.createElement(
            _reactBootstrap.Checkbox,
            { checked: !!this.state.http.requirePassword, onChange: this.handleSetHttpRequirePassword },
            __('Proxy server requires password')
          )
        ),
        _react2.default.createElement(
          'div',
          { style: !this.state.http.requirePassword ? { display: 'none' } : {} },
          _react2.default.createElement(
            _reactBootstrap.Col,
            { xs: 6 },
            _react2.default.createElement(
              _reactBootstrap.FormGroup,
              null,
              _react2.default.createElement(
                _reactBootstrap.ControlLabel,
                null,
                __('Username')
              ),
              _react2.default.createElement(_reactBootstrap.FormControl, { type: 'text', placeholder: __('Username'), value: this.state.http.username, onChange: this.handleHttpUsernameChange })
            )
          ),
          _react2.default.createElement(
            _reactBootstrap.Col,
            { xs: 6 },
            _react2.default.createElement(
              _reactBootstrap.FormGroup,
              null,
              _react2.default.createElement(
                _reactBootstrap.ControlLabel,
                null,
                __('Password')
              ),
              _react2.default.createElement(_reactBootstrap.FormControl, { type: 'password', placeholder: __('Password'), value: this.state.http.password, onChange: this.handleHttpPasswordChange })
            )
          )
        )
      ) : this.state.use == 'socks5' ? _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,
              __('Proxy server address')
            ),
            _react2.default.createElement(_reactBootstrap.FormControl, { type: 'text', placeholder: __('Proxy server address'), value: this.state.socks5.host, onChange: this.handleSocksHostChange })
          )
        ),
        _react2.default.createElement(
          _reactBootstrap.Col,
          { xs: 6 },
          _react2.default.createElement(
            _reactBootstrap.FormGroup,
            null,
            _react2.default.createElement(
              _reactBootstrap.ControlLabel,
              null,
              __('Proxy server port')
            ),
            _react2.default.createElement(_reactBootstrap.FormControl, { type: 'text', placeholder: __('Proxy server port'), value: this.state.socks5.port, onChange: this.handleSocksPortChange })
          )
        )
      ) : this.state.use === 'pac' ? _react2.default.createElement(
        _reactBootstrap.Grid,
        null,
        _react2.default.createElement(
          _reactBootstrap.Col,
          { xs: 12 },
          _react2.default.createElement(
            _reactBootstrap.FormGroup,
            null,
            _react2.default.createElement(
              _reactBootstrap.ControlLabel,
              null,
              __('PAC address')
            ),
            _react2.default.createElement(_reactBootstrap.FormControl, { type: 'text', placeholder: __('PAC address'), value: this.state.pacAddr, onChange: this.handlePACAddrChange })
          )
        )
      ) : _react2.default.createElement(
        _reactBootstrap.Grid,
        null,
        _react2.default.createElement(
          _reactBootstrap.Col,
          { xs: 12 },
          _react2.default.createElement(
            'center',
            null,
            __('Will connect to server directly.')
          )
        )
      ),
      _react2.default.createElement(_divider2.default, { text: __('Times of reconnect') }),
      _react2.default.createElement(
        _reactBootstrap.Grid,
        null,
        _react2.default.createElement(
          _reactBootstrap.Col,
          { xs: 12 },
          _react2.default.createElement(_reactBootstrap.FormControl, { type: 'number', value: this.state.retries, onChange: this.handleSetRetries })
        ),
        _react2.default.createElement(
          _reactBootstrap.Col,
          { xs: 12 },
          _react2.default.createElement(
            _reactBootstrap.Alert,
            { bsStyle: 'danger' },
            __('It may be unsafe!')
          )
        )
      ),
      _react2.default.createElement(_divider2.default, { onClick: this.handleShowAdvanced, text: _react2.default.createElement(
          'span',
          null,
          _react2.default.createElement(
            'span',
            null,
            __('Advanced (require restart)')
          ),
          _react2.default.createElement(_reactFontawesome2.default, { name: this.state.showAdvanced ? 'angle-up' : 'angle-down' })
        ) }),
      _react2.default.createElement(
        _reactBootstrap.Grid,
        null,
        _react2.default.createElement(
          _reactBootstrap.Collapse,
          { 'in': this.state.showAdvanced },
          _react2.default.createElement(
            'div',
            null,
            _react2.default.createElement(
              _reactBootstrap.Col,
              { xs: 12 },
              _react2.default.createElement(
                _reactBootstrap.ControlLabel,
                null,
                __("poi port")
              ),
              _react2.default.createElement(_reactBootstrap.FormControl, { type: 'number', value: this.state.port, onChange: this.handleSetPort, placeholder: __("Default: 0 (Use random port)") })
            ),
            _react2.default.createElement(
              _reactBootstrap.Col,
              { xs: 12 },
              _react2.default.createElement(
                _reactBootstrap.Checkbox,
                { checked: this.state.allowLAN, onChange: this.handleSetAllowLAN },
                __("Allow connections from LAN")
              )
            )
          )
        )
      ),
      _react2.default.createElement(_divider2.default, { text: __('Save settings') }),
      _react2.default.createElement(
        _reactBootstrap.Grid,
        null,
        _react2.default.createElement(
          _reactBootstrap.Col,
          { xs: 12 },
          _react2.default.createElement(
            _reactBootstrap.Button,
            { bsStyle: 'success', onClick: this.handleSaveConfig, style: { width: '100%' } },
            __('Save')
          )
        )
      )
    );
  }
});

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