'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 _pathExtra = require('path-extra');

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _lodash = require('lodash');

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

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

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

exports.default = (0, _reactRedux.connect)(state => ({
  constructions: state.info.constructions,
  $ships: state.const.$ships,
  canNotify: state.misc.canNotify
}))((_temp2 = _class = class ConstructionPanel extends _react.Component {
  constructor(...args) {
    var _temp;

    return _temp = super(...args), this.handleResponse = e => {
      const { path } = e.detail;
      switch (path) {
        case '/kcsapi/api_start2':
          // Do not notify before entering the game
          this.canNotify = false;
          break;
        case '':
          this.canNotify = true;
          break;
      }
    }, this.getMaterialImage = idx => {
      return _react2.default.createElement(_icon.MaterialIcon, { materialId: idx, className: 'material-icon' });
    }, this.getDockShipName = (dockId, defaultVal) => {
      const id = (0, _lodash.get)(this.props.constructions, [dockId, 'api_created_ship_id']);
      return id ? __(i18n.resources.__(this.props.$ships[id].api_name)) : defaultVal;
    }, this.getLabelStyle = ({ isLSC }, timeRemaining) => {
      return timeRemaining > 600 && isLSC ? 'danger' : timeRemaining > 600 ? 'primary' : timeRemaining > 0 ? 'warning' : timeRemaining == 0 ? 'success' : 'default';
    }, _temp;
  }

  componentDidMount() {
    window.addEventListener('game.response', this.handleResponse);
  }
  componentWillUnmount() {
    window.removeEventListener('game.response', this.handleResponse);
  }

  render() {
    const { constructions, canNotify } = this.props;
    return _react2.default.createElement(
      'div',
      null,
      (0, _lodash.range)(4).map(i => {
        const dock = (0, _lodash.get)(constructions, i, { api_state: -1, api_complete_time: 0 });
        const isInUse = dock.api_state > 0;
        const isLSC = isInUse && dock.api_item1 >= 1000;
        const dockName = dock.api_state == -1 ? __('Locked') : dock.api_state == 0 ? __('Empty') : this.getDockShipName(i, '???');
        const completeTime = isInUse ? dock.api_complete_time : -1;
        const tooltipTitleClassname = isLSC ? { color: '#D9534F', fontWeight: 'bold' } : null;
        return _react2.default.createElement(
          _reactBootstrap.OverlayTrigger,
          { key: i, placement: 'top', overlay: _react2.default.createElement(
              _reactBootstrap.Tooltip,
              { id: `kdock-material-${i}`, style: !isInUse && { display: 'none' } },
              _react2.default.createElement(
                'span',
                { style: tooltipTitleClassname },
                dockName,
                _react2.default.createElement('br', null)
              ),
              this.getMaterialImage(1),
              ' ',
              dock.api_item1,
              this.getMaterialImage(2),
              ' ',
              dock.api_item2,
              this.getMaterialImage(3),
              ' ',
              dock.api_item3,
              this.getMaterialImage(4),
              ' ',
              dock.api_item4,
              this.getMaterialImage(7),
              ' ',
              dock.api_item5
            ) },
          _react2.default.createElement(
            'div',
            { className: 'panel-item kdock-item' },
            _react2.default.createElement(
              'span',
              { className: 'kdock-name' },
              dockName
            ),
            _react2.default.createElement(_countdownTimer.CountdownNotifierLabel, {
              timerKey: `kdock-${i + 1}`,
              completeTime: completeTime,
              isLSC: isLSC,
              getLabelStyle: this.getLabelStyle,
              getNotifyOptions: () => canNotify && completeTime >= 0 && _extends({}, this.constructor.basicNotifyConfig, {
                args: dockName,
                completeTime: completeTime
              })
            })
          )
        );
      })
    );
  }
}, _class.basicNotifyConfig = {
  icon: (0, _pathExtra.join)(ROOT, 'assets', 'img', 'operation', 'build.png'),
  type: 'construction',
  title: __('Construction'),
  message: names => `${(0, _lodash.join)(names, ', ')} ${__('built')}`
}, _temp2));
module.exports = exports['default'];