import { TestBed } from '@angular/core/testing';
import { OldAirportVisibilityService } from '@shared/services/old-airport-visibility.service';

function getFirstFlightLegs(withLatest = true) {
    const legs: any[] = [
        {
            departure: {
                scheduled: {
                    airportCode: 'KUF',
                },
                latest: withLatest
                    ? {
                          airportCode: 'KUF',
                      }
                    : undefined,
            },
            arrival: {
                scheduled: {
                    airportCode: 'SVO',
                },
                latest: withLatest
                    ? {
                          airportCode: 'VKO',
                      }
                    : undefined,
            },
        },
        {
            departure: {
                scheduled: {
                    airportCode: 'SVO',
                },
                latest: withLatest
                    ? {
                          airportCode: 'VKO',
                      }
                    : undefined,
            },
            arrival: {
                scheduled: {
                    airportCode: 'CDG',
                },
                latest: withLatest
                    ? {
                          airportCode: 'CDG',
                      }
                    : undefined,
            },
        },
    ];

    legs[0].next = legs[1];
    legs[1].previous = legs[0];

    return legs;
}

function getSecondFlightLegs() {
    const legs: any[] = [
        {
            departure: {
                scheduled: {
                    airportCode: 'SVO',
                },
                latest: {
                    airportCode: 'SVO',
                },
            },
            arrival: {
                scheduled: {
                    airportCode: 'SCO',
                },
                latest: {
                    airportCode: 'SVO',
                },
            },
        },
        {
            departure: {
                scheduled: {
                    airportCode: 'SVO',
                },
                latest: {
                    airportCode: 'SVO',
                },
            },
            arrival: {
                scheduled: {
                    airportCode: 'SCO',
                },
                latest: {
                    airportCode: 'SCO',
                },
            },
        },
    ];

    legs[0].next = legs[1];
    legs[1].previous = legs[0];

    return legs;
}

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

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

        service = TestBed.inject(OldAirportVisibilityService);
    });

    it('should return departure', () => {
        const legs = getFirstFlightLegs();
        expect(service.departureStationForTerminalChange(legs[1])).toEqual(
            legs[1].departure,
        );
    });

    it('should return arrival', () => {
        const legs = getFirstFlightLegs();
        expect(service.arrivalStationForTerminalChange(legs[0])).toEqual(
            legs[0].arrival,
        );
    });

    it('should return undefined instead of departure', () => {
        const legs = getSecondFlightLegs();
        expect(service.departureStationForTerminalChange(legs[1])).toBe(
            undefined,
        );
    });

    it('should return undefined instead of arrival', () => {
        const legs = getSecondFlightLegs();
        expect(service.arrivalStationForTerminalChange(legs[0])).toBe(
            undefined,
        );
    });

    it('should return undefined because leg is not connected', () => {
        expect(service.arrivalStationForTerminalChange({} as any)).toBe(
            undefined,
        );
        expect(service.departureStationForTerminalChange({} as any)).toBe(
            undefined,
        );
    });

    it('should return undefined because there is no latest info', () => {
        const legs = getFirstFlightLegs(false);
        expect(service.departureStationForTerminalChange(legs[1])).toEqual(
            undefined,
        );
    });
});
