import { TestBed } from '@angular/core/testing';
import { ScheduleHelpersService } from '@schedule/services/schedule-helpers.service';
import { RouteTypeLegacy } from '@shared/enumerators';
import { ScheduleItemModel } from '@shared/models-legacy';

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

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

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

    function getFlights(startPId: number) {
        return [
            {
                routeType: RouteTypeLegacy.Direct,
                pId: String(startPId + 1),
            } as any,
            {
                routeType: RouteTypeLegacy.MultiLeg,
                pId: String(startPId + 2),
            } as any,
            {
                routeType: RouteTypeLegacy.Connecting,
                flights: [
                    {
                        routeType: RouteTypeLegacy.Direct,
                        pId: String(startPId + 3),
                    },
                    {
                        routeType: RouteTypeLegacy.MultiLeg,
                        pId: String(startPId + 4),
                    },
                ],
            } as any,
        ];
    }
    function getSchedule(startPId = 0): ScheduleItemModel[] {
        const firstItem = new ScheduleItemModel({
            date: new Date(2022, 4, 27),
            flights: getFlights(startPId),
        });
        const secondItem = new ScheduleItemModel({
            date: new Date(2022, 4, 28),
            flights: getFlights(startPId + 4),
        });
        const thirdItem = new ScheduleItemModel({
            date: new Date(2022, 4, 29),
            flights: getFlights(startPId + 8),
        });
        return [firstItem, secondItem, thirdItem];
    }

    it('should return true if schedule contains flight', () => {
        const schedule = getSchedule();
        const flight = {
            routeType: RouteTypeLegacy.Direct,
            pId: '1',
        };

        expect(service.doesScheduleContainFlight(schedule, flight as any)).toBe(
            true,
        );
    });

    it('should return false if schedule does not contain flight', () => {
        const schedule = getSchedule();
        const flight = {
            routeType: RouteTypeLegacy.Direct,
            pId: '100',
        };

        expect(service.doesScheduleContainFlight(schedule, flight as any)).toBe(
            false,
        );
    });

    it('should return empty-schedule type if flight is not provided', () => {
        expect(service.getScheduleType()).toBe('empty-schedule');
    });

    it('should return default-schedule type if both schedules are undefined', () => {
        const flight = {
            routeType: RouteTypeLegacy.Direct,
            pId: '1',
        };
        expect(service.getScheduleType(flight as any)).toBe('default-schedule');
    });

    it('should return default-schedule type if both schedules do not contain flight', () => {
        const flight = {
            routeType: RouteTypeLegacy.Direct,
            pId: '100',
        };

        const schedule = getSchedule();
        const returnSchedule = getSchedule(10);
        expect(
            service.getScheduleType(flight as any, schedule, returnSchedule),
        ).toBe('default-schedule');
    });

    it('should return schedule type if outbound schedule contains flight', () => {
        const flight = {
            routeType: RouteTypeLegacy.Direct,
            pId: '1',
        };

        const outboundSchedule = getSchedule();
        const inboundSchedule = getSchedule(10);
        expect(
            service.getScheduleType(
                flight as any,
                outboundSchedule,
                inboundSchedule,
            ),
        ).toBe('schedule');
    });

    it('should return return-schedule type if inbound schedule contains flight', () => {
        const flight = {
            routeType: RouteTypeLegacy.Direct,
            pId: '11',
        };

        const outboundSchedule = getSchedule();
        const inboundSchedule = getSchedule(10);
        expect(
            service.getScheduleType(
                flight as any,
                outboundSchedule,
                inboundSchedule,
            ),
        ).toBe('return-schedule');
    });

    it('should return default schedule with one flight', () => {
        const flight = {
            routeType: RouteTypeLegacy.Direct,
            pId: '11',
            dateToSearchBy: new Date(2022, 4, 28),
        };

        const schedule = service.getDefaultScheduleForFlight(flight as any);
        expect(schedule.length).toBe(1);
        expect(schedule[0].date.toDateString()).toBe(
            flight.dateToSearchBy.toDateString(),
        );
        expect(schedule[0].flights.length).toBe(1);
        expect(schedule[0].flights[0]).toEqual(flight as any);
    });

    it('should return empty schedule for 26.05.2022', () => {
        const schedule = getSchedule();
        const emptyScheduleItem = service.getFirstScheduleDayToAdd(schedule);

        expect(emptyScheduleItem.date).toEqual(new Date(2022, 4, 26));
        expect(emptyScheduleItem.flights.length).toBe(0);
    });

    it('should return empty schedule for 30.05.2022', () => {
        const schedule = getSchedule();
        const emptyScheduleItem = service.getLastScheduleDayToAdd(schedule);

        expect(emptyScheduleItem.date).toEqual(new Date(2022, 4, 30));
        expect(emptyScheduleItem.flights.length).toBe(0);
    });
});
