import { Pipe, PipeTransform } from '@angular/core';
import { TranslatePipe } from '@ngx-translate/core';
import * as moment from 'moment';
import { IArrivalStation, IDepartureStation } from '../models-legacy';

@Pipe({
    name: 'transferTime',
})
export class TransferTimePipe implements PipeTransform {
    constructor(private translate: TranslatePipe) {}
    transform({ arrival, departure }: { arrival: IArrivalStation; departure: IDepartureStation }, type: 'actual' | 'scheduled'): string {
        if (!arrival || !departure) return '';

        const first =
            type === 'actual'
                ? moment(arrival?.times?.latest?.utc || arrival?.times?.scheduled?.utc)
                : moment(arrival?.times?.scheduled?.utc);
        const last =
            type === 'actual'
                ? moment(departure?.times?.latest?.utc || departure?.times?.scheduled?.utc)
                : moment(departure?.times?.scheduled?.utc);
        const span = moment.duration(last.diff(first));
        const hours = span.hours();
        const minutes = span.minutes();

        if (span < moment.duration(15, 'minutes') || hours < 0 || minutes < 0) {
            return this.translate.transform('FLIGHT-STATUSES.Unknown');
        }

        return `${hours}${this.translate.transform('SHARED.SHORT-HOUR')} ${minutes}${this.translate.transform('SHARED.SHORT-MIN')}`;
    }
}
