import {
    Component,
    EventEmitter,
    Input,
    OnChanges,
    Output,
    SimpleChanges,
} from '@angular/core';
import {
    FlightModel,
    getFirstLeg,
    getLastLeg,
    getLegs,
    getStrategy,
    IArrivalStation,
    IDepartureStation,
    Leg,
} from '@shared/models-legacy';
import { IFlight } from '@typings/flight/flight';

@Component({
    selector: 'connecting-flight-body',
    templateUrl: './connecting-flight-body.component.html',
    styleUrls: ['./connecting-flight-body.component.scss'],
})
export class ScheduleSearchResultConnectingFlightBodyComponent
    implements OnChanges
{
    @Input() flight: FlightModel;
    @Output() toDetails = new EventEmitter<IFlight>();

    legs: Leg[];
    arrival: IArrivalStation;
    departure: IDepartureStation;

    ngOnChanges(changes: SimpleChanges) {
        if (this.flight && 'flight' in changes) {
            this.arrival = getLastLeg(this.flight).arrival;
            this.departure = getFirstLeg(this.flight).departure;
            this.legs = getLegs(this.flight);
        }
    }

    getFlightLegs(flight: FlightModel): Leg[] {
        return getStrategy(flight).getLegs(flight);
    }

    handleToDetailsEvent(flight: IFlight) {
        this.toDetails.emit(flight);
    }
}
