'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.MiniSquardRow = exports.MiniShipRow = undefined;

var _class, _temp;

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

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

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

var _shallowEqual = require('fbjs/lib/shallowEqual');

var _shallowEqual2 = _interopRequireDefault(_shallowEqual);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _reselect = require('reselect');

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

var _lodash = require('lodash');

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

var _reactFontawesome2 = _interopRequireDefault(_reactFontawesome);

var _statuslabel = require('views/components/ship-parts/statuslabel');

var _statuslabel2 = _interopRequireDefault(_statuslabel);

var _slotitems = require('views/components/ship/slotitems');

var _icon = require('views/components/etc/icon');

var _gameUtils = require('views/utils/game-utils');

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

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

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

const slotitemsDataSelectorFactory = (0, _lodash.memoize)(shipId => (0, _reselect.createSelector)([(0, _selectors.shipDataSelectorFactory)(shipId), (0, _selectors.shipEquipDataSelectorFactory)(shipId)], ([ship, $ship] = [], equipsData, exslot) => ({
  api_maxeq: ($ship || {}).api_maxeq,
  equipsData
})));

const Slotitems = (0, _reactRedux.connect)((state, { shipId }) => slotitemsDataSelectorFactory(shipId)(state))(function ({ api_maxeq, equipsData }) {
  const tooltipClassName = (0, _classnames2.default)("item-name", {
    "hidden": !equipsData
  });
  return _react2.default.createElement(
    'div',
    { className: tooltipClassName },
    _react2.default.createElement(
      'div',
      { className: 'slotitems-mini', style: { display: "flex", flexFlow: "column" } },
      equipsData.filter(Boolean).map((equipData, equipIdx) => {
        const [equip, $equip, onslot] = equipData;
        const equipIconId = $equip.api_type[3];
        const level = equip.api_level;
        const proficiency = equip.api_alv;
        const isAircraft = (0, _gameUtils.equipIsAircraft)(equipIconId);
        const maxOnslot = (api_maxeq || [])[equipIdx];
        const onslotText = onslot;
        const onslotWarning = maxOnslot && onslot < maxOnslot;
        const onslotClassName = (0, _classnames2.default)("slotitem-onslot", {
          'show': isAircraft,
          'hide': !isAircraft
        });
        return _react2.default.createElement(
          'div',
          { key: equipIdx, className: 'slotitem-container-mini' },
          _react2.default.createElement(_icon.SlotitemIcon, { key: equip.api_id, className: 'slotitem-img', slotitemId: equipIconId }),
          _react2.default.createElement(
            'span',
            { className: 'slotitem-name-mini' },
            i18n.resources.__(($equip || { api_name: '??' }).api_name),
            level ? _react2.default.createElement(
              'strong',
              { style: { color: '#45A9A5' } },
              ' ',
              _react2.default.createElement(_reactFontawesome2.default, { name: 'star' }),
              level
            ) : '',
            '\xA0\xA0',
            proficiency && _react2.default.createElement('img', { className: 'alv-img', src: (0, _pathExtra.join)('assets', 'img', 'airplane', `alv${proficiency}.png`) })
          ),
          _react2.default.createElement(
            _reactBootstrap.Label,
            {
              className: onslotClassName,
              bsStyle: `${onslotWarning ? 'warning' : 'default'}`
            },
            onslotText
          )
        );
      })
    )
  );
});

const miniShipRowDataSelectorFactory = (0, _lodash.memoize)(shipId => (0, _reselect.createSelector)([(0, _selectors.shipDataSelectorFactory)(shipId), (0, _selectors.shipRepairDockSelectorFactory)(shipId), (0, _selectors.escapeStatusSelectorFactory)(shipId), _selectors.configLayoutSelector, _selectors.configDoubleTabbedSelector], ([ship, $ship] = [], repairDock, escaped, layout, doubleTabbed) => ({
  ship: ship || {},
  $ship: $ship || {},
  labelStatus: (0, _gameUtils.getShipLabelStatus)(ship, $ship, repairDock, escaped),
  layout,
  doubleTabbed
})));

