import { reactive, toRaw } from 'vue';
import { getPara5GFilee, savePara5GFile, updateNeInfo } from '@/api/ne/neInfo';
import useNeInfoStore from '@/store/modules/neinfo';
import { RESULT_CODE_SUCCESS } from '@/constants/result-constants';

/**对象信息信息状态类型 */
type StateType = {
  /**表单数据 */
  from: Record<string, any>;
  /**OMC信息，需修改当前的IP */
  omcInfo: Record<string, any>;
  /**根据网元显示配置项 */
  hasNE: {
    amf: boolean;
    upf: boolean;
    ims: boolean;
    mme: boolean;
  };
  /**确定按钮 loading */
  confirmLoading: boolean;
};

export function usePara5G() {
  /**对象信息状态 */
  let state: StateType = reactive({
    from: {},
    omcInfo: {},
    hasNE: {
      amf: false,
      upf: false,
      ims: false,
      mme: false,
    },
    confirmLoading: false,
  });

  /**载入数据*/
  function fnReloadData() {
    state.confirmLoading = true;
    Promise.all([getPara5GFilee(), useNeInfoStore().fnRefreshNelist()]).then(
      resArr => {
        // 已保存的配置
        if (resArr[0].code === RESULT_CODE_SUCCESS) {
          Object.assign(state.from, resArr[0].data);
        }
        // 填充固定网元类型的ip
        if (
          resArr[1].code === RESULT_CODE_SUCCESS &&
          Array.isArray(resArr[1].data)
        ) {
          for (const item of resArr[1].data) {
            // 公共配置文件sbi的各网元IP
            switch (item.neType) {
              case 'OMC':
                state.from.sbi.omc_ip = item.ip;
                Object.assign(state.omcInfo, item); // 主动改OMC_IP
                break;
              case 'IMS':
                state.from.sbi.ims_ip = item.ip;
                state.hasNE.ims = true;
                break;
              case 'AMF':
                state.from.sbi.amf_ip = item.ip;
                state.hasNE.amf = true;
                break;
              case 'AUSF':
                state.from.sbi.ausf_ip = item.ip;
                break;
              case 'UDM':
                state.from.sbi.udm_ip = item.ip;
                state.from.sbi.db_ip = '0.0.0.0';
                break;
              case 'SMF':
                state.from.sbi.smf_ip = item.ip;
                break;
              case 'PCF':
                state.from.sbi.pcf_ip = item.ip;
                break;
              case 'NSSF':
                state.from.sbi.nssf_ip = item.ip;
                break;
              case 'NRF':
                state.from.sbi.nrf_ip = item.ip;
                break;
              case 'UPF':
                state.from.sbi.upf_ip = item.ip;
                state.hasNE.upf = true;
                break;
              case 'LMF':
                state.from.sbi.lmf_ip = item.ip;
                break;
              case 'NEF':
                state.from.sbi.nef_ip = item.ip;
                break;
              case 'MME':
                state.from.sbi.mme_ip = item.ip;
                if (item.ip.includes('.')) {
                  state.from.external.mmes11_ip = item.ip + '/24';
                }
                state.hasNE.mme = true;
                break;
              case 'N3IWF':
                state.from.sbi.n3iwf_ip = item.ip;
                break;
              case 'SMSC':
                state.from.sbi.smsc_ip = item.ip;
                break;
            }
          }
        }
        state.confirmLoading = false;
      }
    );
  }

  /**保存数据 */
  async function fnSaveData() {
    if (state.confirmLoading) return;
    state.confirmLoading = true;
    const res = await savePara5GFile({
      content: toRaw(state.from),
      syncNe: [],
    });
    if (res.code === RESULT_CODE_SUCCESS) {
      // 更新omc_ip
      if (state.omcInfo.id) {
        state.omcInfo.ip = state.from.sbi.omc_ip;
        await updateNeInfo(toRaw(state.omcInfo));
      }
    }
    state.confirmLoading = false;
    return res;
  }

  return {
    state,
    fnReloadData,
    fnSaveData,
  };
}
