import { NO_ERRORS_SCHEMA } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { TranslatePipe } from '@ngx-translate/core';
import { OnlineBoardFlightDetailsTitleComponent } from '@online-board/pages/flight-details/components/title/flight-details-title.component';
import { OnlineBoardUrlBuilderService } from '@online-board/services/url';
import { RouteType } from '@typings/enums';
import { IFlightId } from '@typings/flight/flight-id';

describe('OnlineBoardFlightDetailsTitleComponent', () => {
    let fixture: ComponentFixture<OnlineBoardFlightDetailsTitleComponent>;
    let component: OnlineBoardFlightDetailsTitleComponent;

    const firstFlightId: IFlightId = {
        flightNumber: '0001',
        carrier: 'SU',
        date: '2022-05-28T00:00:00',
    };
    const secondFlightId: IFlightId = {
        flightNumber: '0002',
        carrier: 'SU',
        date: '2022-05-28T00:00:00',
    };

    const titleBase = 'Информация о рейсе';
    const pluralTitleBase = 'Информация о рейсах';

    const transformMock = jasmine
        .createSpy('transform')
        .withArgs('SHARED.FLIGHT-INFO')
        .and.returnValue(titleBase)
        .withArgs('SHARED.FLIGHTS-INFO')
        .and.returnValue(pluralTitleBase);

    const formatFlightIdMock = jasmine
        .createSpy('formatFlightId')
        .withArgs(firstFlightId)
        .and.returnValue('SU 0001')
        .withArgs(secondFlightId)
        .and.returnValue('SU 0002');

    beforeEach(async () => {
        await TestBed.configureTestingModule({
            declarations: [OnlineBoardFlightDetailsTitleComponent],
            providers: [
                {
                    provide: TranslatePipe,
                    useValue: {
                        transform: transformMock,
                    },
                },
                {
                    provide: OnlineBoardUrlBuilderService,
                    useValue: {
                        formatFlightId: formatFlightIdMock,
                    },
                },
            ],
            schemas: [NO_ERRORS_SCHEMA],
        }).compileComponents();
    });

    beforeEach(() => {
        fixture = TestBed.createComponent(
            OnlineBoardFlightDetailsTitleComponent,
        );
        component = fixture.componentInstance;

        transformMock.calls.reset();
        formatFlightIdMock.calls.reset();
    });

    it('should set empty string to title if flight was not provided', () => {
        component.ngOnChanges();

        expect(component.title).toBe('');
    });

    it('should set title for non connecting flight', () => {
        component.flight = {
            routeType: RouteType.DIRECT,
            flightId: firstFlightId,
            leg: {
                departure: {
                    latest: {
                        city: 'Москва',
                    },
                },
                arrival: {
                    latest: {
                        city: 'Самара',
                    },
                },
            },
        } as any;

        component.ngOnChanges();
        expect(component.title).toBe(
            'Информация о рейсе: SU 0001, Москва - Самара',
        );
    });

    it('should set title for connecting flight', () => {
        component.flight = {
            routeType: RouteType.CONNECTING,
            flights: [
                {
                    routeType: RouteType.DIRECT,
                    flightId: firstFlightId,
                    leg: {
                        departure: {
                            latest: {
                                city: 'Москва',
                            },
                        },
                        arrival: {
                            latest: {
                                city: 'Самара',
                            },
                        },
                    },
                },
                {
                    routeType: RouteType.DIRECT,
                    flightId: secondFlightId,
                    leg: {
                        departure: {
                            latest: {
                                city: 'Самара',
                            },
                        },
                        arrival: {
                            latest: {
                                city: 'Казань',
                            },
                        },
                    },
                },
            ],
        } as any;

        component.ngOnChanges();
        expect(component.title).toBe(
            'Информация о рейсах: SU 0001, SU 0002, Москва - Казань',
        );
    });
});
