import { Inject, Injectable } from '@angular/core';
import { UrlBuilderService } from '@shared/services/url/url-builder.service';
import { ApiParserService } from '@shared/services/api/parser.service';
import { IUrlTimeRange } from '@typings/common/url';
import { IFlightId, IParsedFlightId } from '@typings/flight/flight-id';
import { IOnlineBoardRoutePageUrlParams } from './types';
import { ONLINE_BOARD_URL_BASE } from './url-base';

@Injectable()
export class OnlineBoardUrlBuilderService extends UrlBuilderService {
    constructor(
        @Inject(ONLINE_BOARD_URL_BASE) public base,
        protected apiParser: ApiParserService,
    ) {
        super(apiParser);
    }

    public getStartPageUrl() {
        return this.base;
    }

    public getDetailsPageUrl(flightId: IFlightId, searchDate: Date) {
        return `${this.base}/${this.formatFlightIdWithDate(
            flightId,
            searchDate,
        )}`;
    }

    public getFlightNumberPageUrl(params: IParsedFlightId) {
        const urlDate = this.formatDate(params.date);
        // uncomment in the following line to use 123D instead of 0123D style for flight numbers
        const flightNumber = (params.flightNumber+params.suffix).slice(/*+params.flightNumber > 999 ?*/ -5 /*: -4*/);

        return `${this.base}/flight/${params.carrier}${flightNumber}-${urlDate}`;
    }

    public getRoutePageUrl(params: IOnlineBoardRoutePageUrlParams) {
        const { departure, arrival, date } = params;

        if (departure && !arrival) {
            return this.getDeparturePageUrl(params);
        }

        if (!departure && arrival) {
            return this.getArrivalPageUrl(params);
        }

        const baseUrl = `${this.base}/route`;
        const location = `${departure}-${arrival}`;
        const timeRange: IUrlTimeRange = {
            timeFrom: params.timeFrom,
            timeTo: params.timeTo,
        };

        return this.getLocationPageUrl(baseUrl, location, date, timeRange);
    }

    private getArrivalPageUrl(params: IOnlineBoardRoutePageUrlParams) {
        const baseUrl = `${this.base}/arrival`;
        const timeRange: IUrlTimeRange = {
            timeFrom: params.timeFrom,
            timeTo: params.timeTo,
        };

        return this.getLocationPageUrl(
            baseUrl,
            params.arrival,
            params.date,
            timeRange,
        );
    }

    private getDeparturePageUrl(params: IOnlineBoardRoutePageUrlParams) {
        const baseUrl = `${this.base}/departure`;
        const timeRange: IUrlTimeRange = {
            timeFrom: params.timeFrom,
            timeTo: params.timeTo,
        };

        return this.getLocationPageUrl(
            baseUrl,
            params.departure,
            params.date,
            timeRange,
        );
    }

    private getLocationPageUrl(
        baseUrl: string,
        location: string,
        flightDate: Date,
        urlTimeRange?: IUrlTimeRange,
    ) {
        const urlDate = this.formatDate(flightDate);
        const timeRange = this.formatTimeRange(urlTimeRange);

        let params = `${location}-${urlDate}`;

        if (timeRange) {
            params = `${params}-${timeRange}`;
        }

        return `${baseUrl}/${params}`;
    }
}
