import { createRouter, createWebHistory } from 'vue-router';

import HvacLayout from '@/layout/HvacLayout.vue';
import { useUserInfoStore } from '@/stores/user-info';
import { getPermission, getToken } from '@/utils/auth';
import { ViewPermission } from '@/utils/permission-type';
import type { IconfontIcon } from '@/icons/fonts/iconfont';

import type { RouteRecordRaw } from 'vue-router';

declare module 'vue-router' {
  interface RouteMeta {
    title?: string;
    icon?: IconfontIcon;
    hideInMenu?: boolean; // 该页面是否在侧栏菜单中显示
    hideSubMenu?: boolean; // 该页面是否不显示在侧栏的子菜单，即作为一级菜单使用
    requiresAuth: boolean;
    permission?: number;
  }
}

/**
 * 数据中心相关路由
 */
const dataCenterRoutes: Readonly<RouteRecordRaw[]> = [
  {
    path: '/env-monitor',
    redirect: '/env-monitor/index',
    component: HvacLayout,
    meta: {
      title: 'envMonitor',
      icon: 'env-monitoring',
      permission: ViewPermission.环境监控,
      hideSubMenu: true,
      requiresAuth: true,
    },
    children: [
      {
        path: 'index',
        name: 'envMonitor',
        component: () => import('@/views/env-monitor/EnvMonitor.vue'),
        meta: {
          requiresAuth: true,
        },
      },
    ],
  },
  {
    path: '/energy-analysis',
    redirect: '/energy-analysis/index',
    component: HvacLayout,
    meta: {
      title: 'energyAnalysis',
      icon: 'energy-analysis',
      permission: ViewPermission.能耗分析,
      hideSubMenu: true,
      requiresAuth: true,
    },
    children: [
      {
        path: 'index',
        name: 'energyAnalysis',
        component: () => import('@/views/energy-analysis/EnergyAnalysis.vue'),
        meta: {
          requiresAuth: true,
        },
      },
    ],
  },
  {
    path: '/large-screen',
    redirect: '/large-screen/index',
    component: HvacLayout,
    meta: {
      title: 'largeScreen',
      icon: 'large-screen',
      permission: ViewPermission.大屏,
      hideSubMenu: true,
      requiresAuth: true,
    },
    children: [
      {
        path: 'index',
        name: 'largeScreen',
        component: () => import('@/views/large-screen/LargeScreen.vue'),
        meta: {
          requiresAuth: true,
        },
      },
    ],
  },
  {
    path: '/data-query',
    redirect: '/data-query/index',
    component: HvacLayout,
    meta: {
      title: 'dataQuery',
      icon: 'data-query',
      permission: ViewPermission.数据查询,
      hideSubMenu: true,
      requiresAuth: true,
    },
    children: [
      {
        path: 'index',
        name: 'dataQuery',
        component: () => import('@/views/data-query/DataQuery.vue'),
        meta: {
          requiresAuth: true,
        },
      },
    ],
  },
  {
    path: '/alarm-manage',
    redirect: '/alarm-manage/index',
    component: HvacLayout,
    meta: {
      title: 'alarmManage',
      icon: 'alarm-management',
      permission: ViewPermission.事件响应,
      hideSubMenu: true,
      requiresAuth: true,
    },
    children: [
      {
        path: 'index',
        name: 'alarmManage',
        component: () => import('@/views/alarm-manage/AlarmManage.vue'),
        meta: {
          requiresAuth: true,
        },
      },
    ],
  },
];

/**
 * 运维中心相关路由
 */
