import { findClosestFlight } from '@online-board/pages/search/components/view/utils/find-closest-flight';

const mockedFlights = {
    yesterday: [
        {
            routeType: 'Direct',
            leg: {
                arrival: {
                    times: {
                        actual: {
                            local: '2021-12-16T21:48:00',
                        },
                    },
                },
                departure: {
                    times: {
                        actual: {
                            local: '2021-12-16T21:48:00',
                        },
                    },
                },
            },
        },
        {
            routeType: 'Direct',
            leg: {
                arrival: {
                    times: {
                        actual: {
                            local: '2021-12-17T04:05:00',
                        },
                    },
                },
                departure: {
                    times: {
                        actual: {
                            local: '2021-12-17T04:05:00',
                        },
                    },
                },
            },
        },
        {
            routeType: 'Direct',
            leg: {
                arrival: {
                    times: {
                        actual: {
                            local: '2021-12-17T06:12:00',
                        },
                    },
                },
                departure: {
                    times: {
                        actual: {
                            local: '2021-12-17T06:12:00',
                        },
                    },
                },
            },
        },
        {
            routeType: 'Direct',
            leg: {
                arrival: {
                    times: {
                        actual: {
                            local: '2021-12-17T06:57:00',
                        },
                    },
                },
                departure: {
                    times: {
                        actual: {
                            local: '2021-12-17T06:57:00',
                        },
                    },
                },
            },
        },
        {
            routeType: 'Direct',
            leg: {
                arrival: {
                    times: {
                        actual: {
                            local: '2021-12-17T09:16:00',
                        },
                    },
                },
                departure: {
                    times: {
                        actual: {
                            local: '2021-12-17T09:16:00',
                        },
                    },
                },
            },
        },
        {
            routeType: 'Direct',
            leg: {
                arrival: {
                    times: {
                        actual: {
                            local: '2021-12-17T13:29:00',
                        },
                    },
                },
                departure: {
                    times: {
                        actual: {
                            local: '2021-12-17T13:29:00',
                        },
                    },
                },
            },
        },
        {
            routeType: 'Direct',
            leg: {
                arrival: {
                    times: {
                        actual: {
                            local: '2021-12-17T16:35:00',
                        },
                    },
                },
                departure: {
                    times: {
                        actual: {
                            local: '2021-12-17T16:35:00',
                        },
                    },
                },
            },
        },
        {
            routeType: 'Direct',
            leg: {
                arrival: {
                    times: {
                        actual: {
                            local: '2021-12-17T18:05:00',
                        },
                    },
                },
                departure: {
                    times: {
                        actual: {
                            local: '2021-12-17T18:05:00',
                        },
                    },
                },
            },
        },
    ],
    today: [
        {
            routeType: 'Direct',
            leg: {
                arrival: {
                    times: {
                        actual: {
                            local: '2021-12-17T21:48:00',
                        },
                        scheduled: {
                            local: '2021-12-17T21:40:00',
                        },
                    },
                },
                departure: {
                    times: {
                        actual: {
                            local: '2021-12-17T21:48:00',
                        },
                        scheduled: {
                            local: '2021-12-17T21:40:00',
                        },
                    },
                },
            },
        },
        {
            routeType: 'Direct',
            leg: {
                arrival: {
                    times: {
                        scheduled: {
                            local: '2021-12-18T04:05:00',
                        },
                    },
                },
                departure: {
                    times: {
                        scheduled: {
                            local: '2021-12-18T04:05:00',
                        },
                    },
                },
            },
        },
        {
            routeType: 'Direct',
            leg: {
                arrival: {
                    times: {
                        actual: {
                            local: '2021-12-18T06:12:00',
                        },
                        scheduled: {
                            local: '2021-12-18T05:55:00',
                        },
                    },
                },
                departure: {
                    times: {
                        actual: {
                            local: '2021-12-18T06:12:00',
                        },
                        scheduled: {
                            local: '2021-12-18T05:55:00',
                        },
                    },
                },
            },
        },
        {
            routeType: 'Direct',
            leg: {
                arrival: {
                    times: {
                        actual: {
                            local: '2021-12-18T06:57:00',
                        },
                        scheduled: {
                            local: '2021-12-18T07:00:00',
                        },
                    },
                },
                departure: {
                    times: {
                        actual: {
                            local: '2021-12-18T06:57:00',
                        },
                        scheduled: {
                            local: '2021-12-18T07:00:00',
                        },
                    },
                },
            },
        },
        {
            routeType: 'Direct',
            leg: {
                arrival: {
                    times: {
                        actual: {
                            local: '2021-12-18T09:16:00',
                        },
                        scheduled: {
                            local: '2021-12-18T09:05:00',
                        },
                    },
                },
                departure: {
                    times: {
                        actual: {
                            local: '2021-12-18T09:16:00',
                        },
                        scheduled: {
                            local: '2021-12-18T09:05:00',
                        },
                    },
                },
            },
        },
        {
            routeType: 'Direct',
            leg: {
                arrival: {
                    times: {
                        actual: {
                            local: '2021-12-18T13:29:00',
                        },
                        scheduled: {
                            local: '2021-12-18T13:10:00',
                        },
                    },
                },
                departure: {
                    times: {
                        actual: {
                            local: '2021-12-18T13:29:00',
                        },
                        scheduled: {
                            local: '2021-12-18T13:10:00',
                        },
                    },
                },
            },
        },
        {
            routeType: 'Direct',
            leg: {
                arrival: {
                    times: {
                        scheduled: {
                            local: '2021-12-18T16:35:00',
                        },
                    },
                },
                departure: {
                    times: {
                        scheduled: {
                            local: '2021-12-18T16:35:00',
                        },
                    },
                },
            },
        },
        {
            routeType: 'Direct',
            leg: {
                arrival: {
                    times: {
                        scheduled: {
                            local: '2021-12-18T18:05:00',
                        },
                    },
                },
                departure: {
                    times: {
                        scheduled: {
                            local: '2021-12-18T18:05:00',
                        },
                    },
                },
            },
        },
    ],
    tomorrow: [
        {
            routeType: 'Direct',
            leg: {
                arrival: {
                    times: {
                        scheduled: {
                            local: '2021-12-18T21:40:00',
                        },
                    },
                },
            },
        },
        {
            routeType: 'Direct',
            leg: {
                arrival: {
                    times: {
                        scheduled: {
                            local: '2021-12-19T04:05:00',
                        },
                    },
                },
                departure: {
                    times: {
                        scheduled: {
                            local: '2021-12-19T04:05:00',
                        },
                    },
                },
            },
        },
        {
            routeType: 'Direct',
            leg: {
                arrival: {
                    times: {
                        scheduled: {
                            local: '2021-12-19T05:55:00',
                        },
                    },
                },
                departure: {
                    times: {
                        scheduled: {
                            local: '2021-12-19T05:55:00',
                        },
                    },
                },
            },
        },
        {
            routeType: 'Direct',
            leg: {
                arrival: {
                    times: {
                        scheduled: {
                            local: '2021-12-19T07:00:00',
                        },
                    },
                },
                departure: {
                    times: {
                        scheduled: {
                            local: '2021-12-19T07:00:00',
                        },
                    },
                },
            },
        },
        {
            routeType: 'Direct',
            leg: {
                arrival: {
                    times: {
                        scheduled: {
                            local: '2021-12-19T09:05:00',
                        },
                    },
                },
                departure: {
                    times: {
                        scheduled: {
                            local: '2021-12-19T09:05:00',
                        },
                    },
                },
            },
        },
        {
            routeType: 'Direct',
            leg: {
                arrival: {
                    times: {
                        scheduled: {
                            local: '2021-12-19T13:10:00',
                        },
                    },
                },
                departure: {
                    times: {
                        scheduled: {
                            local: '2021-12-19T13:10:00',
                        },
                    },
                },
            },
        },
        {
            routeType: 'Direct',
            leg: {
                arrival: {
                    times: {
                        scheduled: {
                            local: '2021-12-19T16:35:00',
                        },
                    },
                },
                departure: {
                    times: {
                        scheduled: {
                            local: '2021-12-19T16:35:00',
                        },
                    },
                },
            },
        },
        {
            routeType: 'Direct',
            leg: {
                arrival: {
                    times: {
                        scheduled: {
                            local: '2021-12-19T18:05:00',
                        },
                    },
                },
                departure: {
                    times: {
                        scheduled: {
                            local: '2021-12-19T18:05:00',
                        },
                    },
                },
            },
        },
    ],
};

