import {BaseResult, BaseResultContent, GSBaseResult} from '@models';
import {userStore} from '@stores';
import {E2E_NETWORK_URL, NETWORK_ERROR_TIPS, NETWORK_URL} from '@utils';
import {message} from 'antd';
import axios from 'axios';

// 普通请求
const http = axios.create({
  baseURL: process.env.IS_E2E ? E2E_NETWORK_URL : NETWORK_URL,
  headers: {
    'Content-Type': 'application/json',
    'Cache-Control': 'no-cache',
  },
});

const exportReg = /export|excel/i;

http.interceptors.request.use(function (config) {
  if (config?.url) {
    const isExport = exportReg.test(config.url);
    isExport && (config.responseType = 'blob');
  }

  const {token, id} = userStore.getState();
  config.headers.token = token;
  config.headers.userId = String(id);

  return config;
});

export async function request<T, R extends BaseResultContent<any>>(options: {
  method: 'POST' | 'GET' | 'PUT' | 'DELETE';
  url: string;
  data?: T;
  skipError?: boolean;
  useBody?: boolean;
  signal?: AbortSignal;
}) {
  const {data, skipError, method, url, useBody, signal} = options;

  let res: BaseResultContent<any>;
  const useData = useBody ? useBody : method !== 'GET' && method !== 'DELETE';

  try {
    const result = await http.request<R>({
      method,
      url,
      data: useData ? data : void 0,
      params: !useData ? data : void 0,
      signal,
    });

    res = result.data;

    if (res.msg !== '200' && !skipError && !exportReg.test(url))
      message.error(res.errMsg);
  } catch (error: any) {
    res = {msg: '510', errMsg: NETWORK_ERROR_TIPS};

    if (!skipError && error.code !== 'ERR_CANCELED' && !exportReg.test(url))
      message.error(res.errMsg);
  }

  return res;
}

// gs请求
const GSHttp = axios.create({
  baseURL: (process.env.IS_E2E ? E2E_NETWORK_URL : NETWORK_URL) + '/gsAccess',
  headers: {
    'Content-Type': 'application/json',
    'Cache-Control': 'no-cache',
  },
});

GSHttp.interceptors.request.use(function (config) {
  const {token, id} = userStore.getState();
  config.headers.token = token;
  config.headers.userId = String(id);

  return config;
});

export async function gsRequest<T>(url: string, data: T, signal?: AbortSignal) {
  let res: GSBaseResult;

  try {
    const result = await GSHttp.request<GSBaseResult>({
      method: 'POST',
      url,
      data,
      signal,
    });

    res = result.data;

    if (res.status !== '0000') message.error(res.msg);
  } catch (error) {
    res = {status: '0002', msg: NETWORK_ERROR_TIPS};

    message.error(res.msg);
  }

  return res;
}

// 上传文件
const uploadHttp = axios.create({
  baseURL: process.env.IS_E2E ? E2E_NETWORK_URL : NETWORK_URL,
  headers: {
    'Content-Type': 'multipart/form-data',
    'Cache-Control': 'no-cache',
  },
});

uploadHttp.interceptors.request.use(function (config) {
  const {token, id} = userStore.getState();
  config.headers.token = token;
  config.headers.userId = String(id);

  return config;
});

export async function uploadRequest(url: string, data: FormData): BaseResult {
  let res: BaseResultContent<unknown>;

  try {
    res = (await GSHttp.post(url, data)).data;
  } catch (error: any) {
    res = {msg: '510', errMsg: NETWORK_ERROR_TIPS};

    error.code !== 'ERR_CANCELED' && message.error(res.errMsg);
  }

  return res;
}
