import {
    Component,
    EventEmitter,
    Inject,
    Input,
    OnChanges,
    Output,
    SimpleChanges,
} from '@angular/core';
import { RouteTypeLegacy } from '@shared/enumerators';
import { ViewType } from '@shared/enumerators/flight-request-type.enum';
import { AppSettings, FlightModel, Leg } from '@shared/models-legacy';
import DeprecatedFlightUtils from '@shared/models-legacy/get-flight.strategy';
import { APP_SETTINGS } from '@shared/services';
import { ISimpleFlight } from '@typings/flight/flight';

@Component({
    selector: 'online-board-details-view',
    templateUrl: './online-board-details.component.html',
    styleUrls: ['./online-board-details.component.scss'],
})
export class OnlineBoardDetailsViewComponent implements OnChanges {
    @Input() flightLegacy: FlightModel;
    @Input() flightsLegacy: FlightModel[];
    @Input() flight: ISimpleFlight;
    @Input() loading = false;
    @Input() searchDate: Date;

    @Output() dateChange = new EventEmitter<Date>();
    @Output() open = new EventEmitter<ISimpleFlight>();

    ViewType = ViewType;
    RouteType = RouteTypeLegacy;
    firstLeg: Leg;

    constructor(@Inject(APP_SETTINGS) public settings: AppSettings) {}

    ngOnChanges(changes: SimpleChanges) {
        if ('flight' in changes) {
            this.firstLeg = DeprecatedFlightUtils.getFirstLeg(
                this.flightLegacy,
            );
        }
    }

    handleOpenEvent(flight: ISimpleFlight) {
        // Click on the currently opened flight in the list will cause
        // infinite loading state
        if (this.flightLegacy.id === flight.id) {
            return;
        }

        this.open.emit(flight);
    }

    handleDateChanged(date: Date) {
        this.dateChange.emit(date);
    }
}
