import { Component, Input } from '@angular/core';
import { getAirline } from '@app/shared/airlines';
import { FlightModel, getFlights } from '@app/shared/models-legacy';
import { RouterHandlerService } from '@app/shared/services/router-handler.service';
import { FlightStatusLogic } from './flight-status.logic';

@Component({
    selector: 'flight-status-button',
    templateUrl: './flight-status-button.component.html',
    styleUrls: ['./flight-status-button.component.scss'],
})
export class FlightStatusButtonComponent {
    @Input() flight: FlightModel;
    @Input() small = false;
    window$ = window;
    constructor(private navigate: RouterHandlerService, private logic: FlightStatusLogic) {}
    open() {
        getFlights(this.flight).forEach((flight) => this.openOne(flight));
    }

    private openOne(flight: FlightModel) {
        if (!this.logic.available(flight)) return;

        const { canViewStatus, canViewNativeStatus, url } = getAirline(flight) || {};

        if (!canViewStatus) return;

        if (canViewNativeStatus) return this.navigate.toBoardDetailsWithBlank(flight);

        return this.window$.open(url, '_blank');
    }
}
