import type { AppRouteRecordRaw, Menu } from '/@/router/types';
import { defineStore } from 'pinia';
import { store } from '/@/store';
import { useI18n } from '/@/hooks/web/useI18n';
import { useUserStore } from './user';
import { useAppStoreWithOut } from './app';
import { toRaw } from 'vue';
import { transformObjToRoute, flatMultiLevelRoutes } from '/@/router/helper/routeHelper';
import { transformRouteToMenu } from '/@/router/helper/menuHelper';
import projectSetting from '/@/settings/projectSetting';
import { PermissionModeEnum } from '/@/enums/appEnum';
import { asyncRoutes } from '/@/router/routes';
import { ERROR_LOG_ROUTE, PAGE_NOT_FOUND_ROUTE } from '/@/router/routes/basic';
import { filter } from '/@/utils/helper/treeHelper';
import { getMenuList, getMenusIdsByRoleId } from '/@/api/sys/menu';
import { getPermCode } from '/@/api/sys/user';
import { useMessage } from '/@/hooks/web/useMessage';
import { PageEnum } from '/@/enums/pageEnum';
import { USER_INFO_KEY } from '/@/enums/cacheEnum';
import { getAuthCache, setAuthCache } from '/@/utils/auth';
import { createStorage } from '/@/utils/cache/index';
import { session } from '/@/utils/Memory';

