import { IParsedFlightId } from '@typings/flight/flight-id';
import { Inject, Injectable } from '@angular/core';
import { UrlParserService } from '@shared/services/url/url-parser.service';
import { IScheduleDetailsParams, IScheduleRouteDirectionParams } from './types';
import { SCHEDULE_URL_BASE } from './url-base';

@Injectable()
export class ScheduleUrlParserService extends UrlParserService {
    constructor(@Inject(SCHEDULE_URL_BASE) public base) {
        super();
    }

    parseRouteParams(params: string): IScheduleRouteDirectionParams {
        // Last two params are timeRange and connections.
        // Both params are optional, so we cannot rely on
        // params order because connections param can be in
        // params but timeRange not.
        const [departure, arrival, dateFrom, dateTo, ...rest] =
            params.split('-');

        const { timeRange, connections } =
            ScheduleUrlParserService.parseOptionalRouteParams(rest);

        return {
            departure,
            arrival,
            connections: ScheduleUrlParserService.parseConnections(connections),
            dateFrom: this.parseDate(dateFrom),
            dateTo: this.parseDate(dateTo),
            ...this.parseTimeRange(timeRange),
        };
    }

    parseFlightDetailsParams(params: string, departure: string, arrival: string): IScheduleDetailsParams {
        const flightsParams = params.split('/');

        if (flightsParams.length > 0 && flightsParams[0].length==3) {
            let flights: IParsedFlightId[] = [];
            for(var i=0; i<flightsParams.length; i++) {
                if (flightsParams[i].length <= 3) {
                    continue;
                }
                let flight = this.parseFlightId(flightsParams[i]);
                if (i > 0)
                    flight.departure = flightsParams[i-1];
                if (i < flightsParams.length - 1)
                    flight.arrival = flightsParams[i+1];
                flights.push(flight);
            }
            return {
                flights: flights,
                departure: departure,
                arrival: arrival,
             };
        }

        return {
            flights: flightsParams.map((flightParams) => {
                return this.parseFlightId(flightParams);
            }),
            departure: departure,
            arrival: arrival,
        };
    }

    private static parseConnections(connections?: string) {
        // connections format is `C${numberOfConnections}`
        return connections ? Number(connections.slice(1)) : undefined;
    }

    private static parseOptionalRouteParams(params: string[]) {
        if (!params.length) {
            return {};
        }

        if (params.length === 2) {
            return {
                timeRange: params[0],
                connections: params[1],
            };
        }

        // params === [`C${numberOfConnections`}]
        if (params[0][0] === 'C') {
            return {
                connections: params[0],
            };
        }

        return {
            timeRange: params[0],
        };
    }
}
