import "choerodon-ui/pro/lib/modal/style";
import _Modal from "choerodon-ui/pro/lib/modal";
import React, { useMemo, useState } from 'react';
import { observer } from 'mobx-react-lite';
import { HeaderButtons } from '@choerodon/master';
import { useResourceStore } from "../../../../stores";
import { useApplicationStore } from "../stores";
import Detail from "./detail";
import KeyValueModal from "./key-value/KeyValueProIndex";
import CreateNetwork2 from "./network2";
import DomainForm from "../../../components/domain-form";

var modalKey1 = _Modal.key();

var modalKey2 = _Modal.key();

var createNetWorkKey = _Modal.key();

var createDomainKey = _Modal.key();

var modalStyle2 = {
  width: 'calc(100vw - 3.52rem)'
};
var modalStyle3 = {
  width: 740
};
var AppModals = observer(function () {
  var modalStyle = useMemo(function () {
    return {
      width: 380
    };
  }, []);

  var _useResourceStore = useResourceStore(),
      intlPrefix = _useResourceStore.intlPrefix,
      prefixCls = _useResourceStore.prefixCls,
      formatMessage = _useResourceStore.intl.formatMessage,
      resourceStore = _useResourceStore.resourceStore,
      treeDs = _useResourceStore.treeDs;

  var _useApplicationStore = useApplicationStore(),
      _useApplicationStore$ = _useApplicationStore.tabs,
      NET_TAB = _useApplicationStore$.NET_TAB,
      MAPPING_TAB = _useApplicationStore$.MAPPING_TAB,
      CIPHER_TAB = _useApplicationStore$.CIPHER_TAB,
      baseInfoDs = _useApplicationStore.baseInfoDs,
      mappingStore = _useApplicationStore.mappingStore,
      cipherStore = _useApplicationStore.cipherStore,
      networkStore = _useApplicationStore.networkStore,
      netDs = _useApplicationStore.netDs,
      mappingDs = _useApplicationStore.mappingDs,
      cipherDs = _useApplicationStore.cipherDs,
      appStore = _useApplicationStore.appStore,
      checkAppExist = _useApplicationStore.checkAppExist;

  var _resourceStore$getSel = resourceStore.getSelectedMenu,
      id = _resourceStore$getSel.id,
      parentId = _resourceStore$getSel.parentId,
      appType = _resourceStore$getSel.type;

  function refresh() {
    checkAppExist().then(function (query) {
      if (query) {
        treeDs.query();
        baseInfoDs.query();
        var current = appStore.getTabKey;

        switch (current) {
          case NET_TAB:
            netDs.query();
            break;

          case MAPPING_TAB:
            mappingDs.query();
            break;

          case CIPHER_TAB:
            cipherDs.query();
            break;

          default:
        }
      }
    });
  }

  function setTabKey(key) {
    var current = appStore.getTabKey;

    if (current !== key) {
      appStore.setTabKey(key);
    }

    refresh();
  }

  function openDetail() {
    var record = baseInfoDs.current;
    if (!record) return;

    _Modal.open({
      key: modalKey1,
      title: formatMessage({
        id: "".concat(intlPrefix, ".service.detail")
      }),
      children: /*#__PURE__*/React.createElement(Detail, {
        record: record,
        intlPrefix: intlPrefix,
        prefixCls: prefixCls,
        formatMessage: formatMessage,
        type: appType
      }),
      drawer: true,
      style: modalStyle,
      okCancel: false,
      okText: formatMessage({
        id: 'close'
      })
    });
  }

  function openKeyValue(type) {
    _Modal.open({
      key: modalKey2,
      style: modalStyle2,
      drawer: true,
      title: formatMessage({
        id: "".concat(intlPrefix, ".").concat(type, ".create")
      }),
      children: /*#__PURE__*/React.createElement(KeyValueModal, {
        intlPrefix: intlPrefix,
        modeSwitch: type === MAPPING_TAB,
        title: type,
        envId: parentId,
        appId: id,
        store: type === MAPPING_TAB ? mappingStore : cipherStore,
        refresh: function refresh() {
          return setTabKey(type);
        }
      }),
      okText: formatMessage({
        id: 'create'
      })
    });
  }

  function saveNetworkIds(ids) {
    var getTabKey = appStore.getTabKey,
        setNetworkIds = appStore.setNetworkIds;

    if (getTabKey === 'net') {
      setNetworkIds(ids);
    }
  }

  function openNetWork() {
    _Modal.open({
      key: createNetWorkKey,
      title: formatMessage({
        id: 'network.header.create'
      }),
      style: {
        width: 740
      },
      okText: formatMessage({
        id: 'create'
      }),
      drawer: true,
      children: /*#__PURE__*/React.createElement(CreateNetwork2, {
        envId: parentId,
        appId: id,
        networkStore: networkStore,
        refresh: refresh
      })
    });
  }

  function openDomain() {
    _Modal.open({
      key: createDomainKey,
      style: modalStyle3,
      drawer: true,
      title: formatMessage({
        id: 'domain.create.head'
      }),
      children: /*#__PURE__*/React.createElement(DomainForm, {
        envId: parentId,
        appServiceId: id,
        refresh: refresh,
        intlPrefix: intlPrefix,
        prefixCls: prefixCls,
        saveNetworkIds: saveNetworkIds
      }),
      okText: formatMessage({
        id: 'create'
      })
    });
  }

  function getButtons() {
    var envRecord = treeDs.find(function (record) {
      return record.get('key') === parentId;
    });
    var connect = envRecord.get('connect');
    var notReady = !baseInfoDs.current;
    var disabled = !connect || notReady;
    return [{
      name: '创建资源',
      icon: 'playlist_add',
      groupBtnItems: [{
        permissions: ['choerodon.code.project.deploy.app-deployment.resource.ps.network'],
        disabled: disabled,
        name: formatMessage({
          id: "".concat(intlPrefix, ".create.network")
        }),
        handler: openNetWork
      }, {
        permissions: ['choerodon.code.project.deploy.app-deployment.resource.ps.domain'],
        disabled: disabled,
        name: formatMessage({
          id: "".concat(intlPrefix, ".create.ingress")
        }),
        handler: openDomain
      }, {
        permissions: ['choerodon.code.project.deploy.app-deployment.resource.ps.configmap'],
        disabled: disabled,
        name: formatMessage({
          id: "".concat(intlPrefix, ".create.configMap")
        }),
        handler: function handler() {
          return openKeyValue(MAPPING_TAB);
        }
      }, {
        permissions: ['choerodon.code.project.deploy.app-deployment.resource.ps.cipher'],
        disabled: disabled,
        name: formatMessage({
          id: "".concat(intlPrefix, ".create.cipher")
        }),
        handler: function handler() {
          return openKeyValue(CIPHER_TAB);
        }
      }]
    }, {
      disabled: notReady,
      name: formatMessage({
        id: "".concat(intlPrefix, ".service.detail")
      }),
      icon: 'find_in_page-o',
      handler: openDetail
    }, {
      icon: 'refresh',
      handler: refresh
    }];
  }

  return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(HeaderButtons, {
    items: getButtons(),
    showClassName: true
  }));
});
export default AppModals;