import "choerodon-ui/pro/lib/tooltip/style";
import _Tooltip from "choerodon-ui/pro/lib/tooltip";
import "choerodon-ui/pro/lib/modal/style";
import _Modal from "choerodon-ui/pro/lib/modal";

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

import React, { useCallback, useMemo } from 'react';
import { Action } from '@choerodon/boot';
import UserInfo from "../../../../../../components/userInfo";
import TimePopover from "../../../../../../components/timePopover";
import StatusTagOutLine from "../../components/statusTagOutLine";
import eventStopProp from "../../../../../../utils/eventStopProp";
import { useHostConfigStore } from "../../../../stores";
import CreateHost from "../../../create-host";
import DeleteCheck from "../deleteCheck";
import apis from "../../../../apis";

var HostsItem = function HostsItem(_ref) {
  var sshPort = _ref.sshPort,
      hostStatus = _ref.hostStatus,
      hostIp = _ref.hostIp,
      name = _ref.name,
      id = _ref.id,
      jmeterPort = _ref.jmeterPort,
      jmeterStatus = _ref.jmeterStatus,
      authType = _ref.authType,
      jmeterPath = _ref.jmeterPath,
      username = _ref.username,
      lastUpdateDate = _ref.lastUpdateDate,
      updaterInfo = _ref.updaterInfo,
      listDs = _ref.listDs,
      record = _ref.record;

  var _useHostConfigStore = useHostConfigStore(),
      prefixCls = _useHostConfigStore.prefixCls,
      formatMessage = _useHostConfigStore.formatMessage,
      intlPrefix = _useHostConfigStore.intlPrefix,
      refresh = _useHostConfigStore.refresh,
      projectId = _useHostConfigStore.projectId,
      mainStore = _useHostConfigStore.mainStore;

  var type = mainStore.getCurrentTabKey; // 主机类型 deploy / distribute_test

  var getMainStatus = useMemo(function () {
    if (type === 'deploy') {
      return hostStatus;
    }

    if (type === 'distribute_test') {
      if (jmeterStatus === 'occupied') {
        return 'occupied';
      }

      if (jmeterStatus === 'success' && hostStatus === 'success') {
        return 'success';
      }

      if (jmeterStatus === 'failed' || hostStatus === 'failed') {
        return 'failed';
      }

      return 'operating';
    }

    return 'operating';
  }, [jmeterStatus, hostStatus]);

  var handleCorrect = /*#__PURE__*/function () {
    var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
      var res;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.prev = 0;
              _context.next = 3;
              return apis.batchCorrect(projectId, [id], type);

            case 3:
              res = _context.sent;

              if (!(res && res.failed)) {
                _context.next = 6;
                break;
              }

              return _context.abrupt("return");

            case 6:
              refresh();
              _context.next = 12;
              break;

            case 9:
              _context.prev = 9;
              _context.t0 = _context["catch"](0);
              throw new Error(_context.t0);

            case 12:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, null, [[0, 9]]);
    }));

    return function handleCorrect() {
      return _ref2.apply(this, arguments);
    };
  }();

  function deleteRerord() {
    return _deleteRerord.apply(this, arguments);
  }

  function _deleteRerord() {
    _deleteRerord = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
      var res;
      return regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              _context2.prev = 0;
              _context2.next = 3;
              return apis.getDeleteHostUrl(projectId, id, type);

            case 3:
              res = _context2.sent;

              if (!(res && res.failed)) {
                _context2.next = 6;
                break;
              }

              return _context2.abrupt("return", false);

            case 6:
              refresh();
              return _context2.abrupt("return", true);

            case 10:
              _context2.prev = 10;
              _context2.t0 = _context2["catch"](0);
              throw new Error(_context2.t0);

            case 13:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2, null, [[0, 10]]);
    }));
    return _deleteRerord.apply(this, arguments);
  }

  function handleDelete() {
    return _handleDelete.apply(this, arguments);
  }

  function _handleDelete() {
    _handleDelete = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
      var modalProps;
      return regeneratorRuntime.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              modalProps = {
                key: _Modal.key(),
                children: /*#__PURE__*/React.createElement(DeleteCheck, {
                  formatMessage: formatMessage,
                  hostId: id,
                  projectId: projectId,
                  handleDelete: deleteRerord,
                  hostType: type
                }),
                footer: null
              };

              _Modal.open(modalProps);

            case 2:
            case "end":
              return _context3.stop();
          }
        }
      }, _callee3);
    }));
    return _handleDelete.apply(this, arguments);
  }

  function handleModify() {
    _Modal.open({
      key: _Modal.key(),
      title: formatMessage({
        id: "".concat(intlPrefix, ".modify")
      }),
      style: {
        width: 380
      },
      drawer: true,
      children: /*#__PURE__*/React.createElement(CreateHost, {
        hostId: id,
        refresh: refresh,
        hostType: type
      }),
      okText: formatMessage({
        id: 'save'
      })
    });
  }

  var getActionData = useCallback(function () {
    return getMainStatus !== 'operating' || getMainStatus !== 'occupied' ? [{
      service: ['choerodon.code.project.deploy.host.ps.correct'],
      text: '校准状态',
      action: handleCorrect
    }, {
      service: ['choerodon.code.project.deploy.host.ps.edit'],
      text: '修改',
      action: handleModify
    }, {
      service: ['choerodon.code.project.deploy.host.ps.delete'],
      text: formatMessage({
        id: 'delete'
      }),
      action: handleDelete
    }] : [];
  }, [getMainStatus, handleCorrect, handleDelete, handleModify]);
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-content-list-item")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-content-list-item-header")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-content-list-item-header-left")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-content-list-item-header-left-top")
  }, /*#__PURE__*/React.createElement(StatusTagOutLine, {
    status: getMainStatus
  }), /*#__PURE__*/React.createElement(_Tooltip, {
    title: name,
    placement: "top"
  }, /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-content-list-item-header-left-top-name")
  }, name))), /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-content-list-item-header-left-bottom")
  }, /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(UserInfo, {
    name: (updaterInfo === null || updaterInfo === void 0 ? void 0 : updaterInfo.ldap) ? "".concat(updaterInfo === null || updaterInfo === void 0 ? void 0 : updaterInfo.realName, "(").concat(updaterInfo === null || updaterInfo === void 0 ? void 0 : updaterInfo.loginName, ")") : "".concat(updaterInfo === null || updaterInfo === void 0 ? void 0 : updaterInfo.loginName, "(").concat(updaterInfo === null || updaterInfo === void 0 ? void 0 : updaterInfo.email, ")"),
    showName: false,
    avatar: updaterInfo === null || updaterInfo === void 0 ? void 0 : updaterInfo.imageUrl
  })), /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("span", null, "\u66F4\u65B0\u4E8E"), /*#__PURE__*/React.createElement(TimePopover, {
    style: {
      marginLeft: '4px',
      fontSize: '12px',
      fontFamily: 'PingFangSC-Regular, PingFang SC',
      fontWeight: 400,
      color: 'rgba(58, 52, 95, 0.65)'
    },
    content: lastUpdateDate
  })))), getMainStatus !== 'operating' && getMainStatus !== 'occupied' && /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-content-list-item-header-right")
  }, /*#__PURE__*/React.createElement(Action, {
    data: getActionData(),
    onClick: eventStopProp,
    style: {
      color: '#5365EA',
      marginRight: '4px'
    }
  }))), /*#__PURE__*/React.createElement("main", {
    className: "".concat(prefixCls, "-content-list-item-main")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-content-list-item-main-item")
  }, /*#__PURE__*/React.createElement("span", null, "IP\u4E0E\u7AEF\u53E3"), /*#__PURE__*/React.createElement("span", null, hostIp && sshPort ? "".concat(hostIp, "\uFF1A").concat(sshPort) : '-')), type === 'distribute_test' && /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-content-list-item-main-item")
  }, /*#__PURE__*/React.createElement("span", null, "Jmeter\u7AEF\u53E3"), /*#__PURE__*/React.createElement("span", null, jmeterPort)), /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-content-list-item-main-item")
  }, /*#__PURE__*/React.createElement("span", null, "Jmeter\u8DEF\u5F84"), /*#__PURE__*/React.createElement("span", null, jmeterPath)))));
};

export default HostsItem;