const MiniShipRow = exports.MiniShipRow = (0, _reactRedux.connect)((state, { shipId }) => miniShipRowDataSelectorFactory(shipId))((_temp = _class = class miniShipRow extends _react.Component {

  shouldComponentUpdate(nextProps) {
    // Remember to expand the list in case you add new properties to display
    const shipPickProps = ['api_lv', 'api_exp', 'api_id', 'api_nowhp', 'api_maxhp', 'api_cond', 'api_slot', 'api_slot_ex'];
    return !(0, _shallowEqual2.default)((0, _lodash.omit)(this.props, ['ship']), (0, _lodash.omit)(nextProps, ['ship'])) || !(0, _lodash.isEqual)((0, _lodash.pick)(this.props.ship, shipPickProps), (0, _lodash.pick)(nextProps.ship, shipPickProps));
  }

  render() {
    const { ship, $ship, labelStatus, layout, doubleTabbed } = this.props;
    if (!ship) return _react2.default.createElement('div', null);
    const labelStatusStyle = (0, _gameUtils.getStatusStyle)(labelStatus);
    const hpPercentage = ship.api_nowhp / ship.api_maxhp * 100;
    return _react2.default.createElement(
      'div',
      { className: 'ship-tile' },
      _react2.default.createElement(
        _reactBootstrap.OverlayTrigger,
        {
          placement: !doubleTabbed && layout == 'vertical' ? 'left' : 'right',
          overlay: ship.api_slot[0] !== -1 || ship.api_slot_ex > 0 ? _react2.default.createElement(
            _reactBootstrap.Tooltip,
            { id: `ship-pop-${ship.api_id}`, className: 'ship-pop' },
            _react2.default.createElement(Slotitems, { shipId: ship.api_id })
          ) : _react2.default.createElement(_reactBootstrap.Tooltip, { id: `ship-pop-${ship.api_id}`, style: { display: 'none' } })
        },
        _react2.default.createElement(
          'div',
          { className: 'ship-item' },
          _react2.default.createElement(
            _reactBootstrap.OverlayTrigger,
            { placement: 'top', overlay: _react2.default.createElement(
                _reactBootstrap.Tooltip,
                { id: `miniship-exp-${ship.api_id}` },
                'Next. ',
                (ship.api_exp || [])[1]
              ) },
            _react2.default.createElement(
              'div',
              { className: 'ship-info' },
              _react2.default.createElement(
                'span',
                { className: 'ship-name', style: labelStatusStyle },
                i18n.resources.__($ship.api_name || '??')
              ),
              _react2.default.createElement(
                'span',
                { className: 'ship-lv-text top-space', style: labelStatusStyle },
                'Lv. ',
                ship.api_lv || '??'
              )
            )
          ),
          _react2.default.createElement(
            'div',
            { className: 'ship-stat' },
            _react2.default.createElement(
              'div',
              { className: 'div-row' },
              _react2.default.createElement(
                'span',
                { className: 'ship-hp', style: labelStatusStyle },
                ship.api_nowhp,
                ' / ',
                ship.api_maxhp
              ),
              _react2.default.createElement(
                'div',
                { className: 'status-label' },
                _react2.default.createElement(_statuslabel2.default, { label: labelStatus })
              ),
              _react2.default.createElement(
                'div',
                { style: labelStatusStyle },
                _react2.default.createElement(
                  'span',
                  { className: "ship-cond " + (0, _gameUtils.getCondStyle)(ship.api_cond) },
                  _react2.default.createElement(_reactFontawesome2.default, { name: 'star' }),
                  ship.api_cond
                )
              )
            ),
            _react2.default.createElement(
              'span',
              { className: 'hp-progress top-space', style: labelStatusStyle },
              _react2.default.createElement(_reactBootstrap.ProgressBar, { bsStyle: (0, _gameUtils.getHpStyle)(hpPercentage), now: hpPercentage })
            )
          )
        )
      )
    );
  }
}, _class.propTypes = {
  ship: _propTypes2.default.object,
  $ship: _propTypes2.default.object,
  labelStatus: _propTypes2.default.number,
  layout: _propTypes2.default.string,
  doubleTabbed: _propTypes2.default.bool
}, _temp));

const MiniSquardRow = exports.MiniSquardRow = (0, _reactRedux.connect)((state, { squardId }) => (0, _reselect.createSelector)([(0, _selectors.landbaseSelectorFactory)(squardId), (0, _selectors.landbaseEquipDataSelectorFactory)(squardId)], (landbase, equipsData) => ({
  landbase,
  equipsData,
  squardId
})))(({ landbase, equipsData, squardId }) => {
  const { api_action_kind, api_name } = landbase;
  const tyku = (0, _gameUtils.getTyku)([equipsData], api_action_kind);
  const statuslabel = (() => {
    switch (api_action_kind) {
      // 0=待機, 1=出撃, 2=防空, 3=退避, 4=休息
      case 0:
        return _react2.default.createElement(
          _reactBootstrap.Label,
          { bsStyle: 'default' },
          __('Standby')
        );
      case 1:
        return _react2.default.createElement(
          _reactBootstrap.Label,
          { bsStyle: 'danger' },
          __('Sortie')
        );
      case 2:
        return _react2.default.createElement(
          _reactBootstrap.Label,
          { bsStyle: 'warning' },
          __('Defense')
        );
      case 3:
        return _react2.default.createElement(
          _reactBootstrap.Label,
          { bsStyle: 'primary' },
          __('Retreat')
        );
      case 4:
        return _react2.default.createElement(
          _reactBootstrap.Label,
          { bsStyle: 'success' },
          __('Rest')
        );
    }
  })();
  return _react2.default.createElement(
    'div',
    { className: 'ship-tile' },
    _react2.default.createElement(
      'div',
      { className: 'ship-item' },
      _react2.default.createElement(
        'div',
        { className: 'ship-info' },
        _react2.default.createElement(
          'span',
          { className: 'ship-name' },
          api_name
        ),
        _react2.default.createElement(
          'span',
          { className: 'ship-lv-text top-space' },
          _react2.default.createElement(
            'div',
            { className: 'ship-fp' },
            __('Fighter Power'),
            ': ',
            tyku.max
          ),
          statuslabel
        )
      ),
      _react2.default.createElement(
        'div',
        { className: 'ship-stat landbase-stat' },
        _react2.default.createElement(
          'div',
          { className: 'div-row' },
          _react2.default.createElement(_slotitems.LandbaseSlotitems, { landbaseId: squardId, isMini: true })
        )
      )
    )
  );
});