import { BuyTicketLogic } from './buy-ticket.logic';
import { FlightModel } from '../../../shared/models-legacy';
import { RouteTypeLegacy } from '../../../shared/enumerators';
import * as moment from 'moment';

describe('BuyLogic', () => {
    let logic: BuyTicketLogic;
    const in5Hours = moment().add(5, 'hour').toDate();
    const in7Hours = moment().add(7, 'hour').toDate();
    let window$: Window;
    const invalid: FlightModel = {
        routeType: RouteTypeLegacy.Direct,
        leg: {
            departure: { scheduled: { airportCode: 'RYZ' }, times: { scheduled: { utc: moment().add(100500, 'hour').toDate() } } },
            arrival: { scheduled: { airportCode: 'PRG' } },
        },
    } as any;
    const direct: FlightModel = {
        routeType: RouteTypeLegacy.Direct,
        leg: {
            departure: { scheduled: { airportCode: 'RYZ' }, times: { scheduled: { utc: in5Hours, local: in5Hours } } },
            arrival: { scheduled: { airportCode: 'PRG' } },
        },
    } as any;

    const returning: FlightModel = {
        routeType: RouteTypeLegacy.Direct,
        leg: {
            departure: { scheduled: { airportCode: 'RYZ' }, times: { scheduled: { utc: in7Hours, local: in7Hours } } },
            arrival: { scheduled: { airportCode: 'PRG' } },
        },
    } as any;
    beforeEach(() => {
        window$ = {} as any;
        const settings = {
            buyPeriod: { min: -1, max: -10 },
        } as any;
        logic = new BuyTicketLogic(settings, window$);
    });
    it('should return isAnyAvailable for empty flights', () => {
        expect(logic.isAnyAvailable()).toBeFalse();
        expect(logic.isAnyAvailable(undefined as any, null as any)).toBeFalse();
    });

    it('should return isAvailable for valid flight', () => {
        expect(logic.isAvailable(direct)).toBeTrue();
    });

    it('should return isAvailable true for valid flight', () => {
        expect(logic.isAvailable(direct)).toBeTrue();
    });

    it('should return isAvailable false for valid flight', () => {
        expect(logic.isAvailable(invalid)).toBeFalse();
    });

    it('should return isAnyAvailable true for 1 valid of 2 flight', () => {
        expect(logic.isAnyAvailable(invalid, direct)).toBeTrue();
        expect(logic.isAnyAvailable(direct, invalid)).toBeTrue();
    });

    it('should open single url', () => {
        const blank = { focus: jasmine.createSpy().and.callThrough() };
        window$.open = jasmine
            .createSpy('open')
            .withArgs(
                `https://www.aeroflot.ru/sb/app/ru-ru#/search?adults=1&cabin=economy&children=0&infants=0&routes=RYZ.${moment(
                    in5Hours,
                ).format('YYYYMMDD')}.PRG&autosearch=Y`,
                '_blank',
            )
            .and.returnValue(blank);

        logic.buyAll(direct);

        expect(window$.open).toHaveBeenCalled();
        expect(blank.focus).toHaveBeenCalled();
    });

    it('should open double url', () => {
        const blank = { focus: jasmine.createSpy().and.callThrough() };

        const expectedUrl = `https://www.aeroflot.ru/sb/app/ru-ru#/search?adults=1&cabin=economy&children=0&infants=0&routes=RYZ.${moment(
            in5Hours,
        ).format('YYYYMMDD')}.PRG-RYZ.${moment(in7Hours).format('YYYYMMDD')}.PRG&autosearch=Y`;

        window$.open = jasmine.createSpy('open').withArgs(expectedUrl, '_blank').and.returnValue(blank);

        logic.buyAll(direct, returning);

        expect(window$.open).toHaveBeenCalled();
        expect(blank.focus).toHaveBeenCalled();
    });
});
