import { NO_ERRORS_SCHEMA } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { getMockPipe } from '@shared/pipes/pipe.mock';
import { Leg } from '@shared/models-legacy';
import { ITimesSet } from '@typings/times';
import { TimelineComponent } from './timeline.component';

describe('TimelineComponent', () => {
    let component: TimelineComponent;
    let fixture: ComponentFixture<TimelineComponent>;
    const actualBlockOn: ITimesSet = {} as any;
    const scheduledArrival: ITimesSet = {} as any;

    const legs: Leg[] = [
        { arrival: { _times: { scheduledArrival, actualBlockOn } } },
    ] as any;

    beforeEach(async () => {
        await TestBed.configureTestingModule({
            declarations: [
                TimelineComponent,
                getMockPipe('translate'),
                getMockPipe('duration'),
                getMockPipe('transferTime'),
            ],
            schemas: [NO_ERRORS_SCHEMA],
        }).compileComponents();
    });

    beforeEach(() => {
        fixture = TestBed.createComponent(TimelineComponent);
        component = fixture.componentInstance;
        component.legs = [];
        fixture.detectChanges();
    });

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

    it('should not fail on empty legs or out of index', () => {
        expect(component.getArrivalScheduledTimesSet(-100)).toBeUndefined();
        expect(component.getArrivalScheduledTimesSet(100)).toBeUndefined();
        expect(component.getDepartureScheduledTimesSet(-100)).toBeUndefined();
        expect(component.getDepartureScheduledTimesSet(100)).toBeUndefined();

        expect(component.getArrivalActualTimesSet(-100)).toBeUndefined();
        expect(component.getArrivalActualTimesSet(100)).toBeUndefined();
        expect(component.getDepartureActualTimesSet(-100)).toBeUndefined();
        expect(component.getDepartureActualTimesSet(100)).toBeUndefined();

        component.legs = [
            { arrival: { _times: {} }, departure: { _times: {} } },
        ] as any;
        expect(component.getArrivalScheduledTimesSet(0)).toBeUndefined();
        expect(component.getArrivalScheduledTimesSet(0)).toBeUndefined();
        expect(component.getDepartureScheduledTimesSet(0)).toBeUndefined();
        expect(component.getDepartureScheduledTimesSet(0)).toBeUndefined();

        expect(component.getArrivalActualTimesSet(0)).toBeUndefined();
        expect(component.getArrivalActualTimesSet(0)).toBeUndefined();
        expect(component.getDepartureActualTimesSet(0)).toBeUndefined();
        expect(component.getDepartureActualTimesSet(0)).toBeUndefined();
    });

    it('should return latest arrival with canChange = true as latest', () => {
        component.canChange = true;
        component.legs = legs;
        expect(component.getArrivalActualTimesSet(0)).toBe(actualBlockOn);
    });

    it('should return scheduled arrival with canChange = false', () => {
        component.canChange = false;
        component.legs = legs;
        expect(component.getArrivalActualTimesSet(0)).toBe(undefined);
        expect(component.getArrivalScheduledTimesSet(0)).toBe(scheduledArrival);
    });

    it('should return scheduled arrival with canChange = true as baseline', () => {
        component.canChange = true;
        component.legs = legs;
        expect(component.getArrivalScheduledTimesSet(0)).toBe(scheduledArrival);
    });
});
