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

@Injectable()
export class OnlineBoardTitleService {
    constructor(
        private translateService: TranslateService,
        private dictionaries: DictionariesService,
        private dateService: DateTransformerService,
    ) {}

    public getStartPageTitle() {
        return 'BOARD.TITLE';
    }

    public async getArrivalTitle(
        params: IOnlineBoardRoutePageUrlParams,
        withDate = true,
    ): Promise<string> {
        if (!params.arrival) {
            return '';
        }

        const titleBase = this.translateService.instant('BOARD.ARRIVAL');
        const city = await this.dictionaries.getCityOrAirportNameAsync(
            params.arrival,
        );

        if (!withDate) {
            return `${titleBase}: ${city}`;
        }

        const transformedDate = this.dateService.toString(params.date);
        return `${titleBase}: ${city}, ${transformedDate}`;
    }

    public async getDepartureTitle(
        params: IOnlineBoardRoutePageUrlParams,
        withDate = true,
    ): Promise<string> {
        if (!params.departure) {
            return '';
        }

        const titleBase = this.translateService.instant('BOARD.DEPARTURE');
        const city = await this.dictionaries.getCityOrAirportNameAsync(
            params.departure,
        );

        if (!withDate) {
            return `${titleBase}: ${city}`;
        }

        const transformedDate = this.dateService.toString(params.date);
        return `${titleBase}: ${city}, ${transformedDate}`;
    }

    public async getRouteTitle(
        params: IOnlineBoardRoutePageUrlParams,
        withDate = true,
    ): Promise<string> {
        if (!params.departure || !params.arrival) {
            return '';
        }

        const titleBase = this.translateService.instant('BOARD.ROUTE');
        const departure = await this.dictionaries.getCityOrAirportNameAsync(
            params.departure,
        );
        const arrival = await this.dictionaries.getCityOrAirportNameAsync(
            params.arrival,
        );

        if (!withDate) {
            return `${titleBase}: ${departure} - ${arrival}`;
        }

        const transformedDate = this.dateService.toString(params.date);
        return `${titleBase}: ${departure} - ${arrival}, ${transformedDate}`;
    }

    public getFlightNumberTitle(params: IParsedFlightId, withDate = true) {
        const titleBase = this.translateService.instant('SHARED.NUMBER');

        if (!withDate) {
            return `${titleBase}: ${params.carrier} ${params.flightNumber}${params.suffix}`;
        }

        const transformedDate = this.dateService.toString(params.date);
        return `${titleBase}: ${params.carrier} ${params.flightNumber}${params.suffix}, ${transformedDate}`;
    }
}
