import { NgModule } from '@angular/core';
import { Route, RouterModule, UrlSegment } from '@angular/router';
import { CanActivateScheduleDetails } from './guards/can-activate-schedule-details.service';
import { CanActivateScheduleRouteSearch } from './guards/can-activate-schedule-route-search.service';
import { ScheduleFlightDetailsPageComponent } from './pages/flight-details/schedule-flight-details-page.component';
import { ScheduleSearchPageComponent } from './pages/search/schedule-search-page.component';
import { ScheduleStartPageComponent } from './pages/start/schedule-start-page.component';
import { ScheduleDetailsBreadCrumbsResolver } from './resolvers/bread-crumbs/details-bread-crumbs.resolver';
import { ScheduleSearchBreadCrumbsResolver } from './resolvers/bread-crumbs/search-bread-crumbs.resolver';
import { ScheduleDetailsUrlParamsResolver } from './resolvers/url-params/details-url-params.resolver';
import { ScheduleRequestDataResolver } from './resolvers/request-data.resolver';
import { ScheduleSearchPageUrlParamsResolver } from './resolvers/url-params/search-page-url-params.resolver';

export const FLIGHTS_INFO_PARAM_NAME = 'flightsInfo';

const routes: Route[] = [
    {
        path: 'route/:params/:returnParams',
        component: ScheduleSearchPageComponent,
        canActivate: [CanActivateScheduleRouteSearch],
        resolve: {
            urlParams: ScheduleSearchPageUrlParamsResolver,
            breadcrumbs: ScheduleSearchBreadCrumbsResolver,
        },
    },
    {
        path: 'route/:params',
        component: ScheduleSearchPageComponent,
        canActivate: [CanActivateScheduleRouteSearch],
        resolve: {
            urlParams: ScheduleSearchPageUrlParamsResolver,
            breadcrumbs: ScheduleSearchBreadCrumbsResolver,
        },
    },
    {
        // Path can have arbitrary length:
        // /SU0001-${date}/SU0002-${date} or
        // /SU0001-${date}/SU0002-${date}/SU0003-${date}
        // and so on. So we cannot use url params (:params)
        // because we do not know how much params do we need.
        // Instead, we should parse url by ourselves.
        matcher: (segments) => {
            if (!segments.length) {
                return null;
            }

            const flightsInfo = segments
                .map((segment) => segment.path)
                .join('/');

            return {
                consumed: segments,
                posParams: {
                    [FLIGHTS_INFO_PARAM_NAME]: new UrlSegment(flightsInfo, {}),
                },
            };
        },
        component: ScheduleFlightDetailsPageComponent,
        canActivate: [CanActivateScheduleDetails],
        resolve: {
            urlParams: ScheduleDetailsUrlParamsResolver,
            requestData: ScheduleRequestDataResolver,
            breadcrumbs: ScheduleDetailsBreadCrumbsResolver,
        },
    },
    {
        path: '',
        component: ScheduleStartPageComponent,
    },
];

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule],
})
export class ScheduleRoutingModule {}
