import { TestBed } from '@angular/core/testing';
import { DictionariesService } from '@modules/components/page-filters/services/dictionaries-service';
import { TranslateService } from '@ngx-translate/core';
import { ScheduleTitleService } from '@schedule/services/title.service';
import { IScheduleRouteDirectionParams } from '@schedule/services/url/types';

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

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [
                ScheduleTitleService,
                {
                    provide: TranslateService,
                    useValue: {
                        instant: instantMock,
                    },
                },
                {
                    provide: DictionariesService,
                    useValue: {
                        getCityOrAirportNameAsync:
                            getCityOrAirportNameAsyncMock,
                    },
                },
            ],
        });

        service = TestBed.inject(ScheduleTitleService);
        getCityOrAirportNameAsyncMock.calls.reset();
        instantMock.calls.reset();
    });

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

    it('should return search page title', async () => {
        const params: IScheduleRouteDirectionParams = {
            arrival: 'MRS',
            departure: 'MOW',
            dateFrom: new Date(2022, 4, 20),
            dateTo: new Date(2022, 4, 26),
        };

        getCityOrAirportNameAsyncMock
            .withArgs('MOW')
            .and.returnValue(Promise.resolve('Москва'));
        getCityOrAirportNameAsyncMock
            .withArgs('MRS')
            .and.returnValue(Promise.resolve('Марсель'));
        instantMock
            .withArgs('SCHEDULE.SCHEDULE-BY-ROUTE')
            .and.returnValue('Маршрут');

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

    it('should return route title', async () => {
        const params: IScheduleRouteDirectionParams = {
            arrival: 'MRS',
            departure: 'MOW',
            dateFrom: new Date(2022, 4, 20),
            dateTo: new Date(2022, 4, 26),
        };

        getCityOrAirportNameAsyncMock
            .withArgs('MOW')
            .and.returnValue(Promise.resolve('Москва'));
        getCityOrAirportNameAsyncMock
            .withArgs('MRS')
            .and.returnValue(Promise.resolve('Марсель'));

        const result = await service.getSearchPageRouteTitle(params);
        expect(result).toBe('Москва - Марсель');
    });
});
