import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve } from '@angular/router';
import { IBreadCrumbItem } from '@components/page/breadcrumds/page-breadcrumbs.component';
import { OnlineBoardBreadCrumbService } from '@online-board/services/bread-crumb.service';
import { OnlineBoardRequestDataResolver } from '../request-data.resolver';
import { IOnlineBoardRequest } from '@online-board/services/request/request-parser.service';

@Injectable()
export class OnlineBoardDetailsBreadCrumbsResolver
    implements Resolve<IBreadCrumbItem[]>
{
    constructor(
        private breadCrumbService: OnlineBoardBreadCrumbService,
        private requestDataResolver: OnlineBoardRequestDataResolver,
    ) {}

    async resolve(route: ActivatedRouteSnapshot) {
        const requestData = this.requestDataResolver.resolve(route);
        const items: IBreadCrumbItem[] = [
            this.breadCrumbService.getRootBreadCrumb(),
        ];

        if (requestData) {
            const item = await this.getBreadCrumbItem(requestData.parsed);
            items.push(item);
        }

        return items;
    }

    private getBreadCrumbItem(
        request: IOnlineBoardRequest,
    ): Promise<IBreadCrumbItem> {
        switch (request.type) {
            case 'flight-number': {
                const item =
                    this.breadCrumbService.getFlightNumberSearchBreadCrumb(
                        request.params,
                    );

                return Promise.resolve(item);
            }
            case 'route': {
                return this.breadCrumbService.getRouteSearchBreadCrumb(
                    request.params,
                );
            }
        }
    }
}
