import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const pcRouter = new Router({
  routes: [
    {
      path: '/',
      alias: [
        '/user/:id',
        '/login',
        '/other/:id',
        '/me',
        '/editMe',
        '/editInfo/:type',
        '/groupSet/:id',
        '/invite/:inviteType',
        '/packetSend',
        '/packetGet/:id',
        '/search'
      ],
      name: 'noChat',
      component: resolve => {
        require(['@/components/chatRoom/noChat.vue'], resolve)
      }
    },
    {
      path: '/group/:id',
      name: 'groupChat',
      component: resolve => {
        require(['@/components/chatRoom/chatRoom.vue'], resolve)
      }
    },
    {
      path: '/pm/:id',
      name: 'pmChat',
      component: resolve => {
        require(['@/components/chatRoom/chatRoom.vue'], resolve)
      }
    },
    {
      path: '/hotGroup',
      name: 'hotGroup',
      component: resolve => {
        require(['@/components/hotGroup/hotGroup.vue'], resolve)
      }
    },
    {
      path: '/relateGroup',
      name: 'relateGroup',
      component: resolve => {
        require(['@/components/relateGroup/relateGroup.vue'], resolve)
      }
    }
  ]
})

const h5Router = new Router({
  routes: [
    {
      path: '/',
      alias: '/user/:id',
      name: 'chatList',
      component: resolve => {
        require(['@/pages/h5/view/chatList.vue'], resolve)
      }
    },
    {
      path: '/login',
      name: 'login',
      component: resolve => {
        require(['@/pages/h5/view/login.vue'], resolve)
      }
    },
    {
      path: '/loginMee',
      name: 'loginMee',
      component: resolve => {
        require(['@/pages/h5/view/loginMee.vue'], resolve)
      }
    },
    {
      path: '/other/:id',
      name: 'aboutOther',
      component: resolve => {
        require(['@/pages/h5/view/aboutOther.vue'], resolve)
      }
    },
    {
      path: '/me',
      name: 'aboutMe',
      component: resolve => {
        require(['@/pages/h5/view/aboutMe.vue'], resolve)
      }
    },
    {
      path: '/editMe',
      name: 'editMe',
      component: resolve => {
        require(['@/pages/h5/view/editMe.vue'], resolve)
      }
    },
    {
      path: '/editInfo/:type/:id?',
      name: 'editInfo',
      component: resolve => {
        require(['@/pages/h5/view/editInfo.vue'], resolve)
      }
    },
    {
      path: '/group/:id',
      name: 'groupChat',
      component: resolve => {
        require(['@/pages/h5/view/chatRoom.vue'], resolve)
      }
    },
    {
      path: '/groupSet/:id',
      name: 'groupSet',
      component: resolve => {
        require(['@/pages/h5/view/chatSet.vue'], resolve)
      }
    },
    {
      path: '/pm/:id',
      name: 'pmChat',
      component: resolve => {
        require(['@/pages/h5/view/chatRoom.vue'], resolve)
      }
    },
    {
      path: '/invite/:inviteType/:id?',
      name: 'invite',
      component: resolve => {
        require(['@/pages/h5/view/invite.vue'], resolve)
      }
    },
    {
      path: '/search',
      name: 'search',
      component: resolve => {
        require(['@/pages/h5/view/search.vue'], resolve)
      }
    },
    {
      path: '/password',
      name: 'password',
      component: resolve => {
        require(['@/pages/h5/view/password.vue'], resolve)
      }
    },
    {
      path: '/relateGroup',
      name: 'relateGroup',
      component: resolve => {
        require(['@/components/relateGroup/relateGroup.vue'], resolve)
      }
    }
  ]
})

h5Router.beforeEach((to, from, next) => {
  if (to.path !== '/login' && to.path !== '/loginMee') {
    let userId = localStorage.getItem('user_id')
    let token = localStorage.getItem('token')

    if (userId && token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export { h5Router, pcRouter }