const opsCenterRoutes: Readonly<RouteRecordRaw[]> = [
  {
    path: '/user-permission',
    name: 'userPermission',
    redirect: '/user-permission/user-manage',
    component: HvacLayout,
    meta: {
      title: 'userManage',
      icon: 'user-permissions',
      permission: ViewPermission.用户管理,
      requiresAuth: true,
    },
    children: [
      {
        path: 'user-manage',
        name: 'userManage',
        component: () => import('@/views/user-manage/UserManage.vue'),
        meta: {
          title: 'userPermission',
          permission: ViewPermission.账号管理,
          requiresAuth: true,
        },
      },
      {
        path: 'role-manage',
        name: 'roleManage',
        component: () => import('@/views/role-manage/RoleManage.vue'),
        meta: {
          title: 'roleManage',
          permission: ViewPermission.角色管理,
          requiresAuth: true,
        },
      },
      {
        path: 'organization-manage',
        name: 'organizationManage',
        component: () => import('@/views/organization-manage/OrganizationManage.vue'),
        meta: {
          title: 'organizationManage',
          permission: ViewPermission.组织管理,
          requiresAuth: true,
        },
      },
    ],
  },
  {
    path: '/algorithm-manage',
    redirect: '/algorithm-manage/index',
    component: HvacLayout,
    meta: {
      title: 'algorithmManage',
      icon: 'algorithm-management',
      permission: ViewPermission.算法配置,
      hideSubMenu: true,
      requiresAuth: true,
    },
    children: [
      {
        path: 'index',
        name: 'algorithmManage',
        component: () => import('@/views/algorithm-manage/AlgorithmManage.vue'),
        meta: {
          requiresAuth: true,
        },
      },
    ],
  },
  {
    path: '/gateway-protocol',
    redirect: '/gateway-protocol/index',
    component: HvacLayout,
    meta: {
      title: 'protocolManage',
      icon: 'protocol-management',
      permission: ViewPermission.协议管理,
      hideSubMenu: true,
      requiresAuth: true,
    },
    children: [
      {
        path: 'index',
        name: 'gatewayProtocol',
        component: () => import('@/views/gateway-protocol/GatewayProtocol.vue'),
        meta: {
          requiresAuth: true,
        },
      },
    ],
  },

  // {
  //   path: '/protocol-manage',
  //   name: 'protocolManage',
  //   redirect: '/protocol-manage/keyword-library',
  //   component: HvacLayout,
  //   meta: {
  //     title: 'protocolManage',
  //     icon: 'protocol-management',
  //     requiresAuth: true,
  //   },
  //   children: [
  //     {
  //       path: 'gateway-protocol',
  //       name: 'gatewayProtocol',
  //       component: () => import('@/views/gateway-protocol/GatewayProtocol.vue'),
  //       meta: {
  //         title: 'gatewayProtocol',
  //         requiresAuth: true,
  //       },
  //     },
  //     {
  //       path: 'standard-protocol-library',
  //       name: 'standardProtocolLibrary',
  //       component: () => import('@/views/standard-protocol-library/StandardProtocolLibrary.vue'),
  //       meta: {
  //         title: 'standardProtocolLibrary',
  //         requiresAuth: true,
  //       },
  //     },
  //     {
  //       path: 'keyword-library',
  //       name: 'keywordLibrary',
  //       component: () => import('@/views/keyword-library/KeywordLibrary.vue'),
  //       meta: {
  //         title: 'keywordLibrary',
  //         requiresAuth: true,
  //       },
  //     },
  //   ],
  // },
  {
    path: '/device-manage',
    name: 'deviceManage',
    redirect: '/device-manage/device-list',
    component: HvacLayout,
    meta: {
      title: 'device',
      icon: 'device',
      permission: ViewPermission.设备,
      requiresAuth: true,
    },
    children: [
      {
        path: 'device-list',
        name: 'deviceList',
        component: () => import('@/views/device-list/DeviceList.vue'),
        meta: {
          title: 'deviceManage',
          permission: ViewPermission.设备管理,
          requiresAuth: true,
        },
        children: [
          {
            path: 'equipment-details/:id',
            name: 'equipmentDetails',
            component: () => import('@/views/equipment-details/EquipmentDetails.vue'),
            meta: {
              requiresAuth: true,
            },
          },
        ],
      },
      {
        path: 'gateway-list',
        name: 'gatewayList',
        component: () => import('@/views/gateway-list/GatewayList.vue'),
        meta: {
          title: 'gatewayManage',
          permission: ViewPermission.网关管理,
          requiresAuth: true,
        },
      },
    ],
  },
  {
    path: '/log-center',
    redirect: '/log-center/index',
    component: HvacLayout,
    meta: {
      title: 'logCenter',
      icon: 'calendar',
      permission: ViewPermission.日志中心,
      hideSubMenu: true,
      requiresAuth: true,
    },
    children: [
      {
        path: 'index',
        name: 'logCenter',
        component: () => import('@/views/log-center/LogCenter.vue'),
        meta: {
          requiresAuth: true,
        },
      },
    ],
  },
];

