import {
    ChangeDetectorRef,
    Component,
    Input,
    OnChanges,
    OnInit
} from '@angular/core';
import {MealType} from '@app/shared/models-legacy/meal.model';
import {
    MealApiRequestModel,
    SpecialMealCabinType,
    SpecialMealProviderService
} from "@shared/services/special-meal-provider.service";
import {FlightModel, Leg} from "@shared/models-legacy";
import {catchError} from "rxjs/operators";
import {of} from "rxjs";

@Component({
    selector: 'flight-details-meal',
    templateUrl: './flight-details-meal.component.html',
    styleUrls: ['./flight-details-meal.component.scss']
})
export class FlightDetailsMealComponent implements OnInit, OnChanges{
    @Input() leg: Leg;
    @Input() flight: FlightModel;

    public economyLink = 'https://www.aeroflot.ru/ru-ru/information/onboard/dining?0000#meal-type_0';
    public comfortLink = 'https://www.aeroflot.ru/ru-ru/information/onboard/dining?0000#meal-type_1';
    public businessLink = 'https://www.aeroflot.ru/ru-ru/information/onboard/dining?0000#meal-type_2';
    public specialLink = 'https://www.aeroflot.ru/ru-ru/information/onboard/dining?0000#meal-type_3';

    public isLoading: boolean;
    public hasSpecialMeal: boolean;

    constructor(private specialMealProvider: SpecialMealProviderService, private cdr: ChangeDetectorRef) {
    }

    ngOnInit(): void {
    }

    ngOnChanges(){
        this.checkSpecialMeal();
    }

    // TODO: 7350
    public get hasEconomyMeal() {
        return this.hasMealType(MealType.Economy);
    }

    // TODO: 7350
    public get hasComfortMeal() {
        return this.hasMealType(MealType.Comfort);
    }

    // TODO: 7350
    public get hasBusinessMeal() {
        return this.hasMealType(MealType.Business);
    }

    // TODO: 7350
    // public get hasSpecialMeal() {
    //     return this.hasMealType(MealType.Special);
    // }

    // TODO: 7350
    private hasMealType(type: MealType): boolean {
        if (!this.leg.equipment.meal || this.leg.equipment.meal.length === 0) {
            return false;
        }

        const existMeal = this.leg.equipment.meal ? this.leg.equipment.meal.find((t) => t.type == type) : null;
        return !!existMeal;
    }

    private checkSpecialMeal() {
        let cabin = SpecialMealCabinType.none;

        if(this.hasEconomyMeal){
            cabin |= SpecialMealCabinType.econom;
        }

        if(!this.hasEconomyMeal && this.hasBusinessMeal){
            cabin |= SpecialMealCabinType.business;
        }

        if(cabin === SpecialMealCabinType.none){
            this.hasSpecialMeal = false;
            return;
        }

        this.isLoading = true;
        
        const mealModel: MealApiRequestModel = {
            airline: this.flight.flightId.carrier,
            cabin: cabin,
            flightDateTime: this.leg.departure.times.scheduled.utc,
            number: this.flight.flightId.flightNumber,
            origin: this.leg.departure.scheduled.airportCode,
            destination: this.leg.arrival.scheduled.airportCode
        };

        this.specialMealProvider.HasSpecialMeal(mealModel)
            .pipe(
                catchError(error => {
                    console.error('Произошла ошибка при запросе к API:', error);
                    this.isLoading = false;
                    return of(false);
                })
            )
            .subscribe(hasSpecialMeal => {
                this.hasSpecialMeal = hasSpecialMeal;
                this.isLoading = false;
                this.cdr.detectChanges();
            });
    }

}
