import {
    ChangeDetectionStrategy,
    Component,
    Input,
    OnChanges,
    SimpleChanges,
} from '@angular/core';
import { RouteTypeLegacy } from '@app/shared/enumerators';
import { FlightModel, getLegs, Leg } from '@app/shared/models-legacy';
import { FlightStatusLogic } from '../flight-status-button/flight-status.logic';

@Component({
    selector: 'details-header-badge',
    templateUrl: './details-header-badge.component.html',
    styleUrls: ['./details-header-badge.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DetailsHeaderBadgeComponent implements OnChanges {
    @Input() flight: FlightModel;
    @Input() canShowStatus = true;
    @Input() icons = { round: true, large: false };
    shouldShowStatus: boolean;
    RouteType = RouteTypeLegacy;
    legs: Leg[];
    showCodeSharing: boolean;

    constructor(private statusLogic: FlightStatusLogic) {}

    async ngOnChanges(changes: SimpleChanges) {
        if (this.flight && 'flight' in changes) {
            this.legs = getLegs(this.flight);
            this.shouldShowStatus = this.statusLogic.available(this.flight);
            this.showCodeSharing = this.hasCodeSharing();
        }
    }

    private hasCodeSharing() {
        return this.legs.some((leg) => leg.hasCodesharing);
    }
}
