import { ChangeDetectionStrategy, Component, Input, OnChanges, SimpleChanges } from '@angular/core';
import { RouteTypeLegacy } from '@app/shared/enumerators';
import { FlightModel, getFirstLeg, getFlights, getLastLeg, Leg, ListItem } from '@app/shared/models-legacy';

@Component({
    selector: 'schedule-list-flight-header',
    templateUrl: './schedule-list-flight-header.component.html',
    styleUrls: [
        './schedule-list-flight-header.component.scss',
        './schedule-list-flight-header.component.mobile.scss',
        './schedule-list-flight-header.component.tablets.scss',
    ],
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ScheduleListFlightHeaderComponent implements OnChanges {
    RouteType = RouteTypeLegacy;
    @Input() flight: ListItem<FlightModel>;
    @Input() expanded = false;
    flights: FlightModel[];
    departure: Leg;
    arrival: Leg;
    connecting: boolean;
    direct: boolean;
    routeType: RouteTypeLegacy;

    ngOnChanges(changes: SimpleChanges) {
        if (this.flight && 'flight' in changes) {
            this.flights = getFlights(this.flight);
            this.departure = getFirstLeg(this.flight);
            this.arrival = getLastLeg(this.flight);
            this.connecting = this.flight.routeType === RouteTypeLegacy.Connecting;
            this.direct = this.flight.routeType === RouteTypeLegacy.Direct;
            this.routeType = this.flight.routeType;
        }
    }

    get arrowIconClasses() {
        return {
            'arrow-icon': true,
            'arrow-icon--visible': this.expanded,
        };
    }
}
