import Mock from 'mockjs';
import { DEFAULT_USER_AVATAR } from '@/constants/avatar';
import { isLogin } from '@/utils/auth';
import setupMock, {
  failResponseWrap,
  successResponseWrap,
} from '@/mocks/setup';

export interface MockParams {
  url: string;
  type: string;
  body: string;
}

setupMock({
  setup() {
    Mock.mock(/\/api\/user\/info/, () => {
      if (isLogin()) {
        const role = window.localStorage.getItem('userRole') || 'admin';
        return successResponseWrap({
          name: 'admin',
          avatar: DEFAULT_USER_AVATAR,
          email: 'wangliqun@email.com',
          job: 'frontend',
          jobName: '\u524d\u7aef\u827a\u672f\u5bb6',
          organization: 'Frontend',
          organizationName: '\u524d\u7aef',
          location: 'beijing',
          locationName: '\u5317\u4eac',
          introduction: '\u4eba\u7206\u723d\uff0c\u6027\u6e29\u7eaf',
          personalWebsite: 'https://www.arco.design',
          phone: '150****0000',
          registrationDate: '2013-05-10 12:10:00',
          accountId: '15012312300',
          certification: 1,
          role,
        });
      }
      return failResponseWrap(null, '\u672a\u767b\u5f55', 50008);
    });

    Mock.mock(/\/api\/user\/login/, (params: MockParams) => {
      const { username, password } = JSON.parse(params.body);
      if (!username) {
        return failResponseWrap(
          null,
          '\u7528\u6237\u540d\u4e0d\u80fd\u4e3a\u7a7a',
          50000,
        );
      }
      if (!password) {
        return failResponseWrap(null, '\u5bc6\u7801\u4e0d\u80fd\u4e3a\u7a7a', 50000);
      }
      if (username === 'admin' && password === 'admin') {
        window.localStorage.setItem('userRole', 'admin');
        return successResponseWrap({ token: '12345' });
      }
      if (username === 'user' && password === 'user') {
        window.localStorage.setItem('userRole', 'user');
        return successResponseWrap({ token: '54321' });
      }
      return failResponseWrap(
        null,
        '\u8d26\u53f7\u6216\u8005\u5bc6\u7801\u9519\u8bef',
        50000,
      );
    });

    Mock.mock(/\/api\/user\/logout/, () => successResponseWrap(null));
    Mock.mock(/\/api\/user\/menu/, () => {
      const menuList = [
        {
          path: '/dashboard',
          name: 'dashboard',
          meta: {
            locale: 'menu.server.dashboard',
            requiresAuth: true,
            icon: 'icon-dashboard',
            order: 1,
          },
          children: [
            {
              path: 'workplace',
              name: 'Workplace',
              meta: {
                locale: 'menu.server.workplace',
                requiresAuth: true,
              },
            },
            {
              path: 'https://arco.design',
              name: 'arcoWebsite',
              meta: {
                locale: 'menu.arcoWebsite',
                requiresAuth: true,
              },
            },
          ],
        },
      ];
      return successResponseWrap(menuList);
    });
  },
});
