import { Injectable } from '@angular/core';
import { UrlParserService } from '@shared/services/url/url-parser.service';
import {
    IOnlineBoardRoutePageUrlParams,
    IOnlineBoardStationPageUrlParams,
} from './types';

@Injectable()
export class OnlineBoardUrlParserService extends UrlParserService {

    parseArrivalSearchUrlParams(
        params: string,
    ): IOnlineBoardRoutePageUrlParams {
        const stationParams = this.parseStationSearchUrlParams(params);

        return {
            arrival: stationParams.station,
            date: stationParams.date,
            timeFrom: stationParams.timeFrom,
            timeTo: stationParams.timeTo,
        };
    }

    parseDepartureSearchUrlParams(
        params: string,
    ): IOnlineBoardRoutePageUrlParams {
        const stationParams = this.parseStationSearchUrlParams(params);

        return {
            departure: stationParams.station,
            date: stationParams.date,
            timeFrom: stationParams.timeFrom,
            timeTo: stationParams.timeTo,
        };
    }

    parseRouteSearchUrlParams(params: string): IOnlineBoardRoutePageUrlParams {
        const [departure, arrival, date, time] = params.split('-');

        let timeRange = this.parseTimeRange(time);

        return {
            arrival,
            departure,
            date: this.parseDate(date),
            ...timeRange,
        };
    }

    parseStationSearchUrlParams(
        params: string,
    ): IOnlineBoardStationPageUrlParams {
        const [station, date, time] = params.split('-');

        return {
            station,
            date: this.parseDate(date),
            ...this.parseTimeRange(time),
        };
    }
}
