import { TestBed } from '@angular/core/testing';
import { SCHEDULE_URL_BASE } from '@schedule/services/url/url-base';
import { ScheduleUrlParserService } from '@schedule/services/url/url-parser.service';

describe('ScheduleUrlParserService', () => {
    let service: ScheduleUrlParserService;

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [
                ScheduleUrlParserService,
                {
                    provide: SCHEDULE_URL_BASE,
                    useValue: 'schedule',
                },
            ],
        });
        service = TestBed.inject(ScheduleUrlParserService);
    });

    it('should parse url for details page', () => {
        const urlPart = 'SU1765-20220307';

        const result = service.parseFlightDetailsParams(urlPart);

        expect(result.length).toBe(1);
        expect(result[0].flightNumber).toBe('1765');
        expect(result[0].carrier).toBe('SU');
        expect(result[0].date).toEqual(new Date(2022, 2, 7));
    });

    it('should parse url for connecting flight details page', () => {
        const urlPart = 'SU1210-20220307/SU1765-20220307';

        const result = service.parseFlightDetailsParams(urlPart);

        expect(result.length).toBe(2);
        expect(result[0].flightNumber).toBe('1210');
        expect(result[0].carrier).toBe('SU');
        expect(result[0].date).toEqual(new Date(2022, 2, 7));
        expect(result[1].flightNumber).toBe('1765');
        expect(result[1].carrier).toBe('SU');
        expect(result[1].date).toEqual(new Date(2022, 2, 7));
    });

    it('should parse url without time range and connections', () => {
        const paramsUrlPart = 'MOW-KUF-20220425-20220501';
        assertRoute(paramsUrlPart);
    });

    it('should parse url without time range', () => {
        const paramsUrlPart = 'MOW-KUF-20220425-20220501-C1';
        assertRoute(paramsUrlPart, undefined, undefined, 1);
    });

    it('should parse url without connections', () => {
        const paramsUrlPart = 'MOW-KUF-20220425-20220501-06002200';
        assertRoute(paramsUrlPart, '0600', '2200');
    });

    it('should parse url with all params', () => {
        const paramsUrlPart = 'MOW-KUF-20220425-20220501-06002200-С0';
        assertRoute(paramsUrlPart, '0600', '2200', 0);
    });

    function assertRoute(
        params: string,
        timeFrom?: string,
        timeTo?: string,
        connections?: number,
    ) {
        const result = service.parseRouteParams(params);

        expect(result.departure).toBe('MOW');
        expect(result.arrival).toBe('KUF');
        expect(result.dateFrom).toEqual(new Date(2022, 3, 25));
        expect(result.dateTo).toEqual(new Date(2022, 4, 1));
        expect(result.timeFrom).toBe(timeFrom);
        expect(result.timeTo).toBe(timeTo);
        expect(result.connections).toBe(connections);
    }
});
