import { TestBed } from '@angular/core/testing';
import { DictionariesService } from '@modules/components/page-filters/services/dictionaries-service';
import { getDictionariesServiceMock } from '@modules/components/page-filters/services/dictionaries-service.mock';
import { TranslateService } from '@ngx-translate/core';
import { OnlineBoardTitleService } from '@online-board/services/title.service';
import { IOnlineBoardRoutePageUrlParams } from '@online-board/services/url';
import { DateTransformerService } from '@shared/services/date/date-transformer.service';

describe('OnlineBoardTitleService', () => {
    let service: OnlineBoardTitleService;
    const instantMock = jasmine.createSpy('instant');

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [
                OnlineBoardTitleService,
                DateTransformerService,
                {
                    provide: DictionariesService,
                    useValue: getDictionariesServiceMock(),
                },
                {
                    provide: TranslateService,
                    useValue: {
                        instant: instantMock,
                    },
                },
            ],
        });

        service = TestBed.inject(OnlineBoardTitleService);
        instantMock.calls.reset();

        jasmine.clock().install();
        jasmine.clock().mockDate(new Date(2022, 4, 1));
    });

    afterEach(() => {
        jasmine.clock().uninstall();
    });

    it('should return root title', () => {
        expect(service.getStartPageTitle()).toBe('BOARD.TITLE');
    });

    it('should return empty string as arrival title if arrival was not provided in params', async () => {
        const result = await service.getArrivalTitle({} as any);
        expect(result).toBe('');
    });

    it('should return title for arrival page', async () => {
        instantMock.withArgs('BOARD.ARRIVAL').and.returnValue('Прилет');
        const params: IOnlineBoardRoutePageUrlParams = {
            arrival: 'MOW',
            date: new Date(2022, 4, 28),
        };

        let result = await service.getArrivalTitle(params);
        expect(result).toBe('Прилет: Москва, 28.05.2022');

        result = await service.getArrivalTitle(params, false);
        expect(result).toBe('Прилет: Москва');
    });

    it('should return empty string as departure title if departure was not provided in params', async () => {
        const result = await service.getDepartureTitle({} as any);
        expect(result).toBe('');
    });

    it('should return title for departure page', async () => {
        instantMock.withArgs('BOARD.DEPARTURE').and.returnValue('Вылет');
        const params: IOnlineBoardRoutePageUrlParams = {
            departure: 'MOW',
            date: new Date(2022, 4, 28),
        };

        let result = await service.getDepartureTitle(params);
        expect(result).toBe('Вылет: Москва, 28.05.2022');

        result = await service.getDepartureTitle(params, false);
        expect(result).toBe('Вылет: Москва');
    });

    it('should return empty string as route title if departure or arrival was not provided in params', async () => {
        let result = await service.getRouteTitle({} as any);
        expect(result).toBe('');

        result = await service.getRouteTitle({ arrival: 'MOW' } as any);
        expect(result).toBe('');

        result = await service.getRouteTitle({ departure: 'MOW' } as any);
        expect(result).toBe('');
    });

    it('should return title for route page', async () => {
        instantMock.withArgs('BOARD.ROUTE').and.returnValue('Маршрут');
        const params: IOnlineBoardRoutePageUrlParams = {
            departure: 'MOW',
            arrival: 'MRS',
            date: new Date(2022, 4, 28),
        };

        let result = await service.getRouteTitle(params);
        expect(result).toBe('Маршрут: Москва - Марсель, 28.05.2022');

        result = await service.getRouteTitle(params, false);
        expect(result).toBe('Маршрут: Москва - Марсель');
    });

    it('should return title for flight number page', () => {
        instantMock.withArgs('SHARED.NUMBER').and.returnValue('Рейс');

        expect(
            service.getFlightNumberTitle({
                flightNumber: '0001',
                carrier: 'SU',
                date: new Date(2022, 4, 28),
            }),
        ).toBe('Рейс: SU 0001, 28.05.2022');

        expect(
            service.getFlightNumberTitle(
                {
                    flightNumber: '0001',
                    carrier: 'SU',
                    date: new Date(2022, 4, 28),
                },
                false,
            ),
        ).toBe('Рейс: SU 0001');
    });
});
