import { Injectable } from '@angular/core';
import { IParsedFlightId } from '@typings/flight/flight-id';
import * as moment from 'moment';

@Injectable()
export class OnlineBoardFlightNumberFilterValidationService {
    flightNumberError: string | null = null;
    dateError: string | null = null;

    validate(params: Partial<IParsedFlightId>) {
        this.dateError = this.flightNumberError = null;

        if (!params.flightNumber) {
            this.flightNumberError = 'BOARD.FLIGHT_NUMBER-ERROR-EMPTY';
            return false;
        }

        const reg = new RegExp('^\\d\\d\\d\\d?[A-Za-z]?$');
        if (!reg.test(params.flightNumber) || params.flightNumber.length > 5) {
            this.flightNumberError = 'BOARD.FLIGHT_NUMBER-ERROR-ONLY-NUMBER';
            return false;
        }

        if (!params.date || !moment(params.date).isValid()) {
            this.dateError = 'SHARED.DATE_FORMAT-WRONG';
            return false;
        }

        return true;
    }
}
