import { TestBed } from '@angular/core/testing';
import { Router } from '@angular/router';
import {
    IDetailsPageNavigationParams,
    OnlineBoardNavigationService,
} from '@online-board/services/navigation.service';
import {
    IOnlineBoardRoutePageUrlParams,
    ONLINE_BOARD_URL_BASE,
    OnlineBoardUrlBuilderService,
} from '@online-board/services/url';
import { ApiParserService } from '@shared/services/api/parser.service';

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

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [
                OnlineBoardNavigationService,
                OnlineBoardUrlBuilderService,
                ApiParserService,
                {
                    provide: Router,
                    useValue: {
                        navigateByUrl: navigateByUrlMock,
                        navigate: navigateMock,
                    },
                },
                {
                    provide: ONLINE_BOARD_URL_BASE,
                    useValue: 'onlineboard',
                },
            ],
        });

        service = TestBed.inject(OnlineBoardNavigationService);

        navigateByUrlMock.calls.reset();
        navigateMock.calls.reset();
    });

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

    it('should call navigate with details page url', () => {
        const params: IDetailsPageNavigationParams = {
            flightId: {
                flightNumber: '0001',
                carrier: 'SU',
                date: '',
            },
            date: new Date(2022, 4, 28),
        };
        service.toDetailsPage(params);
        expect(navigateMock).toHaveBeenCalledWith(
            ['onlineboard/SU0001-20220528'],
            { queryParams: { request: undefined } },
        );
    });

    it('should call navigate with details page url and request query param', () => {
        const params: IDetailsPageNavigationParams = {
            flightId: {
                flightNumber: '0001',
                carrier: 'SU',
                date: '',
            },
            date: new Date(2022, 4, 28),
            request: 'request string',
        };
        service.toDetailsPage(params);
        expect(navigateMock).toHaveBeenCalledWith(
            ['onlineboard/SU0001-20220528'],
            { queryParams: { request: 'request string' } },
        );
    });

    it('should call navigateByUrl with flight number page url', () => {
        service.toFlightNumberPage({
            flightNumber: '0001',
            carrier: 'SU',
            date: new Date(2022, 4, 28),
        });
        expect(navigateByUrlMock).toHaveBeenCalledWith(
            'onlineboard/flight/SU0001-20220528',
        );
    });

    it('should not call navigateByUrl if arrival and departure are not provided', () => {
        const params: IOnlineBoardRoutePageUrlParams = {
            date: new Date(2022, 4, 28),
        };
        service.toRoutePage(params);
        expect(navigateByUrlMock).not.toHaveBeenCalled();
    });

    it('should call navigateByUrl with arrival page url', () => {
        const params: IOnlineBoardRoutePageUrlParams = {
            arrival: 'MOW',
            date: new Date(2022, 4, 28),
        };
        service.toRoutePage(params);
        expect(navigateByUrlMock).toHaveBeenCalledWith(
            'onlineboard/arrival/MOW-20220528',
        );
    });

    it('should call navigateByUrl with departure page url', () => {
        const params: IOnlineBoardRoutePageUrlParams = {
            departure: 'MOW',
            date: new Date(2022, 4, 28),
        };
        service.toRoutePage(params);
        expect(navigateByUrlMock).toHaveBeenCalledWith(
            'onlineboard/departure/MOW-20220528',
        );
    });

    it('should call navigateByUrl with route page url', () => {
        const params: IOnlineBoardRoutePageUrlParams = {
            departure: 'MOW',
            arrival: 'GDX',
            date: new Date(2022, 4, 28),
        };
        service.toRoutePage(params);
        expect(navigateByUrlMock).toHaveBeenCalledWith(
            'onlineboard/route/MOW-GDX-20220528',
        );
    });
});
