import { Component, Inject } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { MetaBaseComponent } from '@components/meta-base/meta-base.component';
import { DictionariesService } from '@modules/components/page-filters/services/dictionaries-service';
import { TranslatePipe } from '@ngx-translate/core';
import { ScheduleUrlParserService } from '@schedule/services/url/url-parser.service';
import { FLIGHTS_INFO_PARAM } from '@schedule/services/url/flights-info-param';
import { DateTransformerService } from '@shared/services/date/date-transformer.service';

@Component({
    selector: 'schedule-flight-details-meta-tags',
    templateUrl: './schedule-flight-details-meta-tags.component.html',
})
export class ScheduleFlightDetailsMetaTagsComponent extends MetaBaseComponent {
    constructor(
        protected route: ActivatedRoute,
        protected dictionaries: DictionariesService,
        private urlService: ScheduleUrlParserService,
        private dateService: DateTransformerService,
        private translatePipe: TranslatePipe,
        @Inject(FLIGHTS_INFO_PARAM) private flightsInfoParamName,
    ) {
        super(route, dictionaries);
        this.paramsName = this.flightsInfoParamName;
    }

    private translate(tag: 'TITLE' | 'DESCRIPTION', params: string) {
        const flightsIds = this.urlService.parseFlightDetailsParams(params, null, null); // here we do not need departure/arrival airports

        const { flightNumber, carrier, date } = flightsIds.flights[0];
        const transformedDate = this.dateService.toString(date);

        return this.translatePipe.transform(
            `SEO.SCHEDULE.FLIGHT-DETAILS.${tag}`,
            {
                flightNumber: `${carrier} ${flightNumber}`,
                date: transformedDate,
            },
        );
    }

    protected translateTitle(params: string): string {
        return this.translate('TITLE', params);
    }

    protected translateDescription(params: string): string {
        return this.translate('DESCRIPTION', params);
    }
}
