import { Injectable } from '@angular/core';
import { ApiParserService } from '@shared/services/api/parser.service';
import { IFlightId } from '@typings/flight/flight-id';
import FlightDateUtils from '@utils/flight/date';
import * as moment from 'moment';
import { ISimpleFlight } from '@typings/flight/flight';
import { IUrlTimeRange } from '@typings/common/url';

@Injectable({
    providedIn: 'root',
})
export class UrlBuilderService {
    constructor(protected apiParser: ApiParserService) {}

    formatDate(date: Date) {
        return moment(date).format('yyyyMMDD');
    }

    formatFlightId(flightId: IFlightId, withSpace = true) {
        return withSpace
            ? `${flightId.carrier}\u00a0${flightId.flightNumber}${flightId.suffix}`
            : `${flightId.carrier}${flightId.flightNumber}${flightId.suffix}`;
    }

    formatFlightIdWithDate(flightId: IFlightId, searchDate: Date) {
        const flightInfo = this.formatFlightId(flightId, false);
        const urlDate = this.formatDate(searchDate);

        return `${flightInfo}-${urlDate}`;
    }

    formatFlight(flight: ISimpleFlight, searchDate?: Date) {
        const date = searchDate
            ? searchDate
            : FlightDateUtils.getFlightDate(flight);

        return this.formatFlightIdWithDate(flight.flightId, date);
    }

    formatTimeRange(timeRange?: IUrlTimeRange) {
        if (!timeRange?.timeFrom || !timeRange.timeTo) {
            return undefined;
        }

        return `${timeRange.timeFrom}${timeRange.timeTo}`;
    }
}
