'use strict';

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

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

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

var _reactFontawesome2 = _interopRequireDefault(_reactFontawesome);

var _lodash = require('lodash');

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

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

const fatiguedLabel = _react2.default.createElement(
  _reactBootstrap.Label,
  { bsStyle: 'danger', className: 'airbase-state-label' },
  __('Fatigued')
);
const emptyLabel = _react2.default.createElement(
  _reactBootstrap.Label,
  { bsStyle: 'warning', className: 'airbase-state-label' },
  __('Empty slot')
);
const relocateLabel = _react2.default.createElement(
  _reactBootstrap.Label,
  { bsStyle: 'warning', className: 'airbase-state-label' },
  __('Relocating')
);
const resupplyLabel = _react2.default.createElement(
  _reactBootstrap.Label,
  { bsStyle: 'warning', className: 'airbase-state-label' },
  __('Resupply needed')
);
const noActionLabel = _react2.default.createElement(
  _reactBootstrap.Label,
  { bsStyle: 'warning', className: 'airbase-state-label' },
  __('No action')
);
const readyLabel = _react2.default.createElement(
  _reactBootstrap.Label,
  { bsStyle: 'success', className: 'airbase-state-label' },
  __('Ready')
);

const LandbaseButton = exports.LandbaseButton = (0, _reactRedux.connect)(state => ({
  sortieStatus: (0, _lodash.get)(state, 'sortie.sortieStatus', []),
  airbase: (0, _lodash.get)(state, 'info.airbase', []),
  mapareas: (0, _lodash.get)(state, 'const.$mapareas', {})
}))(({ fleetId, activeFleetId, onClick, disabled, airbase, sortieStatus, mapareas, isMini }) => {
  const airbaseProps = airbase.map(a => a.api_area_id).filter(a => mapareas[a]).sort((a, b) => a - b).filter((a, i, arr) => a != arr[i - 1]).map(i => ({
    mapId: i,
    needSupply: airbase.filter(a => mapareas[a.api_area_id]).filter(a => a.api_area_id === i).map(a => a.api_plane_info.map(s => s.api_count !== s.api_max_count).reduce((a, b) => a || b, false)).reduce((a, b) => a || b, false),
    // 0: 未配属あり, 1: 配属済み, >1: 配置転換中あり
    squardState: airbase.filter(a => mapareas[a.api_area_id]).filter(a => a.api_area_id === i).map(a => a.api_plane_info.map(s => s.api_state).reduce((a, b) => a * b, 1)).reduce((a, b) => a * b, 1),
    // 1: 通常, >1: 黄疲労・赤疲労あり
    squardCond: airbase.filter(a => mapareas[a.api_area_id]).filter(a => a.api_area_id === i).map(a => a.api_plane_info.map(s => s.api_cond || 1).reduce((a, b) => a * b, 1)).reduce((a, b) => a * b, 1),
    noAction: airbase.filter(a => mapareas[a.api_area_id]).filter(a => a.api_area_id === i).map(a => a.api_action_kind !== 1 && a.api_action_kind !== 2).reduce((a, b) => a || b, false),
    allEmpty: airbase.filter(a => mapareas[a.api_area_id]).filter(a => a.api_area_id === i).map(a => a.api_plane_info.map(s => s.api_state === 0).reduce((a, b) => a && b, true)).reduce((a, b) => a && b, true)
  }));
  const needSupply = airbaseProps.filter(a => !a.allEmpty).map(a => a.needSupply).reduce((a, b) => a || b, false);
  const squardState = airbaseProps.filter(a => !a.allEmpty).map(a => a.squardState).reduce((a, b) => a * b, 1);
  const squardCond = airbaseProps.filter(a => !a.allEmpty).map(a => a.squardCond).reduce((a, b) => a * b, 1);
  const noAction = airbaseProps.filter(a => !a.allEmpty).map(a => a.noAction).reduce((a, b) => a || b, false);
  const sortie = sortieStatus.filter(a => !a.allEmpty).reduce((a, b) => a || b, false);
  const bsStyle = (() => {
    if (sortie) {
      return 'default';
    } else if (squardCond > 1) {
      return 'danger';
    } else if (squardState !== 1 || needSupply) {
      return 'warning';
    } else if (noAction) {
      return 'info';
    } else {
      return 'success';
    }
  })();
  const propTooltip = _react2.default.createElement(
    _reactBootstrap.Tooltip,
    { id: isMini ? 'airbase-tooltip-mini' : 'airbase-tooltip' },
    airbaseProps.map((airbase, i) => {
      const { mapId, needSupply, squardState, squardCond, noAction } = airbase;
      return _react2.default.createElement(
        'div',
        { key: i },
        _react2.default.createElement(
          'div',
          null,
          '[',
          mapId,
          '] ',
          window.i18n.resources.__((mapareas[mapId] || {}).api_name)
        ),
        squardCond > 1 && fatiguedLabel,
        squardState < 1 && emptyLabel,
        squardState > 1 && relocateLabel,
        needSupply && resupplyLabel,
        noAction && noActionLabel,
        squardCond === 1 && squardState === 1 && !needSupply && !noAction && readyLabel
      );
    })
  );
  return _react2.default.createElement(
    _reactBootstrap.OverlayTrigger,
    { placement: 'bottom', overlay: propTooltip },
    _react2.default.createElement(
      _reactBootstrap.Button,
      {
        bsSize: isMini ? 'xsmall' : 'small',
        bsStyle: bsStyle,
        onClick: onClick,
        disabled: disabled,
        className: fleetId == activeFleetId ? 'active' : ''
      },
      _react2.default.createElement(_reactFontawesome2.default, { name: 'plane' })
    )
  );
});