const routes: Readonly<RouteRecordRaw[]> = [
  {
    path: '/',
    redirect: '/index',
  },
  {
    path: '/index',
    name: 'index',
    component: () => import('@/views/HomePage.vue'),
    meta: {
      hideInMenu: true,
      requiresAuth: false,
    },
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login-component/LoginView.vue'),
    meta: {
      hideInMenu: true,
      requiresAuth: false,
    },
  },
  {
    path: '/home',
    redirect: '/home/index',
    component: HvacLayout,
    meta: {
      title: 'hvacHome',
      icon: 'setting',
      hideSubMenu: true,
      requiresAuth: true,
    },
    children: [
      {
        path: 'index',
        name: 'hvacHome',
        component: () => import('@/views/hvac-home/HvacHome.vue'),
        meta: {
          requiresAuth: true,
        },
      },
    ],
  },
  {
    path: '/first-usage',
    name: 'firstUsage',
    component: () => import('@/views/first-usage/FirstUsage.vue'),
    meta: {
      hideInMenu: true,
      requiresAuth: true,
    },
  },
  {
    path: '/create-customer',
    name: 'createCustomer',
    component: () => import('@/views/create-customer/CreateCustomer.vue'),
    meta: {
      hideInMenu: true,
      requiresAuth: true,
    },
  },
  {
    path: '/setup-protocol',
    name: 'setupProtocol',
    component: () => import('@/views/setup-protocol/SetupProtocol.vue'),
    meta: {
      hideInMenu: true,
      requiresAuth: true,
    },
  },
  {
    path: '/register-gateway',
    name: 'registerGateway',
    component: () => import('@/views/register-gateway/RegisterGateway.vue'),
    meta: {
      hideInMenu: true,
      requiresAuth: true,
    },
  },
  {
    path: '/create-device',
    name: 'createDevice',
    component: () => import('@/views/create-device/CreateDevice.vue'),
    meta: {
      hideInMenu: true,
      requiresAuth: true,
    },
  },
  {
    path: '/ai-smart-ctrl',
    name: 'aiSmartCtrl',
    redirect: '/ai-smart-ctrl/device-group',
    component: HvacLayout,
    meta: {
      title: 'aiSmartCtrl',
      requiresAuth: true,
    },
    children: [
      {
        path: 'device-group/:id',
        name: 'deviceGroup',
        component: () => import('@/views/device-group/DeviceGroup.vue'),
        meta: {
          title: 'deviceGroup',
          requiresAuth: true,
        },
      },
    ],
  },
  {
    path: '/exception',
    name: 'exception',
    component: () => import('@/views/NotAllow.vue'),
    meta: {
      hideInMenu: true,
      requiresAuth: false,
    },
  },
  ...dataCenterRoutes,
  ...opsCenterRoutes,
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth) {
    const token = getToken();
    const permission = getPermission();

    if (token) {
      const { saveToken, savePermission } = useUserInfoStore();
      saveToken(token);
      if (permission) {
        savePermission(permission.split(',').map(Number));
      }
      next();
    } else {
      next('/login');
    }
  } else {
    next();
  }
});

export { dataCenterRoutes, opsCenterRoutes, routes };

export default router;
