'use strict';

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

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _class, _temp2;

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _pathExtra = require('path-extra');

var _reselect = require('reselect');

var _lodash = require('lodash');

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

var _countdownTimer = require('./countdown-timer');

var _selectors = require('views/utils/selectors');

var _tools = require('views/utils/tools');

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

const { ROOT, i18n } = window;

const __ = i18n.main.__.bind(i18n.main);

const fleetsExpeditionSelector = (0, _reselect.createSelector)(_selectors.fleetsSelector, fleets => (0, _lodash.map)(fleets, 'api_mission'));
const fleetsNamesSelector = (0, _reselect.createSelector)(_selectors.fleetsSelector, fleets => (0, _lodash.map)(fleets, 'api_name'));

exports.default = (0, _reactRedux.connect)(state => {
  const fleetsExpedition = fleetsExpeditionSelector(state);
  const fleetNames = fleetsNamesSelector(state);
  const $expeditions = state.const.$missions;
  const notifyBefore = (0, _lodash.get)((0, _selectors.configSelector)(state), 'poi.notify.expedition.value', 60);
  return {
    fleetsExpedition,
    fleetNames,
    $expeditions,
    notifyBefore,
    canNotify: state.misc.canNotify
  };
})((_temp2 = _class = class ExpeditionPanel extends _react.Component {
  constructor(...args) {
    var _temp;

    return _temp = super(...args), this.shouldComponentUpdate = (nextProps, nextState) => {
      return !(0, _lodash.isEqual)(nextProps, this.props);
    }, this.getLabelStyle = (props, timeRemaining) => {
      return timeRemaining > 600 ? 'primary' : timeRemaining > 60 ? 'warning' : timeRemaining >= 0 ? 'success' : 'default';
    }, _temp;
  }

  render() {
    const { fleetsExpedition, fleetNames, $expeditions, canNotify, notifyBefore } = this.props;
    return _react2.default.createElement(
      _reactBootstrap.Panel,
      { bsStyle: 'default' },
      (0, _lodash.range)(1, 4).map(i => {
        const [status, expeditionId, rawCompleteTime] = fleetsExpedition[i] || [-1, 0, -1];
        const fleetName = (0, _lodash.get)(fleetNames, i, '???');
        const expeditionName = status == -1 ? __('Locked') : status == 0 ? __('Ready') : (0, _lodash.get)($expeditions, [expeditionId, 'api_name'], __('???'));
        const completeTime = status > 0 ? rawCompleteTime : -1;
        return _react2.default.createElement(
          'div',
          { className: 'panel-item expedition-item', key: i },
          _react2.default.createElement(
            'span',
            { className: 'expedition-name' },
            expeditionName
          ),
          _react2.default.createElement(
            _reactBootstrap.OverlayTrigger,
            { placement: 'left', overlay: _react2.default.createElement(
                _reactBootstrap.Tooltip,
                { id: `expedition-return-by-${i}`, style: completeTime < 0 && { display: 'none' } },
                _react2.default.createElement(
                  'strong',
                  null,
                  __("Return by : ")
                ),
                (0, _tools.timeToString)(completeTime)
              ) },
            _react2.default.createElement(
              'div',
              null,
              _react2.default.createElement(_countdownTimer.CountdownNotifierLabel, {
                timerKey: `expedition-${i + 1}`,
                completeTime: completeTime,
                getLabelStyle: this.getLabelStyle,
                getNotifyOptions: () => canNotify && completeTime >= 0 && _extends({}, this.constructor.basicNotifyConfig, {
                  args: fleetName,
                  completeTime: completeTime,
                  preemptTime: notifyBefore
                })
              })
            )
          )
        );
      })
    );
  }
}, _class.basicNotifyConfig = {
  type: 'expedition',
  title: __('Expedition'),
  message: names => `${(0, _lodash.join)(names, ', ')} ${__('mission complete')}`,
  icon: (0, _pathExtra.join)(ROOT, 'assets', 'img', 'operation', 'expedition.png')
}, _temp2));
module.exports = exports['default'];