import { Component, Input, OnChanges } from '@angular/core';
import { TranslatePipe } from '@ngx-translate/core';
import { RouteType } from '@typings/enums';
import { IFlight } from '@typings/flight/flight';
import { UrlBuilderService } from '@shared/services/url/url-builder.service';
import { getFlightArrivalCity } from '@utils/flight/arrival/city';
import { getFlightDepartureCity } from '@utils/flight/departure/city';

@Component({
    selector: 'details-title-base',
    template: '',
})
export abstract class DetailsTitleBase implements OnChanges {
    @Input() flight: IFlight;
    title: string;

    protected constructor(
        protected translatePipe: TranslatePipe,
        protected urlService: UrlBuilderService,
    ) {}

    ngOnChanges() {
        this.title = this.translate();
    }

    protected abstract get titleKey(): string;
    protected abstract get pluralTitleKey(): string;
    protected abstract translatePluralTitle(): string;
    protected abstract translateTitle(): string;

    protected get base() {
        return this.isConnecting
            ? this.translatePipe.transform(this.pluralTitleKey)
            : this.translatePipe.transform(this.titleKey);
    }

    protected get flightInfo() {
        if (this.flight.routeType !== RouteType.CONNECTING) {
            return this.urlService.formatFlightId(this.flight.flightId);
        }

        return this.flight.flights
            .map((flight) => this.urlService.formatFlightId(flight.flightId))
            .join(', ');
    }

    protected get departure() {
        return getFlightDepartureCity(this.flight);
    }

    protected get arrival() {
        return getFlightArrivalCity(this.flight);
    }

    private translate(): string {
        if (!this.flight) {
            return '';
        }

        return this.isConnecting
            ? this.translatePluralTitle()
            : this.translateTitle();
    }

    private get isConnecting() {
        return this.flight.routeType === RouteType.CONNECTING;
    }
}
