import { TestBed } from '@angular/core/testing';
import { CurrentScheduleService } from '@schedule/pages/flight-details/services/current-schedule.service';
import { ScheduleDetailsDataSourceService } from '@schedule/services/data-source/details-data-source.service';
import { getScheduleDetailsDataSourceMock } from '@schedule/services/data-source/details-data-source.service.mock';
import { ScheduleHelpersService } from '@schedule/services/schedule-helpers.service';
import { ScheduleStateService } from '@schedule/services/state.service';
import { ScheduleItemModel } from '@shared/models-legacy';

describe('CurrentScheduleService', () => {
    let service: CurrentScheduleService;
    let dataSource: ScheduleDetailsDataSourceService;
    let scheduleHelpers: ScheduleHelpersService;

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [
                CurrentScheduleService,
                ScheduleStateService,
                ScheduleHelpersService,
                {
                    provide: ScheduleDetailsDataSourceService,
                    useValue: getScheduleDetailsDataSourceMock(),
                },
            ],
        });

        service = TestBed.inject(CurrentScheduleService);
        dataSource = TestBed.inject(ScheduleDetailsDataSourceService);
        scheduleHelpers = TestBed.inject(ScheduleHelpersService);
    });

    it('should return outbound schedule as current schedule', () => {
        expect(service.getCurrentSchedule()).toEqual(dataSource.scheduleLegacy);
    });

    it('should return inbound schedule as current schedule', () => {
        (dataSource as any).testUtils_swapFlight.bind(dataSource)();
        expect(service.getCurrentSchedule()).toEqual(
            dataSource.returnScheduleLegacy,
        );
    });

    it('should return empty schedule', () => {
        (dataSource as any).flightLegacy = undefined;
        expect(service.getCurrentSchedule()).toEqual([]);
    });

    it('should return default schedule', () => {
        const schedule = [
            new ScheduleItemModel({
                date: new Date(2022, 4, 28),
                flights: [dataSource.flightLegacy],
            }),
        ];
        const getScheduleTypeMock = spyOn(scheduleHelpers, 'getScheduleType');
        getScheduleTypeMock.and.returnValue('default-schedule');
        const getDefaultScheduleForFlightMock = spyOn(
            scheduleHelpers,
            'getDefaultScheduleForFlight',
        );
        getDefaultScheduleForFlightMock.and.returnValue(schedule);

        expect(service.getCurrentSchedule()).toEqual(schedule);
    });
});
