import { Injectable } from '@angular/core';
import {
    IOnlineBoardUrlDateParams,
    IOnlineBoardRoutePageUrlParams,
} from '@online-board/services/url/types';
import { OnlineBoardUrlParserService } from '@online-board/services/url/url-parser.service';
import { IParsedFlightId } from '@typings/flight/flight-id';
import { IOnlineBoardRequestMode } from './types';

type IRouteRequest = {
    type: 'route';
    params: IOnlineBoardRoutePageUrlParams;
};

type IFlightNumberRequest = {
    type: 'flight-number';
    params: IParsedFlightId;
};
export type IOnlineBoardRequest = IRouteRequest | IFlightNumberRequest;

@Injectable()
export class OnlineBoardRequestParserService {
    constructor(private urlService: OnlineBoardUrlParserService) {}

    parse(request: string): IOnlineBoardRequest {
        //first param is always 'onlineboard' so it's skipped
        const [, mode, ...rest] = request.split('-');

        return this.parseByMode(mode as IOnlineBoardRequestMode, rest);
    }

    private parseByMode(
        mode: IOnlineBoardRequestMode,
        requestParams: string[],
    ): IOnlineBoardRequest {
        switch (mode) {
            case 'arrival': {
                return this.parseArrivalRequest(requestParams);
            }
            case 'departure': {
                return this.parseDepartureRequest(requestParams);
            }
            case 'route': {
                return this.parseRouteRequest(requestParams);
            }
            case 'flight': {
                return this.parseFlightRequest(requestParams);
            }
        }
    }

    private parseArrivalRequest(requestParams: string[]): IRouteRequest {
        const [arrival, date, timeRange] = requestParams;

        return {
            type: 'route',
            params: {
                arrival,
                ...this.parseDatePart(date, timeRange),
            },
        };
    }

    private parseDepartureRequest(requestParams: string[]): IRouteRequest {
        const [departure, date, timeRange] = requestParams;

        return {
            type: 'route',
            params: {
                departure,
                ...this.parseDatePart(date, timeRange),
            },
        };
    }

    private parseRouteRequest(requestParams: string[]): IRouteRequest {
        const [departure, arrival, date, timeRange] = requestParams;

        return {
            type: 'route',
            params: {
                departure,
                arrival,
                ...this.parseDatePart(date, timeRange),
            },
        };
    }

    private parseFlightRequest(requestParams: string[]): IFlightNumberRequest {
        return {
            type: 'flight-number',
            params: this.urlService.parseFlightId(requestParams.join('-')),
        };
    }

    private parseDatePart(
        date: string,
        timeRangeStr?: string,
    ): IOnlineBoardUrlDateParams {
        return {
            date: this.urlService.parseDate(date),
            ...this.urlService.parseTimeRange(timeRangeStr),
        };
    }
}
