import { ChangeDetectionStrategy, Component, Input, OnChanges, SimpleChanges, ViewEncapsulation } from '@angular/core';
import { FlightModel } from '@app/shared/models-legacy';
import { Language } from '@shared/enumerators';
import { LocalizationService } from '@shared/services';

@Component({
    selector: 'operator-logo',
    templateUrl: './operator-logo.component.html',
    styleUrls: ['./operator-logo.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush,
    encapsulation: ViewEncapsulation.None,
})
export class OperatorLogoComponent implements OnChanges {
    @Input() flight: FlightModel;
    @Input() round = false;
    @Input() large = false;
    @Input() caption = false;
    operatingBy: string;
    classes = {
        round: false,
        large: false,
        ru: false,
    };

    constructor(private localization: LocalizationService) {}

    ngOnChanges(changes: SimpleChanges) {
        if (this.flight && 'flight' in changes) {
            const { actual, scheduled } = this.flight.operatingBy || {};
            this.operatingBy = (actual || scheduled)?.replace('/', '');
        }

        this.classes = this.computeClasses();
    }

    private computeClasses() {
        return {
            round: this.round,
            large: this.large,
            ru: this.localization.Language === Language.ru,
            ...this.getLogoClass(this.operatingBy),
        };
    }

    private getLogoClass(operatingBy?: string) {
        return operatingBy
            ? {
                  [`company-logo--${operatingBy}`]: true,
              }
            : {};
    }
}
