import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate } from '@angular/router';
import { CanActivateDateParams } from '@online-board/guards/utils/can-activate-date-params';
import { OnlineBoardUrlParserService } from '@online-board/services/url/url-parser.service';
import { CanActivateRedirrect } from './utils/can-activate-redirrect-to-not-found';
import { OnlineBoardNavigationService } from '../services/navigation.service';

@Injectable()
export class CanActivateFlightNumberSearch implements CanActivate {
    constructor(
        private urlService: OnlineBoardUrlParserService,
        private dateParamsService: CanActivateDateParams,
        private canRedirrect: CanActivateRedirrect,
        private navigationService: OnlineBoardNavigationService,
    ) {}

    async canActivate(route: ActivatedRouteSnapshot) {
        const urlParams = this.urlService.parseFlightId(route.params.params);

        if (!this.canRedirrect.validateFlightNumber(urlParams.flightNumber)) {
            return this.navigationService.toNotFound();
        }

        return this.dateParamsService.canActivate(urlParams);
    }
}
