import { TestBed } from '@angular/core/testing';
import { CanActivateDateParams } from '@online-board/guards/utils/can-activate-date-params';
import { OnlineBoardDateValidationService } from '@online-board/services/date-validation.service';
import { OnlineBoardNavigationService } from '@online-board/services/navigation.service';
import { OnlineBoardUrlParserService } from '@online-board/services/url/url-parser.service';
import { AppSettingsService } from '@shared/services';
import { TimeRangeValidationService } from '@shared/services/validators/time-range.service';

describe('CanActivateDateParams', () => {
    let service: CanActivateDateParams;
    const boardMinDate = new Date(2022, 4, 23);
    const boardMaxDate = new Date(2022, 4, 29);
    const toStartPageMock = jasmine.createSpy('toStartPage');
    const navigationService: Partial<OnlineBoardNavigationService> = {
        toStartPage: toStartPageMock,
    };

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [
                CanActivateDateParams,
                OnlineBoardDateValidationService,
                TimeRangeValidationService,
                OnlineBoardUrlParserService,
                {
                    provide: AppSettingsService,
                    useValue: {
                        getSettings: () => {
                            return Promise.resolve({
                                boardMinDate,
                                boardMaxDate,
                            });
                        },
                    },
                },
                {
                    provide: OnlineBoardNavigationService,
                    useValue: navigationService,
                },
            ],
        });

        toStartPageMock.calls.reset();
        service = TestBed.inject(CanActivateDateParams);
    });

    it('can activate with valid date', async () => {
        const validDate = new Date(2022, 4, 25);
        const result = await service.canActivate({
            date: validDate,
        });

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

    it('can activate with valid date and time range', async () => {
        const validDate = new Date(2022, 4, 25);
        const result = await service.canActivate({
            date: validDate,
            timeFrom: '0600',
            timeTo: '2200',
        });

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

    it('redirect to start page because of invalid date', async () => {
        const validDate = new Date(2022, 4, 21);
        const result = await service.canActivate({
            date: validDate,
        });

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

    it('redirect to start page because of invalid time range', async () => {
        const validDate = new Date(2022, 4, 25);
        const result = await service.canActivate({
            date: validDate,
            timeFrom: '1200',
            timeTo: '1000',
        });

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