import { TestBed } from '@angular/core/testing';
import { Router } from '@angular/router';
import { ScheduleNavigationService } from '@schedule/services/navigation.service';
import { IScheduleRouteDirectionParams } from '@schedule/services/url/types';
import { SCHEDULE_URL_BASE } from '@schedule/services/url/url-base';
import { ScheduleUrlBuilderService } from '@schedule/services/url/url-builder.service';
import { ApiParserService } from '@shared/services/api/parser.service';
import { UrlBuilderService } from '@shared/services/url/url-builder.service';
import { RouteType } from '@typings/enums';
import { IFlight } from '@typings/flight/flight';

describe('ScheduleNavigationService', () => {
    let service: ScheduleNavigationService;
    const navigateByUrlMock = jasmine.createSpy('navigateByUrl');
    const navigateMock = jasmine.createSpy('navigate');

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [
                ScheduleNavigationService,
                UrlBuilderService,
                ApiParserService,
                ScheduleUrlBuilderService,
                {
                    provide: Router,
                    useValue: {
                        navigateByUrl: navigateByUrlMock,
                        navigate: navigateMock,
                    },
                },
                {
                    provide: SCHEDULE_URL_BASE,
                    useValue: 'schedule',
                },
            ],
        });

        service = TestBed.inject(ScheduleNavigationService);
        navigateByUrlMock.calls.reset();
        navigateMock.calls.reset();
    });

    function getFlight(): IFlight {
        return {
            routeType: RouteType.DIRECT,
            flightId: {
                flightNumber: '0012',
                carrier: 'SU',
                date: '2022-05-01',
            },
            leg: {
                departure: {
                    times: {
                        scheduledDeparture: {
                            local: '2022-05-27T19:00:00+03:00',
                        },
                    },
                },
            },
        } as any;
    }

    it('should call navigateByUrl with route page url', () => {
        const params: IScheduleRouteDirectionParams = {
            departure: 'MOW',
            arrival: 'GDX',
            dateTo: new Date(2022, 4, 30),
            dateFrom: new Date(2022, 4, 28),
        };

        service.toRoutePage(params);
        expect(navigateByUrlMock).toHaveBeenCalledWith(
            'schedule/route/MOW-GDX-20220528-20220530',
        );
    });

    it('should call navigateByUrl with start page url', () => {
        service.toStartPage();
        expect(navigateByUrlMock).toHaveBeenCalledWith('schedule');
    });

    it('should call navigateByUrl with details page url', () => {
        service.toDetailsPage(getFlight());
        expect(navigateMock).toHaveBeenCalledWith(
            ['schedule/SU0012-20220527'],
            { queryParams: { request: undefined } },
        );
    });

    it('should call navigateByUrl with details page url and request query param', () => {
        service.toDetailsPage(getFlight(), 'request-string');
        expect(navigateMock).toHaveBeenCalledWith(
            ['schedule/SU0012-20220527'],
            { queryParams: { request: 'request-string' } },
        );
    });
});
