import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { SearchHistoryService } from '@shared/services/history/search-history.service';
import {
    IOnlineBoardRoutePageUrlParams,
    OnlineBoardUrlBuilderService,
} from '@online-board/services/url';
import { IParsedFlightId } from '@typings/flight/flight-id';

@Injectable()
export class OnlineBoardFilterService {
    constructor(
        private router: Router,
        private urlService: OnlineBoardUrlBuilderService,
        private historyService: SearchHistoryService,
    ) {}

    toFlightNumberPage(params: IParsedFlightId) {
        // inlined toFlightNumberPage from OnlineBoardNavigationService
        // to get access to url
        const url = this.urlService.getFlightNumberPageUrl(params);

        this.router.navigateByUrl(url).then((navigated) => {
            if (navigated) {
                this.historyService.add({
                    type: 'flight-number',
                    url,
                    params,
                });
            }
        });
    }

    toRoutePage(params: IOnlineBoardRoutePageUrlParams) {
        // inlined toRoutePage from OnlineBoardNavigationService
        // to get access to url
        if (!params.departure && !params.arrival) {
            return;
        }

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

        this.router.navigateByUrl(url).then((navigated) => {
            if (navigated) {
                this.historyService.add({
                    type: 'board-route',
                    url,
                    params,
                });
            }
        });
    }
}
