import { ComponentFixture, TestBed } from '@angular/core/testing';
import { RouterHandlerService } from '@shared/services/router-handler.service';
import { SharePanelComponent } from '@modules/components/share-panel/share-panel.component';
import { getMockPipe } from '@shared/pipes/pipe.mock';
import { ShareButtonComponent } from './share-button.component';
import { ViewType } from '@shared/enumerators/flight-request-type.enum';
import { FlightModel } from '@shared/models-legacy';
import { RouteTypeLegacy } from '@shared/enumerators';
import { WINDOW } from '@shared/services';

describe('ShareButtonComponent', () => {
    let component: ShareButtonComponent;
    let fixture: ComponentFixture<ShareButtonComponent>;
    let panel: SharePanelComponent;
    let navigate: RouterHandlerService;
    beforeEach(async () => {
        navigate = {} as any;
        await TestBed.configureTestingModule({
            declarations: [ShareButtonComponent, getMockPipe('translate')],
            providers: [
                { provide: RouterHandlerService, useFactory: () => navigate },
                {
                    provide: WINDOW,
                    useValue: {
                        location: {
                            href: 'http://abc.de/fgh/ijk',
                            origin: 'http://abc.de',
                        },
                    } as any,
                },
            ],
        }).compileComponents();
    });

    beforeEach(() => {
        panel = {} as any;
        fixture = TestBed.createComponent(ShareButtonComponent);
        component = fixture.componentInstance;
        component.panel = panel;

        fixture.detectChanges();
    });

    it('should create', () => {
        expect(component).toBeTruthy();
    });
    it('should share current', () => {
        const event = {} as any;
        panel.togglePanel = jasmine
            .createSpy('togglePanel')
            .withArgs(event, 'http://abc.de/fgh/ijk')
            .and.callThrough();
        component.share(event);
        expect(panel.togglePanel).toHaveBeenCalled();
    });
    it('should share board', () => {
        const event = {} as any;

        component.flight = {
            routeType: RouteTypeLegacy.Direct,
            flightId: { carrier: 'SU', flightNumber: '4321' },
            dateToSearchBy: new Date(2021, 11, 21),
        } as any;
        navigate.getDetailsAbsoluteUrl = jasmine
            .createSpy('getDetailsAbsoluteUrl')
            .and.returnValue('http://abc.de/en-en/onlineboard/SU4321/21122021');
        panel.togglePanel = jasmine
            .createSpy('togglePanel')
            .withArgs(event, 'http://abc.de/en-en/onlineboard/SU4321/21122021')
            .and.callThrough();
        component.viewType = ViewType.Onlineboard;
        component.share(event);
        expect(panel.togglePanel).toHaveBeenCalled();
    });

    it('should share schedule', () => {
        const event = {} as any;

        const flight: FlightModel = {
            routeType: RouteTypeLegacy.Connecting,
            flights: [
                {
                    routeType: RouteTypeLegacy.Direct,
                    flightId: { carrier: 'SU', flightNumber: '1234' },
                    dateToSearchBy: new Date(2021, 11, 20),
                } as any,
                {
                    routeType: RouteTypeLegacy.Direct,
                    flightId: { carrier: 'SU', flightNumber: '4321' },
                    dateToSearchBy: new Date(2021, 11, 21),
                } as any,
            ],
        } as any;
        navigate.getDetailsAbsoluteUrl = jasmine
            .createSpy('getDetailsAbsoluteUrl')
            .and.returnValue(
                'http://abc.de/en-en/schedule/SU1234-20122021_SU4321-21122021',
            );
        component.flight = flight;
        component.viewType = ViewType.Schedule;
        panel.togglePanel = jasmine
            .createSpy('togglePanel')
            .withArgs(
                event,
                'http://abc.de/en-en/schedule/SU1234-20122021_SU4321-21122021',
            )
            .and.callThrough();
        component.share(event);
        expect(panel.togglePanel).toHaveBeenCalled();
    });
});
