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

interface FlightDetailsModel {
    showRegistration: boolean;
    showBoarding: boolean;
    showDeboarding: boolean;
    showMeal: boolean;
    showAircraft: boolean;
    showServices: boolean;
}

@Component({
    selector: 'flight-details-wrapper',
    templateUrl: './flight-details-wrapper.component.html',
    styleUrls: ['./flight-details-wrapper.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class FlightDetailsWrapperComponent implements OnChanges {
    @Input() leg: Leg;
    @Input() viewType: ViewType;
    @Input() flight: FlightModel;

    customActiveIndex;
    showDetails = true;
    model: FlightDetailsModel;

    ngOnChanges() {
        this.model = this.getVisibilityModel();
        this.showDetails = Object.keys(this.model)?.some(
            (key) => this.model[key],
        );
    }

    @HostBinding('class') get HeadingClass() {
        return this.customActiveIndex != null ? 'accordion--open' : '';
    }

    private getVisibilityModel(): FlightDetailsModel | null {
        if (!this.leg || !this.viewType) return null;

        return {
            showRegistration: this.showRegistration,
            showBoarding: this.showBoarding,
            showDeboarding: this.showDeboarding,
            showMeal: this.showMeal,
            showAircraft: this.showAircraft,
            showServices: this.showServices,
        };
    }

    get showRegistration(): boolean {
        const registration = this.leg.transition?.registration;

        return this.showBoardProperty(registration);
    }

    get showBoarding(): boolean {
        const boarding = this.leg.transition?.boarding;

        return this.showBoardProperty(boarding);
    }

    get showDeboarding(): boolean {
        const deboarding = this.leg.transition?.deboarding;

        return this.showBoardProperty(deboarding);
    }

    get showServices() {
        return this.leg.equipment.aircraft?.actual?.onBoardServices?.length > 0;
    }

    get showAircraft() {
        const aircraftInfo = this.leg.equipment.aircraft;
        const aircraft = aircraftInfo?.actual ?? aircraftInfo?.scheduled;

        return (
            !!aircraft?.title ||
            !!aircraftInfo?.configuration ||
            !!aircraftInfo?.actual
        );
    }

    get showMeal() {
        return this.leg.equipment.meal?.length > 0;
    }

    get isLegCanceled() {
        return this.leg.status === 'Cancelled';
    }

    private showBoardProperty(property: { status: string }): boolean {
        if (
            this.viewType === ViewType.Schedule ||
            this.isLegCanceled ||
            !property ||
            property?.status === 'Scheduled'
        )
            return false;

        return true;
    }
}
