import { Injectable } from '@angular/core';
import * as moment from 'moment';

@Injectable({
    providedIn: 'root',
})
export class TimeRangeValidationService {
    /**
     * timeFrom - four digit string indicating hours and minutes: form 0000 to 2359
     * timeTo - four digit string indicating hours and minutes: form 0000 to 2359
     *
     * */
    public isTimeRangeValid(timeFrom?: string, timeTo?: string) {
        if (!timeFrom && !timeTo) {
            return true; // time is optional in all urls and requests so undefined timeFrom and timeTo is valid.
        }

        if (!timeFrom || !timeTo) {
            return false; // Both parts must be defined or undefined simultaneously.
        }

        const timeFromMoment = moment(timeFrom, 'HHmm');
        const timeToMoment = moment(timeTo, 'HHmm');

        return timeFromMoment.isBefore(timeToMoment);
    }
}
