import { Injectable } from '@angular/core';
import { IBreadCrumbItem } from '@components/page/breadcrumds/page-breadcrumbs.component';
import { IParsedFlightId } from '@typings/flight/flight-id';
import { OnlineBoardTitleService } from './title.service';
import {
    IOnlineBoardRoutePageUrlParams,
    OnlineBoardUrlBuilderService,
} from './url';

@Injectable()
export class OnlineBoardBreadCrumbService {
    constructor(
        private titleService: OnlineBoardTitleService,
        private urlBuilder: OnlineBoardUrlBuilderService,
    ) {}

    getRootBreadCrumb(): IBreadCrumbItem {
        return {
            url: this.urlBuilder.getStartPageUrl(),
            label: this.titleService.getStartPageTitle(),
        };
    }

    getFlightNumberSearchBreadCrumb(params: IParsedFlightId): IBreadCrumbItem {
        return {
            label: this.titleService.getFlightNumberTitle(params, false),
            url: this.urlBuilder.getFlightNumberPageUrl(params),
        };
    }

    async getRouteSearchBreadCrumb(
        params: IOnlineBoardRoutePageUrlParams,
    ): Promise<IBreadCrumbItem> {
        const label = await this.getRouteSearchBreadCrumbLabel(params);

        return {
            label,
            url: this.urlBuilder.getRoutePageUrl(params),
        };
    }

    private async getRouteSearchBreadCrumbLabel(
        params: IOnlineBoardRoutePageUrlParams,
    ): Promise<string> {
        if (params.arrival && !params.departure) {
            return this.titleService.getArrivalTitle(params, false);
        }

        if (!params.arrival && params.departure) {
            return this.titleService.getDepartureTitle(params, false);
        }

        return this.titleService.getRouteTitle(params, false);
    }
}