interface PermissionState {
  // Permission code list
  permCodeList: string[] | number[];
  // Whether the route has been dynamically added
  isDynamicAddedRoute: boolean;
  // To trigger a menu update
  lastBuildMenuTime: number;
  // Backstage menu list
  backMenuList: Menu[];
  frontMenuList: Menu[];
}
export const usePermissionStore = defineStore({
  id: 'app-permission',
  state: (): PermissionState => ({
    permCodeList: [],
    // Whether the route has been dynamically added
    isDynamicAddedRoute: false,
    // To trigger a menu update
    lastBuildMenuTime: 0,
    // Backstage menu list
    backMenuList: [],
    // menu List
    frontMenuList: [],
  }),
  getters: {
    getPermCodeList(): string[] | number[] {
      return this.permCodeList;
    },
    getBackMenuList(): Menu[] {
      const name = session.getItem('currentSystemName');
      if (name && this.backMenuList.length) {
        this.backMenuList.map((i) => {
          if (i.name == name) return i.children;
        });
      }
      return this.backMenuList;
    },
    getFrontMenuList(): Menu[] {
      return this.frontMenuList;
    },
    getLastBuildMenuTime(): number {
      return this.lastBuildMenuTime;
    },
    getIsDynamicAddedRoute(): boolean {
      return this.isDynamicAddedRoute;
    },
  },
  actions: {
    setPermCodeList(codeList: string[]) {
      this.permCodeList = codeList;
    },

    setBackMenuList(list: Menu[], type: boolean): void {
      // if (type && list.length) session.setItem('allMenuList', list);
      this.backMenuList = list;
      list?.length > 0 && this.setLastBuildMenuTime();
    },

    setFrontMenuList(list: Menu[]) {
      this.frontMenuList = list;
    },

    setLastBuildMenuTime() {
      this.lastBuildMenuTime = new Date().getTime();
    },

    setDynamicAddedRoute(added: boolean) {
      this.isDynamicAddedRoute = added;
    },
    resetState(): void {
      this.isDynamicAddedRoute = false;
      this.permCodeList = [];
      this.backMenuList = [];
      this.lastBuildMenuTime = 0;
    },
    async changePermissionCode() {
      //判空处理，如果没有数据则为空，空数组.filter是不会报错的
      const filterMenu = (allMenuList: Recordable[] = [], menuIdsList) => {
        if (!allMenuList) {
          allMenuList = [];
        }
        return allMenuList
          .filter((item) => {
            return menuIdsList.indexOf(item.id) > -1;
          })
          .map((subItem) => {
            subItem = Object.assign({}, subItem);
            if (subItem.children) {
              subItem.children = filterMenu(subItem.children, menuIdsList);
            }
            return subItem;
          });
      };
      const userInfo: any = getAuthCache(USER_INFO_KEY);
      const isSysAdmin = 'SYS_ADMIN';
      const routeList = (await getMenuList(2)) as AppRouteRecordRaw[];
      let getSysPermission = [];
      /**
       * 否则不是超级管理员-获取对应角色的权限列表
       */
      const codeList = await getPermCode();
      // console.log('codeList', codeList);
      this.setPermCodeList(codeList);
      /**
       * 如果是超级管理员则获取对应权限列表
       */
      if (userInfo.roles.includes(isSysAdmin) || userInfo.realName == '超级管理员') {
        const getMenuIds = await getMenusIdsByRoleId(userInfo.plainRoles[0].roleId);
        //根据对应的使用者的菜单数组和所有菜单数组对象进行过滤，返回最终需要的菜单
        const newMenu = filterMenu(routeList, getMenuIds);
        /**
         * 递归获取对应所有菜单的权限列表。
         */
        function lookForAllId(data = [], arr = []) {
          for (const item of data) {
            arr.push(item.permission);
            if (item.children && item.children.length) lookForAllId(item.children, arr);
          }
          return arr;
        }
        getSysPermission = lookForAllId(newMenu);
        this.setPermCodeList(getSysPermission);
      }
    },
    async buildRoutesAction(): Promise<AppRouteRecordRaw[]> {
      const { t } = useI18n();
      const userStore = useUserStore();
      const appStore = useAppStoreWithOut();

      let routes: AppRouteRecordRaw[] = [];
      const roleList = toRaw(userStore.getRoleList) || [];
      const { permissionMode = projectSetting.permissionMode } = appStore.getProjectConfig;

      const routeFilter = (route: AppRouteRecordRaw) => {
        const { meta } = route;
        const { roles } = meta || {};
        if (!roles) return true;
        return roleList.some((role) => roles.includes(role));
      };

      const routeRemoveIgnoreFilter = (route: AppRouteRecordRaw) => {
        const { meta } = route;
        const { ignoreRoute } = meta || {};
        return !ignoreRoute;
      };

      /**
       * @description 根据设置的首页path，修正routes中的affix标记（固定首页）
       * */
      const patchHomeAffix = (routes: AppRouteRecordRaw[]) => {
        if (!routes || routes.length === 0) return;
        let homePath: string = userStore.getUserInfo.homePath || PageEnum.BASE_HOME;
        function patcher(routes: AppRouteRecordRaw[], parentPath = '') {
          if (parentPath) parentPath = parentPath + '/';
          try {
            routes?.forEach((route: AppRouteRecordRaw) => {
              const { path, children, redirect } = route;
              const currentPath = path.startsWith('/') ? path : parentPath + path;
              if (currentPath === homePath) {
                if (redirect) {
                  homePath = route.redirect! as string;
                } else {
                  route.meta = Object.assign({}, route.meta, { affix: true });
                  throw new Error('end');
                }
              }
              children && children.length > 0 && patcher(children, currentPath);
            });
          } catch (e) {
            return e;
          }
        }
        try {
          patcher(routes);
        } catch (e) {
          // 已处理完毕跳出循环
        }
        return;
      };

      switch (permissionMode) {
        case PermissionModeEnum.ROLE:
          routes = filter(asyncRoutes, routeFilter);
          routes = routes.filter(routeFilter);
          // Convert multi-level routing to level 2 routing
          routes = flatMultiLevelRoutes(routes);
          break;

        case PermissionModeEnum.ROUTE_MAPPING:
          routes = filter(asyncRoutes, routeFilter);
          routes = routes.filter(routeFilter);
          const menuList = transformRouteToMenu(routes, true);
          routes = filter(routes, routeRemoveIgnoreFilter);
          routes = routes.filter(routeRemoveIgnoreFilter);
          menuList.sort((a, b) => {
            return (a.meta?.orderNo || 0) - (b.meta?.orderNo || 0);
          });

          this.setFrontMenuList(menuList);
          // Convert multi-level routing to level 2 routing
          routes = flatMultiLevelRoutes(routes);
          break;

        //  如果你确定不需要进行后台动态权限校验，你可以将以下判断语句注释
        case PermissionModeEnum.BACK:
          const { createMessage } = useMessage();
          createMessage.loading({
            content: t('sys.app.menuLoading'),
            duration: 0.5,
          });

          // ! 模拟从后台获取权限编码的方法，
          // ! 这个函数可能只需要在适当的时候执行一次，实际项目中会根据需要自行放置
          let routeList: AppRouteRecordRaw[] = [];
          const userInfo: any = getAuthCache(USER_INFO_KEY);
          const filterMenu = (allMenuList, menuIdsList) => {
            if (!allMenuList) allMenuList = [];
            return allMenuList
              .filter((item) => {
                return menuIdsList.indexOf(item.id) > -1;
              })
              .map((subItem, index) => {
                subItem = Object.assign({}, subItem);
                if (subItem.children) {
                  subItem.children = filterMenu(subItem.children, menuIdsList);
                }
                return subItem;
              });
          };

          if (userInfo?.needSetPwd == true) {
            routeList = [
              {
                name: 'routes.common.system.system',
                parentId: '',
                subsystem: true,
                children: [
                  {
                    id: 'a8ffa8c5-637e-476b-a9e6-b60cebe95718',
                    createTime: '2021-09-10 20:50:55',
                    updateTime: '2021-11-16 18:58:24',
                    name: 'routes.common.system.modifyPassword',
                    parentId: 'a8ffa8c5-637e-471b-a9e6-b60cebe95713',
                    children: [],
                    path: '/system/changePassword',
                    type: 'SYSADMIN',
                    permission: 'system:password:view',
                    sort: 6,
                    component: '/system/changePassword/index',
                    meta: {
                      icon: 'bx:bx-home',
                      title: 'routes.common.system.modifyPassword',
                      menuType: '1',
                      ignoreKeepAlive: true,
                      hideMenu: false,
                      status: '0',
                    },
                    redirect: '',
                  },
                ],
                path: '/system',
                type: 'SYSADMIN',
                permission: '',
                sort: 6,
                component: 'LAYOUT',
                meta: {
                  icon: 'bx:bx-home',
                  title: 'routes.common.system.system',
                  status: '0',
                  menuType: '0',
                },
                redirect: '/system/systemManagement',
              },
            ] as AppRouteRecordRaw[];
          } else {
            this.changePermissionCode();
            routeList = (await getMenuList(1)) as AppRouteRecordRaw[];
            console.log('routeList', routeList);
            const isSysAdmin = 'SYS_ADMIN';
            /**
             * 解决超级管理员分配菜单权限问题
             */
            if (userInfo.roles.includes(isSysAdmin) || userInfo.realName == '超级管理员') {
              const getMenuIds = await getMenusIdsByRoleId(userInfo.plainRoles[0].roleId);
              //根据对应的使用者的菜单数组和所有菜单数组对象进行过滤，返回最终需要的菜单
              const newMenu = filterMenu(routeList, getMenuIds);
              routeList = newMenu;
            }
            console.log(routeList);
            createStorage('MENU_LIST', JSON.stringify(routeList));
            setAuthCache('MENU_LIST', routeList);
          }
          // 动态导入组件
          routeList = transformObjToRoute(routeList);
          console.log('动态导入组件:', routeList);
          // 将后台返回的路由信息转换成菜单结构需要我们进行路由信息的解析和组装
          const backMenuList = transformRouteToMenu(routeList);
          console.log('路由转菜单：', backMenuList);
          this.setBackMenuList(backMenuList, true);
          // remove meta.ignoreRoute item
          routeList = filter(routeList, routeRemoveIgnoreFilter);
          routeList = routeList.filter(routeRemoveIgnoreFilter);
          routeList = flatMultiLevelRoutes(routeList);
          routes = [PAGE_NOT_FOUND_ROUTE, ...routeList];
          break;
      }
      routes.push(ERROR_LOG_ROUTE);
      patchHomeAffix(routes);
      return routes;
    },
  },
});

// Need to be used outside the setup
export function usePermissionStoreWithOut() {
  return usePermissionStore(store);
}
