import { ChangeDetectionStrategy, Component, Input, OnChanges, ViewEncapsulation } from '@angular/core';
import { ArrivalTimesModel, DepartureTimesModel, Duration, getDuration } from '@app/shared/models-legacy';

@Component({
    selector: 'transfer-time',
    templateUrl: './transfer-time.component.html',
    styleUrls: ['./transfer-time.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush,
    encapsulation: ViewEncapsulation.None,
})
export class TransferTimeComponent implements OnChanges {
    @Input() arrival: ArrivalTimesModel;
    @Input() departure: DepartureTimesModel;
    @Input() type: 'actual' | 'scheduled';
    duration: Duration;

    ngOnChanges() {
        const { type, arrival, departure } = this;
        if (!arrival || !departure) return;

        const first = type === 'actual' ? arrival?.latest?.utc || arrival?.scheduled?.utc : arrival?.scheduled?.utc;
        const last = type === 'actual' ? departure?.latest?.utc || departure?.scheduled?.utc : departure?.scheduled?.utc;

        this.duration = getDuration(first, last);
    }
}
