import Footer from '@/components/Footer';
import RightContent from '@/components/RightContent';
import type { Settings as LayoutSettings } from '@ant-design/pro-components';
import { PageLoading } from '@ant-design/pro-components';
import type { RunTimeLayoutConfig } from 'umi';
import { history } from 'umi';
import defaultSettings from '../defaultSettings';
import { userInfo } from '@/services/login';
import { ErrorShowType, RequestConfig } from '@@/plugin-request/request';

// const isDev = process.env.NODE_ENV === 'development';
const loginPath = '/user/login';
const registerPath = '/user/register';
const enrollLoginPath = '/enroll/login';
const enrollDetailPath = '/enroll/detail';
const paperLoginPath = '/paper/login';
const paperDetail = '/paper/detail';
const paperMobileDetail = '/paper/mobile/detail';
const paperMobileQuestion = '/paper/mobile/question';
const paperResult = '/paper/result';
const homePath = '/home';
const privacyPath = '/privacy';
const agreementPath = '/agreement';
const taskListPath = '/taskList';
const caseListPath = '/caseList';
const caseDetailPath = '/caseDetail';
const articleDetailPath = '/articleDetail';
const partnerPath = '/partner';
const contactUsPath = '/contactUs';
const aboutPath = '/about';
const root = '/';

const noLoginPath = [
  loginPath,
  registerPath,
  homePath,
  paperLoginPath,
  paperDetail,
  paperMobileDetail,
  paperMobileQuestion,
  paperResult,
  privacyPath,
  agreementPath,
  taskListPath,
  enrollLoginPath,
  enrollDetailPath,
  caseListPath,
  caseDetailPath,
  articleDetailPath,
  partnerPath,
  contactUsPath,
  aboutPath,
];

/** 获取用户信息比较慢的时候会展示一个 loading */
// export const initialStateConfig = {
//   loading: <PageLoading />,
// };

const needLogin = () => {
  if (history.location.pathname == root) {
    return false;
  }
  for (let i = 0; i < noLoginPath.length; i++) {
    const path = noLoginPath[i];
    if (history.location.pathname.indexOf(path) >= 0) {
      return false;
    }
  }
  return true;
};

/**
 * @see  https://umijs.org/zh-CN/plugins/plugin-initial-state
 * */
// export async function getInitialState(): Promise<{
//   settings?: Partial<LayoutSettings>;
//   currentUser?: API.User;
//   loading?: boolean;
//   fetchUserInfo?: () => Promise<API.User | undefined>;
// }> {
//   const fetchUserInfo = async () => {
//     try {
//       const msg = await userInfo();
//       return msg.data;
//     } catch (error) {
//       history.push(loginPath);
//     }
//     return undefined;
//   };

//   // console.log('needLogin?', needLogin());

//   if (needLogin()) {
//     const currentUser = await fetchUserInfo();
//     return {
//       fetchUserInfo,
//       currentUser,
//       settings: defaultSettings,
//     };
//   }
//   return {
//     fetchUserInfo,
//     settings: defaultSettings,
//   };
// }

// export const layout: RunTimeLayoutConfig = ({
//   initialState,
//   setInitialState,
// }) => {
//   return {
//     logo: (
//       <>
//         <span className={'menu-logo'}>HIPO </span>
//         <span className={'menu-title'}>用户管理后台</span>
//       </>
//     ),
//     headerHeight: 56,
//     rightContentRender: () => <RightContent />,
//     disableContentMargin: false,
//     waterMarkProps: {
//       content:
//         initialState?.currentUser?.name || initialState?.currentUser?.phone,
//     },
//     footerRender: () => <Footer />,
//     onPageChange: () => {
//       if (!initialState?.currentUser && needLogin()) {
//         history.push(loginPath);
//       }
//     },
//     menuHeaderRender: undefined,
//     // 自定义 403 页面
//     // unAccessible: <div>unAccessible</div>,
//     // 增加一个 loading 的状态
//     ...initialState?.settings,
//   };
// };

export const request: RequestConfig = {
  requestType: 'form',

  errorConfig: {
    adaptor: (resData) => {
      return {
        ...resData,
        success: resData.code == 200,
        errorMessage: resData.message,
        errorCode: resData.code,
        showType: ErrorShowType.REDIRECT,
      };
    },
    errorPage: '/404',
  },
};
