import Vue from 'vue'
import Router from 'vue-router'
import Startpage from '../views/Startpage'
import Login from '../views/Login'
import Dashboard from '../views/Dashboard'
import store from '../store/store';
import OAuthPermission from '../views/OAuthPermission'
import ListApps from '../views/ListApps'
import AppDetails from '../views/AppDetails'
import NewApp from '../views/NewApp'
import Mail from '../views/Mail'
import Invite from '../views/Invite'
import Register from '../views/Register'
import Access from '../views/Access'
import Profile from '../views/Profile'

import AdminSettings from '../views/admin/Settings'
import AdminUserList from '../views/admin/User'
import AdminUserEdit from '../views/admin/UserEdit'
import AdminAppsList from '../views/admin/App'
import AdminAppDetails from '../views/admin/AppDetail'
import AdminInvite from '../views/admin/Invite'
import AdminInviteNew from '../views/admin/InviteNew'

Vue.use(Router)

function guard(to, from, next){
    if(store.state.loggedIn) {
        next(); // allow to enter route
    } else{
        store.state.redirectIfLoggedIn = to;
        next('/login'); // go to '/login';
    }
}

function admin(to, from, next){
    if(store.state.loggedIn) {
        next(); // allow to enter route
    } else{
        store.state.redirectIfLoggedIn = to;
        next('/login'); // go to '/login';
    }
}

export default new Router({
    routes: [
        {
            path: '/',
            name: 'Startpage',
            component: Startpage
        },
        {
            path: '/login',
            name: 'Login',
            component: Login
        },
        {
            path: '/invited',
            name: 'Invite',
            component: Invite
        },
        {
            path: '/register',
            name: 'Register',
            component: Register
        },
        {
            path: '/oauth',
            name: 'oAuthPermission',
            component: OAuthPermission
        },
        {
            beforeEnter: guard,
            path: '/dashboard',
            name: 'Dashboard',
            component: Dashboard
        },
        {
            beforeEnter: guard,
            path: '/apps',
            name: 'Apps',
            component: ListApps
        },
        {
            beforeEnter: guard,
            path: '/access',
            name: 'Access',
            component: Access
        },
        {
            beforeEnter: guard,
            path: '/profile',
            name: 'Profile',
            component: Profile
        },
        {
            beforeEnter: guard,
            path: '/apps/new',
            name: 'newApp',
            component: NewApp
        },
        {
            beforeEnter: guard,
            path: '/apps/:id',
            name: 'appDetails',
            component: AppDetails
        },
        {
            beforeEnter: guard,
            path: '/user/mails',
            name: 'userMails',
            component: Mail
        },
        {
            beforeEnter: admin,
            path: '/admin/settings',
            name: 'adminSettings',
            component: AdminSettings
        },
        {
            beforeEnter: admin,
            path: '/admin/user',
            name: 'adminUserList',
            component: AdminUserList
        },
        {
            beforeEnter: admin,
            path: '/admin/user/:id',
            name: 'adminUserEdit',
            component: AdminUserEdit
        },
        {
            beforeEnter: admin,
            path: '/admin/app',
            name: 'adminListApp',
            component: AdminAppsList
        },
        {
            beforeEnter: admin,
            path: '/admin/app/:id',
            name: 'adminAppDetail',
            component: AdminAppDetails
        },
        {
            beforeEnter: admin,
            path: '/admin/invite',
            name: 'adminInvite',
            component: AdminInvite
        },
        {
            beforeEnter: admin,
            path: '/admin/invite/new',
            name: 'adminInviteNew',
            component: AdminInviteNew
        },


    ]
})
