import { ComponentFixture, TestBed } from '@angular/core/testing';
import { getMockPipe } from '@shared/pipes/pipe.mock';
import { FlightStatusButtonComponent } from './flight-status-button.component';
import { RouteTypeLegacy } from '@shared/enumerators';
import { AirlineCodes } from '@shared/airlines';
import { FlightStatusLogic } from './flight-status.logic';
import { RouterHandlerService } from '@shared/services/router-handler.service';
import { FlightModel } from '@shared/models-legacy';

describe('FlightStatusButtonComponent', () => {
    let component: FlightStatusButtonComponent;
    let fixture: ComponentFixture<FlightStatusButtonComponent>;
    let navigate: RouterHandlerService;
    let logic: FlightStatusLogic;
    const aeroflotFlight: FlightModel = {
        routeType: RouteTypeLegacy.Direct,
        operatingBy: { scheduled: AirlineCodes.Aeroflot },
    } as any;
    const pobedayFlight: FlightModel = {
        routeType: RouteTypeLegacy.Direct,
        operatingBy: { scheduled: AirlineCodes.Pobeda },
    } as any;
    const rossiyaFlight: FlightModel = {
        routeType: RouteTypeLegacy.Direct,
        operatingBy: { scheduled: AirlineCodes.Rossiya },
    } as any;
    const auroraFlight: FlightModel = {
        routeType: RouteTypeLegacy.Direct,
        operatingBy: { scheduled: AirlineCodes.Aurora },
    } as any;
    const airfranceFlight: FlightModel = {
        routeType: RouteTypeLegacy.Direct,
        operatingBy: { scheduled: AirlineCodes.AirFrance },
    } as any;
    let window$: any;
    beforeEach(async () => {
        logic = {} as any;
        navigate = {} as any;
        await TestBed.configureTestingModule({
            declarations: [
                FlightStatusButtonComponent,
                getMockPipe('translate'),
            ],
            providers: [
                { provide: RouterHandlerService, useFactory: () => navigate },
                { provide: FlightStatusLogic, useFactory: () => logic },
            ],
        }).compileComponents();

        fixture = TestBed.createComponent(FlightStatusButtonComponent);
        component = fixture.componentInstance;
        component.window$ = window$ = {} as any;
        fixture.detectChanges();
    });

    it('should create', () => {
        expect(component).toBeTruthy();
    });

    it('should open aeroflot, but not open airfrance', () => {
        logic.available = jasmine
            .createSpy('available')
            .withArgs(airfranceFlight)
            .and.returnValue(false)
            .withArgs(aeroflotFlight)
            .and.returnValue(true);
        navigate.toBoardDetailsWithBlank = jasmine
            .createSpy('toBoardDetailsWithBlank')
            .withArgs(aeroflotFlight)
            .and.callThrough();

        component.flight = {
            routeType: RouteTypeLegacy.Connecting,
            flights: [airfranceFlight, aeroflotFlight],
        } as any;
        component.open();
        expect(navigate.toBoardDetailsWithBlank).toHaveBeenCalled();
    });

    it('should open pobeda and aurora on its own sites and open rossia on native details', () => {
        logic.available = jasmine.createSpy('available').and.returnValue(true);
        navigate.toBoardDetailsWithBlank = jasmine
            .createSpy('toBoardDetailsWithBlank')
            .withArgs(rossiyaFlight)
            .and.callThrough();

        window$.open = jasmine
            .createSpy('open')
            .withArgs('https://www.pobeda.aero', '_blank')
            .and.callThrough()
            .withArgs('https://www.flyaurora.ru', '_blank')
            .and.callThrough();

        component.flight = {
            routeType: RouteTypeLegacy.Connecting,
            flights: [pobedayFlight, auroraFlight, rossiyaFlight],
        } as any;
        component.open();
        expect(window$.open).toHaveBeenCalledTimes(2);
        expect(navigate.toBoardDetailsWithBlank).toHaveBeenCalled();
    });
});
