import { TestBed } from '@angular/core/testing';
import { OnlineBoardRequestBuilderService } from '@online-board/services/request/request-builder.service';
import { IOnlineBoardRoutePageUrlParams } from '@online-board/services/url/types';
import { ONLINE_BOARD_URL_BASE } from '@online-board/services/url/url-base';
import { OnlineBoardUrlBuilderService } from '@online-board/services/url/url-builder.service';
import { IFlightId } from '@typings/flight/flight-id';

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

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [
                OnlineBoardRequestBuilderService,
                OnlineBoardUrlBuilderService,
                {
                    provide: ONLINE_BOARD_URL_BASE,
                    useValue: 'onlineboard',
                },
            ],
        });

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

    it('should build request from arrival params without time range', () => {
        const params: IOnlineBoardRoutePageUrlParams = {
            arrival: 'SVO',
            date: new Date(2022, 2, 5),
        };

        expect(service.buildRouteRequest(params)).toBe(
            'onlineboard-arrival-SVO-20220305',
        );
    });

    it('should build request from arrival params with time range', () => {
        const params: IOnlineBoardRoutePageUrlParams = {
            arrival: 'SVO',
            date: new Date(2022, 2, 5),
            timeFrom: '0600',
            timeTo: '2200',
        };

        expect(service.buildRouteRequest(params)).toBe(
            'onlineboard-arrival-SVO-20220305-06002200',
        );
    });

    it('should build request from departure params without time range', () => {
        const params: IOnlineBoardRoutePageUrlParams = {
            departure: 'SVO',
            date: new Date(2022, 2, 5),
        };

        expect(service.buildRouteRequest(params)).toBe(
            'onlineboard-departure-SVO-20220305',
        );
    });

    it('should build request from departure params with time range', () => {
        const params: IOnlineBoardRoutePageUrlParams = {
            departure: 'SVO',
            date: new Date(2022, 2, 5),
            timeFrom: '0600',
            timeTo: '2200',
        };

        expect(service.buildRouteRequest(params)).toBe(
            'onlineboard-departure-SVO-20220305-06002200',
        );
    });

    it('should build request from route params without time range', () => {
        const params: IOnlineBoardRoutePageUrlParams = {
            departure: 'MOW',
            arrival: 'LED',
            date: new Date(2022, 2, 5),
        };

        expect(service.buildRouteRequest(params)).toBe(
            'onlineboard-route-MOW-LED-20220305',
        );
    });

    it('should build request from route params with time range', () => {
        const params: IOnlineBoardRoutePageUrlParams = {
            departure: 'MOW',
            arrival: 'LED',
            date: new Date(2022, 2, 5),
            timeFrom: '0600',
            timeTo: '2200',
        };

        expect(service.buildRouteRequest(params)).toBe(
            'onlineboard-route-MOW-LED-20220305-06002200',
        );
    });

    it('should build request from flight number params', () => {
        const flightId: IFlightId = {
            flightNumber: 'SU1210',
            carrier: '',
            date: '',
        };
        const date = new Date(2022, 2, 5);

        expect(service.buildFlightNumberRequest(flightId, date)).toBe(
            'onlineboard-flight-SU1210-20220305',
        );
    });

    it('should return undefined for route params if neither departure nor arrival were provided', () => {
        const params: IOnlineBoardRoutePageUrlParams = {
            date: new Date(2022, 2, 5),
        };

        expect(service.buildRouteRequest(params)).toBe(undefined);
    });
});
