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 { CanActivateStations } from '@online-board/guards/utils/can-activate-stations';
import { OnlineBoardNavigationService } from '@online-board/services/navigation.service';
import { StationCodeValidationService } from '@shared/services/validators/station-code.service';

describe('CanActivateStations', () => {
    let service: CanActivateStations;
    const toNotFoundMock = jasmine.createSpy('toNotFound');
    const navigationService: Partial<OnlineBoardNavigationService> = {
        toNotFound: toNotFoundMock,
    };

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [
                CanActivateStations,
                StationCodeValidationService,
                {
                    provide: DictionariesService,
                    useValue: getDictionariesServiceMock(),
                },
                {
                    provide: OnlineBoardNavigationService,
                    useValue: navigationService,
                },
            ],
        });

        toNotFoundMock.calls.reset();
        service = TestBed.inject(CanActivateStations);
    });

    it('can activate arrival station', async () => {
        const result = await service.canActivate({
            arrival: 'MOW',
        });

        expect(result).toBe(true);
    });

    it('can activate departure station', async () => {
        const result = await service.canActivate({
            departure: 'MRS',
        });

        expect(result).toBe(true);
    });

    it('can activate both stations', async () => {
        const result = await service.canActivate({
            arrival: 'MOW',
            departure: 'MRS',
        });

        expect(result).toBe(true);
    });

    it('redirect to not found because arrival is not valid', async () => {
        const result = await service.canActivate({
            arrival: 'ABA',
            departure: 'LED',
        });

        expect(result).not.toBe(true);
        expect(toNotFoundMock).toHaveBeenCalled();
    });

    it('redirect to not found because departure is not valid', async () => {
        const result = await service.canActivate({
            arrival: 'MOW',
            departure: 'ABA',
        });

        expect(result).not.toBe(true);
        expect(toNotFoundMock).toHaveBeenCalled();
    });

    it('redirect to not found because both stations are not valid', async () => {
        const result = await service.canActivate({
            arrival: 'KZN',
            departure: 'ABA',
        });

        expect(result).not.toBe(true);
        expect(toNotFoundMock).toHaveBeenCalled();
    });

    it('should redirect to not found because neither departure nor arrival were provided', async () => {
        const result = await service.canActivate({} as any);

        expect(result).not.toBe(true);
        expect(toNotFoundMock).toHaveBeenCalled();
    });
});