describe('findClosestFlight', () => {
    beforeEach(() => {
        jasmine.clock().install();
    });

    afterEach(() => {
        jasmine.clock().uninstall();
    });

    it('should select the most closest future flight for today', () => {
        mockDate('2021-12-18T15:26:28');
        const flights = mockedFlights.today as any;
        const flight = findClosestFlight(flights, new Date('2021-12-18'), true);

        expect(flight).toEqual(flights[6]);
    });

    it('should select the most closest future flight for today if isArrival is false', () => {
        mockDate('2021-12-18T15:26:28');
        const flights = mockedFlights.today as any;
        const flight = findClosestFlight(
            flights,
            new Date('2021-12-18'),
            false,
        );

        expect(flight).toEqual(flights[6]);
    });

    it('should select the most closest completed flight for today', () => {
        mockDate('2021-12-18T13:46:28');
        const flights = mockedFlights.today as any;
        const flight = findClosestFlight(flights, new Date('2021-12-18'), true);

        expect(flight).toEqual(flights[5]);
    });

    it('should select last flight', () => {
        mockDate('2021-12-18T19:46:28');
        const flights = mockedFlights.today as any;
        const flight = findClosestFlight(flights, new Date('2021-12-18'), true);

        expect(flight).toEqual(flights[7]);
    });

    it('should select the first for tomorrow', () => {
        mockDate('2021-12-18T15:26:28');
        const flights = mockedFlights.tomorrow as any;
        const flight = findClosestFlight(flights, new Date('2021-12-17'), true);

        expect(flight).toEqual(flights[0]);
    });

    it('should select the last for yesterday', () => {
        mockDate('2021-12-18T15:26:28');
        const flights = mockedFlights.yesterday as any;
        const flight = findClosestFlight(flights, new Date('2021-12-19'), true);

        expect(flight).toEqual(flights[7]);
    });

    it('should return undefined if no flights were provided', () => {
        expect(findClosestFlight([], new Date('2021-12-19'), true)).toBe(
            undefined,
        );
        expect(findClosestFlight(undefined, new Date('2021-12-19'), true)).toBe(
            undefined,
        );
    });
});

function mockDate(date: string) {
    const currentDate = new Date(date);
    jasmine.clock().mockDate(currentDate);
}
