import {
    ChangeDetectionStrategy,
    Component,
    EventEmitter,
    Input,
    OnChanges,
    Output,
    SimpleChanges,
} from '@angular/core';
import { ViewType } from '@app/shared/enumerators/flight-request-type.enum';
import { FlightModel } from '@app/shared/models-legacy';
import { IFlight } from '@typings/flight/flight';
import { BuyTicketLogic } from '../buy-ticket-button/buy-ticket.logic';
import { RegistrationLogic } from '../registration-button/registration.logic';
import { FlightStatusLogic } from '../flight-status-button/flight-status.logic';
@Component({
    selector: 'flight-actions',
    templateUrl: './flight-actions.component.html',
    styleUrls: ['./flight-actions.component.scss'],
    host: {
        class: 'p-print-none',
    },
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class FlightActionsComponent implements OnChanges {
    @Input() print = false; // hidden in scope of 7541
    @Input() share = true;
    @Input() details = true;
    @Input() register = true;
    @Input() status = true;
    @Input() buy = true;

    @Input() viewType: ViewType;
    @Input() flight: FlightModel;
    @Input() wide = false;

    @Output() toDetails = new EventEmitter<IFlight>();

    canBuy: boolean;
    canRegister: boolean;
    canViewStatus: boolean;

    constructor(
        private logic: BuyTicketLogic,
        private registration: RegistrationLogic,
        private statuses: FlightStatusLogic,
    ) {}

    ngOnChanges(changes: SimpleChanges) {
        if (this.flight && 'flight' in changes) {
            this.canBuy = this.logic.isAnyAvailable(this.flight);
            this.canRegister = this.registration.isAnyAvailable(this.flight);
            this.canViewStatus = this.statuses.available(this.flight);
        }
    }

    handleToDetailsEvent(flight: IFlight) {
        this.toDetails.emit(flight);
    }
}
