import { Inject, Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve } from '@angular/router';
import { FLIGHTS_INFO_PARAM } from '@schedule/services/url/flights-info-param';
import { ScheduleRequestParserService } from '@schedule/services/request/request-parser.service';
import { ScheduleUrlParserService } from '@schedule/services/url/url-parser.service';
import { IScheduleUrlParams } from '@schedule/types';

@Injectable()
export class ScheduleDetailsUrlParamsResolver
    implements Resolve<IScheduleUrlParams>
{
    constructor(
        private urlService: ScheduleUrlParserService,
        private requestService: ScheduleRequestParserService,
        @Inject(FLIGHTS_INFO_PARAM) private flightsInfoParamName,
    ) {}

    resolve(route: ActivatedRouteSnapshot): IScheduleUrlParams {
        let departure: string;
        let arrival: string;

        if (route.queryParams.request != null) {
            const request = this.requestService.parseRouteRequest(route.queryParams.request);
            departure = request.outbound.departure;
            arrival = request.outbound.arrival;
        }

        const flightParams = route.params[this.flightsInfoParamName];

        return {
            details: this.urlService.parseFlightDetailsParams(flightParams, departure, arrival),
        };
    }
}
