import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { FeedsComponent } from '../feeds/feeds.component';
import { NotFoundComponent } from './components';
import { ApiConsumerTokenResolve, ApiUserTokenResolve, LanguageResolve, DelayResolve, TestParameterResolve } from './shared';

import { CreateKnokComponent } from '../createknok';
import { CreateKnokComponent as _CreateKnokComponent } from '../todo-createknok';
import { DiscoverComponent } from '../discover';
import { FollowsComponent } from '../follows';
import { TrackComponent } from '../track';
import { EndedComponent } from '../ended';
import { KnkKnokOutletComponent } from './components/knk-knok-outlet';
import { KnkOngoingContentComponent } from './components/knk-ongoing-content';
import { KnkDiscoverContentComponent } from './components/knk-discover-content';
import { KnkPublishedContentComponent } from './components/knk-published-content/knk-published-content.component';
import { KnkPublishedKnokCurrentlyComponent } from './components/knk-published-knok-currently';
import { KnkPublishedKnokPastComponent } from './components/knk-published-knok-past';

const publishedRoutes: Routes = [
  {
    path: '',
    pathMatch: 'full',
    redirectTo: 'currently'
  },
  {
    path: '',
    children: [
      {
        path: 'currently',
        children: [
          {
            path: '',
            component: KnkPublishedKnokCurrentlyComponent,
            resolve: {
              token: ApiConsumerTokenResolve
            }
          },
          {
            path: ':id',
            component: KnkKnokOutletComponent,
            resolve: {
              token: ApiConsumerTokenResolve
            },
            outlet: 'knok'
          }
        ]
      },
      {
        path: 'past',
        children: [
          {
            path: '',
            component: KnkPublishedKnokPastComponent,
            resolve: {
              token: ApiConsumerTokenResolve
            }
          },
          {
            path: ':id',
            component: KnkKnokOutletComponent,
            resolve: {
              token: ApiConsumerTokenResolve
            },
            outlet: 'knok'
          }
        ]
      }
    ]
  }
];

const trackRoutes: Routes = [
  {
    path: '',
    pathMatch: 'full',
    redirectTo: 'knoks'
  },
  {
    path: '',
    children: [
      {
        path: 'knoks',
        children: [
          {
            path: '',
            component: TrackComponent,
            resolve: {
              token: ApiConsumerTokenResolve
            }
          },
          {
            path: ':id',
            component: KnkKnokOutletComponent,
            resolve: {
              token: ApiConsumerTokenResolve
            },
            outlet: 'knok'
          }
        ]
      },
      {
        path: 'ended',
        children: [
          {
            path: '',
            component: EndedComponent,
            resolve: {
              token: ApiConsumerTokenResolve
            }
          },
          {
            path: ':id',
            component: KnkKnokOutletComponent,
            resolve: {
              token: ApiConsumerTokenResolve
            },
            outlet: 'knok'
          }
        ]
      }
    ]
  }
];

const discoverRoutes: Routes = [
  {
    path: '',
    component: DiscoverComponent,
    resolve: {
      token: ApiConsumerTokenResolve
    }
  },
  {
    path: ':id',
    component: KnkKnokOutletComponent,
    resolve: {
      token: ApiConsumerTokenResolve
    },
    outlet: 'knok'
  }
];

export const coreRoutes: Routes = [
  {
    path: ':lang',
    resolve: {
      lang: LanguageResolve
    },
    children: [
      {
        path: '',
        redirectTo: 'discover',
        pathMatch: 'full'
      },
      {
        path: 'signup', loadChildren: '../registration/registration.module#RegistrationModule'
      },
      {
        path: 'login', loadChildren: '../login/login.module#LoginModule'
      },
      {
        path: 'resetpassword', loadChildren: '../resetpassword/resetpassword.module#ResetPasswordModule'
      },
      {
        path: '404',
        component: NotFoundComponent
      },
      {
        path: 'myaccount', loadChildren: '../myaccount/myaccount.module#MyAccountModule'
      },
      {
        path: 'feeds',
        component: FeedsComponent,
        resolve: {
          token: ApiConsumerTokenResolve
        }
      },
      {
        path: 'discover',
        component: KnkDiscoverContentComponent,
        children: discoverRoutes
      },
      {
        path: 'ongoing',
        data: {userIsKnokser: false}, // todo: remove
        component: KnkOngoingContentComponent,
        children: trackRoutes
      },
      {
        path: 'published',
        data: {userIsKnokser: true}, // todo: remove
        component: KnkPublishedContentComponent,
        children: publishedRoutes
      },
      {
        path: 'follows',
        component: FollowsComponent,
        resolve: {
          token: ApiConsumerTokenResolve
        }
      },
      {
        path: 'createknok',
        component: CreateKnokComponent,
        resolve: {
          token: ApiUserTokenResolve
        }
      },
      {
        path: 'todo-createknok',
        component: _CreateKnokComponent,
        resolve: {
          token: ApiUserTokenResolve
        }
      },
      {
        path: '**',
        component: NotFoundComponent
      }
    ]
  }
];

@NgModule({
  imports: [RouterModule.forChild(coreRoutes)],
  exports: [RouterModule],
  providers: [LanguageResolve, ApiConsumerTokenResolve]
})
export class CoreRoutingModule {
}
