import { Component, HostBinding, Input, OnChanges } from '@angular/core';
import { RouteTypeLegacy } from '@app/shared/enumerators';
import { ViewType } from '@app/shared/enumerators/flight-request-type.enum';
import { ITimesModel, Leg, Transfer, TransferLogic } from '@shared/models-legacy';
import { FlightModel } from '@shared/models-legacy/flight.model';

@Component({
    selector: 'transfer',
    templateUrl: './transfer.component.html',
    styleUrls: ['./transfer.component.scss'],
})
export class TransferComponent implements OnChanges {
    @HostBinding('class.separated') get separated() {
        return this.transfer?.type === RouteTypeLegacy.Connecting;
    }
    constructor(private logic: TransferLogic) {}

    @Input() flight?: FlightModel;
    @Input() nextFlight?: FlightModel;
    @Input() leg?: Leg;
    @Input() viewType: ViewType;

    RouteType = RouteTypeLegacy;
    ViewType = ViewType;
    transfer: Transfer;

    ngOnChanges() {
        this.transfer = this.logic.getTransfer(this.flight, this.nextFlight, this.leg);
    }

    getTimes(times: ITimesModel) {
        const { latest, scheduled } = times;

        if (this.viewType === ViewType.Schedule) {
            return scheduled;
        }

        return latest || scheduled;
    }

    get arrivalTimes() {
        return this.getTimes(this.transfer.arrival.times);
    }

    get departureTimes() {
        return this.getTimes(this.transfer.departure.times);
    }
}
