import { NgModule } from '@angular/core';
import { ExtraOptions, RouterModule, Routes } from '@angular/router';
import { Feature } from '@msfa-enums/feature.enum';
import { RoleEnum } from '@msfa-enums/role.enum';
import { environment } from '@msfa-environment';
import { AuthGuard } from '@msfa-guards/auth.guard';
import { OrganizationGuard } from '@msfa-guards/organization.guard';
import { RoleGuard } from '@msfa-guards/role.guard';

const activeFeatures: Feature[] = environment.activeFeatures;

const routes: Routes = [
  {
    path: '',
    data: { title: '' },
    loadChildren: () => import('./pages/start/start.module').then(m => m.StartModule),
    canActivate: [AuthGuard, OrganizationGuard],
  },
  {
    path: 'logga-ut',
    data: { title: 'Logga ut' },
    loadChildren: () => import('./pages/logout/logout.module').then(m => m.LogoutModule),
    canActivate: [AuthGuard],
  },
  {
    path: 'organization-picker',
    data: { title: 'Välj organisation' },
    loadChildren: () =>
      import('./pages/organization-picker/organization-picker.module').then(m => m.OrganizationPickerModule),
    canActivate: [AuthGuard],
  },
  {
    path: 'obehorig',
    data: { title: 'Saknar behörighet' },
    loadChildren: () => import('./pages/unauthorized/unauthorized.module').then(m => m.UnauthorizedModule),
    canActivate: [AuthGuard],
  },
];

activeFeatures.forEach(feature => {
  switch (feature) {
    case Feature.ADMINISTRATION:
      routes.push({
        path: 'administration',
        data: { title: 'Administration', expectedRole: RoleEnum.MSFA_AuthAdmin },
        loadChildren: () => import('./pages/administration/administration.module').then(m => m.AdministrationModule),
        canActivate: [AuthGuard, OrganizationGuard, RoleGuard],
      });
      break;
    case Feature.AVROP:
      routes.push({
        path: 'nya-deltagare',
        data: { title: 'Nya deltagare', expectedRole: RoleEnum.MSFA_ReceiveDeltagare },
        loadChildren: () => import('./pages/avrop/avrop.module').then(m => m.AvropModule),
        canActivate: [AuthGuard, OrganizationGuard, RoleGuard],
      });
      break;
    case Feature.DELTAGARE:
      routes.push({
        path: 'deltagare',
        data: { title: 'Deltagare', expectedRole: RoleEnum.MSFA_ReportAndPlanning },
        loadChildren: () => import('./pages/deltagare/deltagare.module').then(m => m.DeltagareModule),
        canActivate: [AuthGuard, OrganizationGuard, RoleGuard],
      });
      break;
    case Feature.MY_ACCOUNT:
      routes.push({
        path: 'mitt-konto',
        data: { title: 'Mitt konto' },
        loadChildren: () => import('./pages/my-account/my-account.module').then(m => m.MyAccountModule),
        canActivate: [AuthGuard, OrganizationGuard],
      });
      break;
    case Feature.RELEASES:
      routes.push({
        path: 'releases',
        data: { title: 'Releaser' },
        loadChildren: () => import('./pages/releases/releases.module').then(m => m.ReleasesModule),
        canActivate: [AuthGuard],
      });
      break;
    case Feature.MOCK_LOGIN:
      routes.push({
        path: 'mock-login',
        data: { title: 'Mock login' },
        loadChildren: () => import('./pages/mock-login/mock-login.module').then(m => m.MockLoginModule),
      });
      break;
    case Feature.ACCESSIBILITY_REPORT:
      routes.push({
        path: 'tillganglighet',
        data: { title: 'Tillgänglighetsredogörelse' },
        loadChildren: () => import('./pages/accessibility/accessibility.module').then(m => m.AccessibilityModule),
        canActivate: [AuthGuard],
      });
      break;
    default:
      break;
  }
});

routes.push({
  path: '**',
  data: { title: 'Sidan hittas inte' },
  loadChildren: () => import('./pages/page-not-found/page-not-found.module').then(m => m.PageNotFoundModule),
  canActivate: [AuthGuard],
});

const options: ExtraOptions = {
  useHash: false,
  scrollPositionRestoration: 'enabled',
  onSameUrlNavigation: 'reload',
};

@NgModule({
  imports: [RouterModule.forRoot(routes, options)],
  exports: [RouterModule],
})
export class AppRoutingModule {}
