import Vue from 'vue'
import Router from 'vue-router'
import {
  ScheduleType,
  Access
} from '@/constant'
import Layout from '@/layout'
import Solo from '@/layout/Solo'

Vue.use(Router)

/**
 * sub-menu only appear when route children.length >= 1
 * dev: true,                      if set true, item will not show in production
 * hidden: true,                   if set true, item will not show in the sidebar(default is false)
 * name:'router-name' ,            if it is not a wrapping layer(Layout or Solo), it must be set!!!
 * include: ['admin'],             control the page roles (you can set multiple roles)
 * access: 'manage-profile',       control the page access
 * meta : {
 *   title: 'title',               the name show in sidebar and breadcrumb (recommend set)
 *   icon: 'svg-name'/'el-icon-x', the icon show in the sidebar
 *   cache: 'router-name'          if set, the router will be cached (only in Solo)
 * }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    hidden: true,
    path: '/error',
    component: () => import('@/views/platform/error-page/index')
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  {
    path: '/',
    redirect: '/dashboard',
    component: Layout,
    children: [
      {
        name: 'dashboard',
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'home' }
      },
      {
        hidden: true,
        name: 'profile',
        path: 'profile',
        component: () => import('@/views/platform/profile/index'),
        meta: { title: '个人设置' }
      }
    ]
  },
  {
    path: '/cm',
    component: Layout,
    meta: { title: '智能信发', icon: 'cm' },
    children: [
      {
        name: 'media',
        path: 'media',
        component: () => import('@/views/platform/media/index'),
        access: [Access.VIEW_CALENDAR, Access.MANAGE_CALENDAR, Access.MANAGE_GROUP],
        meta: { title: '媒资管理' }
      },
      {
        name: 'program-list',
        path: 'program',
        component: () => import('@/views/bigscreen/index'),
        access: [Access.VIEW_CALENDAR, Access.MANAGE_CALENDAR, Access.MANAGE_GROUP],
        meta: { title: '节目管理' }
      },
      {
        path: 'recur',
        component: Solo,
        meta: { title: '轮播管理' },
        children: [
          {
            name: 'recur-list',
            path: '',
            component: () => import('@/views/schedule/index'),
            access: [Access.VIEW_CALENDAR, Access.MANAGE_CALENDAR, Access.MANAGE_GROUP],
            meta: { cache: 'ScheduleList' },
            props: { type: ScheduleType.RECUR, redirect: 'recur-design' }
          },
          {
            hidden: true,
            name: 'recur-design',
            path: ':id',
            component: () => import('@/views/schedule/designer/index'),
            access: Access.MANAGE_CALENDAR,
            meta: { title: '编辑', cache: 'ScheduleList' },
            props: { redirect: 'recur-list' }
          }
        ]
      },
      {
        path: 'schedule',
        component: Solo,
        meta: { title: '排期管理' },
        children: [
          {
            name: 'schedule-list',
            path: '',
            component: () => import('@/views/schedule/index'),
            access: [Access.VIEW_CALENDAR, Access.MANAGE_CALENDAR, Access.MANAGE_GROUP],
            meta: { cache: 'ScheduleList' },
            props: {
              type: ScheduleType.COMPLEX,
              redirect: 'schedule-design'
            }
          },
          {
            hidden: true,
            name: 'schedule-design',
            path: ':id',
            component: () => import('@/views/schedule/designer/index'),
            access: Access.MANAGE_CALENDAR,
            meta: {
              title: '编辑',
              cache: 'ScheduleList'
            },
            props: {
              redirect: 'schedule-list'
            }
          }
        ]
      },
      {
        name: 'schedule-deploy',
        path: 'deploy',
        component: () => import('@/views/schedule/deploy/index'),
        access: Access.MANAGE_CALENDAR,
        meta: { title: '排期发布' }
      },
      {
        name: 'review',
        path: 'review',
        component: () => import('@/views/review/index'),
        access: Access.MANAGE_GROUP,
        meta: { title: '审核管理' }
      },
      {
        name: 'schedule-deploy-history',
        path: 'history',
        component: () => import('@/views/schedule/history/index'),
        access: [Access.MANAGE_CALENDAR, Access.MANAGE_GROUP],
        meta: { title: '发布历史' }
      }
    ]
  },
  {
    path: '/dm',
    component: Layout,
    meta: { title: '大屏设备', icon: 'dm' },
    children: [
      {
        name: 'device-timeline',
        path: 'timeline',
        component: () => import('@/views/device/timeline/index'),
        meta: { title: '排期预览' }
      },
      {
        path: 'device',
        component: Solo,
        meta: { title: '设备管理' },
        children: [
          {
            name: 'device-list',
            path: '',
            component: () => import('@/views/device/index'),
            meta: { cache: 'DeviceList' }
          },
          {
            hidden: true,
            name: 'device-detail',
            path: ':id',
            component: () => import('@/views/device/detail/index'),
            meta: { title: '设备详情', cache: 'DeviceList' }
          }
        ]
      },
      {
        name: 'group',
        path: 'group',
        component: () => import('@/views/device/group/index'),
        meta: { title: '分组管理' }
      },
      {
        name: 'back',
        path: 'back',
        component: () => import('@/views/device/back/index'),
        meta: { title: '视频回传' }
      }
    ]
  },
  {
    path: '/em',
    component: Layout,
    meta: { title: '设备录入', icon: 'em' },
    children: [
      {
        path: 'transmitter',
        name: 'transmitter',
        access: Access.MANAGE_TENANTS,
        component: () => import('@/views/external/transmitter/index'),
        meta: { title: '发送控制设备' }
      },
      {
        path: 'camera',
        name: 'camera',
        access: Access.VIEW_CAMERAS,
        component: () => import('@/views/external/camera/index'),
        meta: { title: '摄像头' }
      }
    ]
  },
  {
    path: '/pm',
    component: Layout,
    meta: { title: '平台管理', icon: 'pm' },
    children: [
      {
        name: 'org',
        path: 'org',
        component: () => import('@/views/realm/tenant/index'),
        access: [Access.MANAGE_TENANTS, Access.MANAGE_TENANT],
        meta: { title: '组织管理' }
      },
      {
        name: 'user',
        path: 'user',
        component: () => import('@/views/realm/user/index'),
        access: [Access.MANAGE_TENANTS, Access.MANAGE_TENANT],
        meta: { title: '用户管理' }
      },
      {
        name: 'device',
        path: 'device',
        component: () => import('@/views/realm/device/index'),
        access: [Access.MANAGE_TENANTS, Access.MANAGE_TENANT],
        meta: { title: '设备管理' }
      },
      {
        name: 'device-assign',
        path: 'assign',
        component: () => import('@/views/realm/assign/index'),
        access: [Access.MANAGE_TENANTS, Access.MANAGE_TENANT],
        meta: { title: '设备分配' }
      },
      {
        name: 'stock',
        path: 'stock',
        component: () => import('@/views/realm/ai-stock/index'),
        access: Access.MANAGE_TENANTS,
        meta: { title: 'AI审核库存' }
      }
    ]
  },
  {
    path: '/l',
    component: Layout,
    access: Access.VIEW_LOGS,
    meta: { icon: 'logger' },
    children: [
      {
        path: '',
        name: 'logger',
        component: () => import('@/views/platform/logger/index'),
        meta: { title: '操作日志' }
      }
    ]
  },
  {
    path: '/d',
    component: Layout,
    access: Access.MANAGE_TENANTS,
    meta: { icon: 'bug' },
    children: [
      {
        path: '',
        name: 'debug',
        component: () => import('@/views/realm/debug/index'),
        meta: { title: '调试' }
      }
    ]
  },
  {
    path: '/u',
    component: Layout,
    access: Access.MANAGE_TENANTS,
    meta: { title: '升级管理', icon: 'upgrade' },
    children: [
      {
        path: 'apk',
        name: 'upgrade-apk',
        component: () => import('@/views/realm/upgrade/index'),
        meta: { title: '版本管理' }
      },
      {
        path: 'deploy',
        name: 'upgrade-deploy',
        component: () => import('@/views/realm/upgrade/deploy/index'),
        meta: { title: '发布升级' }
      }
    ]
  },
  {
    hidden: true,
    name: 'program',
    path: '/cm/program/:id',
    component: () => import('@/views/bigscreen/ast/index'),
    props: true
  },
  {
    hidden: true,
    name: 'gateway',
    path: '/cm/gateway/:id',
    component: () => import('@/views/device/detail/components/Gateway/index'),
    props: true
  },
  // 404 page must be placed at the end !!!
  { hidden: true, path: '*', redirect: '/' }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => { return { y: 0 } },
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter () {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

export default router
