import "choerodon-ui/lib/tooltip/style";
import _Tooltip from "choerodon-ui/lib/tooltip";
import "choerodon-ui/lib/button/style";
import _Button from "choerodon-ui/lib/button";

var _dec, _class;

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

import React, { PureComponent, Fragment } from 'react';
import { inject } from 'mobx-react';
import { FormattedMessage, injectIntl } from 'react-intl';
import debounce from 'lodash/debounce';
import assign from 'lodash/assign';
import { Choerodon } from '@choerodon/boot';
import "./index.less";
var Pods = (_dec = inject('AppState'), _dec(_class = injectIntl(_class = /*#__PURE__*/function (_PureComponent) {
  _inherits(Pods, _PureComponent);

  var _super = _createSuper(Pods);

  function Pods() {
    var _this;

    _classCallCheck(this, Pods);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      btnDisable: false,
      textDisplay: false
    };
    _this.operatePodCount = debounce(function (count) {
      var _this$props = _this.props,
          store = _this$props.store,
          envId = _this$props.envId,
          name = _this$props.name,
          projectId = _this$props.AppState.currentMenuType.id,
          refresh = _this$props.refresh;
      store.operatePodCount(projectId, envId, name, count).then(function () {
        refresh();
      })["catch"](function (err) {
        Choerodon.handleResponseError(err);
      });
    }, 600);

    _this.changeTextDisplay = function () {
      var textDisplay = _this.state.textDisplay;

      if (!textDisplay) {
        textDisplay = true;
      }

      _this.setState({
        textDisplay: textDisplay
      });
    };

    _this.handleDecrease = function () {
      var _this$props2 = _this.props,
          targetCount = _this$props2.targetCount,
          handleChangeCount = _this$props2.handleChangeCount,
          podType = _this$props2.podType,
          name = _this$props2.name,
          sum = _this$props2.count.sum;
      var currentPodTargetCount = targetCount["".concat(name, "-").concat(podType)] || sum;
      var btnDisable = _this.state.btnDisable;

      if (currentPodTargetCount > 1) {
        var count = currentPodTargetCount - 1; // 最小pod数为1

        if (count <= 1) {
          btnDisable = true;
        }

        _this.changeTextDisplay();

        _this.setState({
          btnDisable: btnDisable
        });

        _this.operatePodCount(count);

        handleChangeCount(assign({}, targetCount, _defineProperty({}, "".concat(name, "-").concat(podType), count)));
      }
    };

    _this.handleIncrease = function () {
      var _this$props3 = _this.props,
          targetCount = _this$props3.targetCount,
          handleChangeCount = _this$props3.handleChangeCount,
          podType = _this$props3.podType,
          name = _this$props3.name,
          sum = _this$props3.count.sum;
      var currentPodTargetCount = targetCount["".concat(name, "-").concat(podType)] || sum;
      var btnDisable = _this.state.btnDisable;
      var count = currentPodTargetCount + 1;

      _this.changeTextDisplay();

      if (btnDisable && count > 1) {
        btnDisable = false;
      }

      _this.setState({
        btnDisable: btnDisable
      });

      _this.operatePodCount(count);

      handleChangeCount(assign({}, targetCount, _defineProperty({}, "".concat(name, "-").concat(podType), count)));
    };

    return _this;
  }

  _createClass(Pods, [{
    key: "renderCircle",
    get:
    /**
     * 获取 pod 的环形图
     * @readonly
     */
    function get() {
      var _this$props$count = this.props.count,
          sum = _this$props$count.sum,
          correct = _this$props$count.correct,
          correctCount = _this$props$count.correctCount;
      return /*#__PURE__*/React.createElement("svg", {
        width: "70",
        height: "70"
      }, /*#__PURE__*/React.createElement("circle", {
        cx: "35",
        cy: "35",
        r: "30",
        strokeWidth: sum === 0 || sum > correctCount ? 5 : 0,
        stroke: sum > 0 ? '#ffb100' : '#f3f3f3',
        className: "c7n-pod-circle-error"
      }), /*#__PURE__*/React.createElement("circle", {
        cx: "35",
        cy: "35",
        r: "30",
        className: "c7n-pod-circle",
        strokeDasharray: "".concat(correct, ", 10000")
      }), /*#__PURE__*/React.createElement("text", {
        x: "50%",
        y: "32.5",
        className: "c7n-pod-circle-num"
      }, sum), /*#__PURE__*/React.createElement("text", {
        x: "50%",
        y: "50",
        className: "c7n-pod-circle-text"
      }, sum > 1 ? 'pods' : 'pod'));
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props4 = this.props,
          podType = _this$props4.podType,
          status = _this$props4.status,
          connect = _this$props4.connect,
          name = _this$props4.name,
          sum = _this$props4.count.sum,
          targetCount = _this$props4.targetCount,
          formatMessage = _this$props4.intl.formatMessage,
          instanceStatus = _this$props4.instanceStatus;
      var _this$state = this.state,
          btnDisable = _this$state.btnDisable,
          textDisplay = _this$state.textDisplay;
      var currentPodTargetCount = targetCount["".concat(name, "-").concat(podType)] || sum; // 实际pod数和目标数不同
      // 修改过pod数

      var show = textDisplay && sum !== currentPodTargetCount && connect && status === 'success';
      var descIsEnable = instanceStatus === 'stopped' || !connect || currentPodTargetCount <= 1 || status !== 'success';
      return /*#__PURE__*/React.createElement("div", {
        className: "c7ncd-pod"
      }, /*#__PURE__*/React.createElement("div", {
        className: "c7ncd-pod-wrap"
      }, /*#__PURE__*/React.createElement("div", {
        className: "c7ncd-pod-content"
      }, this.renderCircle), podType === 'deploymentVOS' && /*#__PURE__*/React.createElement("div", {
        className: "c7ncd-pod-content c7ncd-pod-btn-wrap"
      }, /*#__PURE__*/React.createElement(_Button, {
        disabled: !(connect && status === 'success' && instanceStatus !== 'stopped'),
        className: "c7ncd-pod-btn",
        size: "small",
        icon: "expand_less",
        onClick: this.handleIncrease
      }), /*#__PURE__*/React.createElement(_Tooltip, {
        title: instanceStatus !== 'stopped' && connect && currentPodTargetCount === 1 && status === 'success' ? formatMessage({
          id: 'c7ncd.deployment.pod.disabled.tops'
        }) : '',
        placement: "bottom"
      }, /*#__PURE__*/React.createElement(_Button, {
        disabled: descIsEnable,
        className: "c7ncd-pod-btn",
        size: "small",
        icon: "expand_more",
        onClick: this.handleDecrease,
        style: {
          padding: '0 !important'
        }
      })))), show ? /*#__PURE__*/React.createElement("div", {
        className: "c7ncd-pod-count"
      }, /*#__PURE__*/React.createElement(FormattedMessage, {
        id: "ist.expand.count"
      }), /*#__PURE__*/React.createElement("span", {
        className: "c7ncd-pod-count-value"
      }, currentPodTargetCount)) : null);
    }
  }]);

  return Pods;
}(PureComponent)) || _class) || _class);
export { Pods as default };