import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { RootNavigationService } from '@shared/services/navigation.service';
import { IFlightId, IParsedFlightId } from '@typings/flight/flight-id';
import { IOnlineBoardRoutePageUrlParams } from './url/types';
import { OnlineBoardUrlBuilderService } from './url/url-builder.service';

export type IDetailsPageNavigationParams = {
    flightId: IFlightId;
    date: Date;
    request?: string;
};

@Injectable()
export class OnlineBoardNavigationService extends RootNavigationService {
    constructor(
        protected router: Router,
        private urlService: OnlineBoardUrlBuilderService,
    ) {
        super(router);
    }

    public toStartPage() {
        const url = this.urlService.getStartPageUrl();

        return this.router.navigateByUrl(url);
    }

    public toDetailsPage(params: IDetailsPageNavigationParams) {
        const { flightId, date, request } = params;
        const url = this.urlService.getDetailsPageUrl(flightId, date);

        return this.router.navigate([url], {
            queryParams: {
                request,
            },
        });
    }

    public toFlightNumberPage(params: IParsedFlightId) {
        const url = this.urlService.getFlightNumberPageUrl(params);

        return this.router.navigateByUrl(url);
    }

    public toRoutePage(params: IOnlineBoardRoutePageUrlParams) {
        if (!params.departure && !params.arrival) {
            return;
        }

        const url = this.urlService.getRoutePageUrl(params);

        return this.router.navigateByUrl(url);
    }
}
