import { Injectable } from '@angular/core';
import { TranslatePipe } from '@ngx-translate/core';
import { DictionariesService } from '@modules/components/page-filters/services/dictionaries-service';
import { DateTransformerService } from '@shared/services/date/date-transformer.service';
import { IParsedFlightId } from '@typings/flight/flight-id';
import { IOnlineBoardRoutePageUrlParams } from './url';

type IMetaTags = {
    title: string;
    description: string;
};
@Injectable()
export class OnlineBoardMetaTagsService {
    constructor(
        private translatePipe: TranslatePipe,
        private dictionaries: DictionariesService,
        private dateService: DateTransformerService,
    ) {}

    public async getArrivalMetaTags(
        params: IOnlineBoardRoutePageUrlParams,
    ): Promise<IMetaTags> {
        return this.getStationMetaTags(params, 'arrival');
    }

    public async getDepartureMetaTags(
        params: IOnlineBoardRoutePageUrlParams,
    ): Promise<IMetaTags> {
        return this.getStationMetaTags(params, 'departure');
    }

    public async getRouteMetaTags(
        params: IOnlineBoardRoutePageUrlParams,
    ): Promise<IMetaTags> {
        if (!params.arrival || !params.departure) {
            return {
                title: '',
                description: '',
            };
        }

        const arrivalCity = await this.dictionaries.getCityOrAirportNameAsync(
            params.arrival,
        );
        const departureCity = await this.dictionaries.getCityOrAirportNameAsync(
            params.departure,
        );
        const date = this.dateService.toString(params.date);

        const title = this.translatePipe.transform(
            `SEO.BOARD.ROUTE-SEARCH.TITLE`,
            {
                departureCity,
                arrivalCity,
                date,
            },
        );

        const description = this.translatePipe.transform(
            `SEO.BOARD.ROUTE-SEARCH.DESCRIPTION`,
            {
                departureCity,
                arrivalCity,
                date,
            },
        );

        return {
            title,
            description,
        };
    }

    public getFlightNumberMetaTags(params: IParsedFlightId): IMetaTags {
        const { carrier, flightNumber, suffix, date } = params;
        const title = this.translatePipe.transform(
            `SEO.BOARD.FLIGHT-SEARCH.TITLE`,
            {
                flightNumber: `${carrier} ${flightNumber}${suffix}`,
                date: this.dateService.toString(date),
            },
        );
        const description = this.translatePipe.transform(
            `SEO.BOARD.FLIGHT-SEARCH.DESCRIPTION`,
            {
                flightNumber: `${carrier} ${flightNumber}${suffix}`,
                date: this.dateService.toString(date),
            },
        );

        return {
            title,
            description,
        };
    }

    private async getStationMetaTags(
        params: IOnlineBoardRoutePageUrlParams,
        key: 'departure' | 'arrival',
    ): Promise<IMetaTags> {
        if (!params[key]) {
            return {
                title: '',
                description: '',
            };
        }

        const city = await this.dictionaries.getCityOrAirportNameAsync(
            params[key],
        );
        const date = this.dateService.toString(params.date);

        const title = this.translatePipe.transform(
            `SEO.BOARD.${key.toUpperCase()}-SEARCH.TITLE`,
            {
                [`${key}City`]: city,
                date,
            },
        );

        const description = this.translatePipe.transform(
            `SEO.BOARD.${key.toUpperCase()}-SEARCH.DESCRIPTION`,
            {
                [`${key}City`]: city,
                date,
            },
        );

        return {
            title,
            description,
        };
    }
}
