import { TestBed } from '@angular/core/testing';
import { IScheduleRouteDirectionParams } from '@schedule/services/url/types';
import { SCHEDULE_URL_BASE } from '@schedule/services/url/url-base';
import { ScheduleUrlBuilderService } from '@schedule/services/url/url-builder.service';
import { RouteType } from '@typings/enums';
import { IFlight } from '@typings/flight/flight';

describe('ScheduleUrlBuilderService', () => {
    let service: ScheduleUrlBuilderService;

    beforeEach(() => {
        TestBed.configureTestingModule({
            providers: [
                ScheduleUrlBuilderService,
                {
                    provide: SCHEDULE_URL_BASE,
                    useValue: 'schedule',
                },
            ],
        });
        service = TestBed.inject(ScheduleUrlBuilderService);
    });

    it('start page url should be "schedule"', () => {
        expect(service.getStartPageUrl()).toBe('schedule');
    });

    it('should return details page url for non connecting flight', () => {
        const flight: Partial<IFlight> = {
            routeType: RouteType.DIRECT,
            flightId: {
                flightNumber: '0012',
                carrier: 'SU',
                date: '2022-05-01',
            },
            leg: {
                departure: {
                    times: {
                        scheduledDeparture: {
                            local: '2022-05-27T19:00:00+03:00',
                        },
                    },
                },
            } as any,
        };

        const url = service.getDetailsPageUrl(flight as any);
        expect(url).toBe('schedule/SU0012-20220527');
    });

    it('should return details page url for connecting flight', () => {
        const firstFlight: Partial<IFlight> = {
            routeType: RouteType.DIRECT,
            flightId: {
                flightNumber: '0012',
                carrier: 'SU',
                date: '2022-05-01',
            },
            leg: {
                departure: {
                    times: {
                        scheduledDeparture: {
                            local: '2022-05-01T19:00:00+03:00',
                        },
                    },
                },
            } as any,
        };
        const secondFlight: Partial<IFlight> = {
            routeType: RouteType.DIRECT,
            flightId: {
                flightNumber: '0013',
                carrier: 'SU',
                date: '2022-05-07',
            },
            leg: {
                departure: {
                    times: {
                        scheduledDeparture: {
                            local: '2022-05-07T19:00:00+03:00',
                        },
                    },
                },
            } as any,
        };
        const connectingFlight: Partial<IFlight> = {
            routeType: RouteType.CONNECTING,
            flights: [firstFlight as any, secondFlight as any],
        };

        const url = service.getDetailsPageUrl(connectingFlight as any);

        expect(url).toBe('schedule/SU0012-20220501/SU0013-20220507');
    });

    it('should return url for route page for one way rote without time range and connections', () => {
        const route: IScheduleRouteDirectionParams = {
            departure: 'MOW',
            arrival: 'KUF',
            dateFrom: new Date(2022, 3, 25),
            dateTo: new Date(2022, 4, 1),
        };

        const url = service.getRoutePageUrl(route);
        expect(url).toBe('schedule/route/MOW-KUF-20220425-20220501');
    });

    it('should return url for route page for one way rote without connections', () => {
        const route: IScheduleRouteDirectionParams = {
            departure: 'MOW',
            arrival: 'KUF',
            timeFrom: '0600',
            timeTo: '2200',
            dateFrom: new Date(2022, 3, 25),
            dateTo: new Date(2022, 4, 1),
        };

        const url = service.getRoutePageUrl(route);
        expect(url).toBe('schedule/route/MOW-KUF-20220425-20220501-06002200');
    });

    it('should return url for route page for one way rote without time range', () => {
        const route: IScheduleRouteDirectionParams = {
            departure: 'MOW',
            arrival: 'KUF',
            connections: 1,
            dateFrom: new Date(2022, 3, 25),
            dateTo: new Date(2022, 4, 1),
        };

        const url = service.getRoutePageUrl(route);
        expect(url).toBe('schedule/route/MOW-KUF-20220425-20220501-C1');
    });

    it('should return url for route page for one way rote', () => {
        const route: IScheduleRouteDirectionParams = {
            departure: 'MOW',
            arrival: 'KUF',
            connections: 1,
            timeFrom: '0600',
            timeTo: '2200',
            dateFrom: new Date(2022, 3, 25),
            dateTo: new Date(2022, 4, 1),
        };

        const url = service.getRoutePageUrl(route);
        expect(url).toBe(
            'schedule/route/MOW-KUF-20220425-20220501-06002200-C1',
        );
    });

    it('should return url for route page for two ways rote without time range and connections', () => {
        const route: IScheduleRouteDirectionParams = {
            departure: 'MOW',
            arrival: 'KUF',
            dateFrom: new Date(2022, 3, 25),
            dateTo: new Date(2022, 4, 1),
        };

        const returnRoute: IScheduleRouteDirectionParams = {
            departure: 'KUF',
            arrival: 'MOW',
            dateFrom: new Date(2022, 4, 2),
            dateTo: new Date(2022, 4, 8),
        };

        const url = service.getRoutePageUrl(route, returnRoute);
        expect(url).toBe(
            'schedule/route/MOW-KUF-20220425-20220501/KUF-MOW-20220502-20220508',
        );
    });

    it('should return url for route page for two ways rote without connections', () => {
        const route: IScheduleRouteDirectionParams = {
            departure: 'MOW',
            arrival: 'KUF',
            timeFrom: '0600',
            timeTo: '2200',
            dateFrom: new Date(2022, 3, 25),
            dateTo: new Date(2022, 4, 1),
        };

        const returnRoute: IScheduleRouteDirectionParams = {
            departure: 'KUF',
            arrival: 'MOW',
            timeFrom: '0600',
            timeTo: '2200',
            dateFrom: new Date(2022, 4, 2),
            dateTo: new Date(2022, 4, 8),
        };

        const url = service.getRoutePageUrl(route, returnRoute);
        expect(url).toBe(
            'schedule/route/MOW-KUF-20220425-20220501-06002200/KUF-MOW-20220502-20220508-06002200',
        );
    });

    it('should return url for route page for one way rote without time range', () => {
        const route: IScheduleRouteDirectionParams = {
            departure: 'MOW',
            arrival: 'KUF',
            connections: 0,
            dateFrom: new Date(2022, 3, 25),
            dateTo: new Date(2022, 4, 1),
        };

        const returnRoute: IScheduleRouteDirectionParams = {
            departure: 'KUF',
            arrival: 'MOW',
            connections: 0,
            dateFrom: new Date(2022, 4, 2),
            dateTo: new Date(2022, 4, 8),
        };

        const url = service.getRoutePageUrl(route, returnRoute);
        expect(url).toBe(
            'schedule/route/MOW-KUF-20220425-20220501-C0/KUF-MOW-20220502-20220508-C0',
        );
    });

    it('should return url for route page for two ways rote', () => {
        const route: IScheduleRouteDirectionParams = {
            departure: 'MOW',
            arrival: 'KUF',
            connections: 0,
            timeFrom: '0600',
            timeTo: '2200',
            dateFrom: new Date(2022, 3, 25),
            dateTo: new Date(2022, 4, 1),
        };

        const returnRoute: IScheduleRouteDirectionParams = {
            departure: 'KUF',
            arrival: 'MOW',
            connections: 0,
            timeFrom: '0600',
            timeTo: '2200',
            dateFrom: new Date(2022, 4, 2),
            dateTo: new Date(2022, 4, 8),
        };

        const url = service.getRoutePageUrl(route, returnRoute);
        expect(url).toBe(
            'schedule/route/MOW-KUF-20220425-20220501-06002200-C0/KUF-MOW-20220502-20220508-06002200-C0',
        );
    });
});
