import { TestBed } from '@angular/core/testing';
import { ApiParserService } from '@shared/services/api/parser.service';
import { UrlBuilderService } from '@shared/services/url/url-builder.service';
import { IUrlTimeRange } from '@typings/common/url';
import { IFlightId } from '@typings/flight/flight-id';
import FlightDateUtils from '@utils/flight/date';

describe('UrlBuilderServer', () => {
    let service: UrlBuilderService;
    let getFlightDateMock;

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [UrlBuilderService, ApiParserService],
        });

        service = TestBed.inject(UrlBuilderService);

        getFlightDateMock = spyOn(FlightDateUtils, 'getFlightDate');
    });

    it('should format date for url', () => {
        const date = new Date(2022, 4, 21);

        expect(service.formatDate(date)).toBe('20220521');
    });

    it('should format flight id', () => {
        const flightId: IFlightId = {
            flightNumber: '0001',
            carrier: 'SU',
            date: '',
        };

        expect(service.formatFlightId(flightId)).toBe('SU\u00a00001');
        expect(service.formatFlightId(flightId, false)).toBe('SU0001');
    });

    it('should format time range', () => {
        const timeRange: IUrlTimeRange = {
            timeFrom: '0030',
            timeTo: '2200',
        };

        expect(service.formatTimeRange(timeRange)).toBe('00302200');
    });

    it('should return undefined if time range does not contain one of the values or timeRange is not provided', () => {
        expect(
            service.formatTimeRange({
                timeFrom: '0030',
            }),
        ).toBe(undefined);

        expect(
            service.formatTimeRange({
                timeTo: '2200',
            }),
        ).toBe(undefined);

        expect(service.formatTimeRange()).toBe(undefined);
    });

    it('should format flight id with date', () => {
        const flightId: IFlightId = {
            flightNumber: '0001',
            carrier: 'SU',
            date: '',
        };

        expect(
            service.formatFlightIdWithDate(flightId, new Date(2022, 4, 29)),
        ).toBe('SU0001-20220529');
    });

    it('should format flight', () => {
        const date = new Date(2022, 4, 28);
        const flightId: IFlightId = {
            flightNumber: '0001',
            carrier: 'SU',
            date: '2022-05-2022T00:00:00',
        };
        const flight = { flightId } as any;
        getFlightDateMock.withArgs(flight).and.returnValue(date);

        expect(service.formatFlight(flight)).toBe('SU0001-20220528');
    });

    it('should format flight with provided date', () => {
        const date = new Date(2022, 4, 29);
        const flightId: IFlightId = {
            flightNumber: '0001',
            carrier: 'SU',
            date: '2022-05-2022T00:00:00',
        };
        const flight = { flightId } as any;

        expect(service.formatFlight(flight, date)).toBe('SU0001-20220529');
        expect(getFlightDateMock).not.toHaveBeenCalled();
    });
});
