import { ElementRef, NO_ERRORS_SCHEMA } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { CitiesSearchService } from '@components/city-autocomplete/cities-search-service/cities-search.service';
import { CityAutocompleteComponent } from '@components/city-autocomplete/city-autocomplete.component';
import { DictionariesService } from '@modules/components/page-filters/services/dictionaries-service';
import { getDictionariesServiceMock } from '@modules/components/page-filters/services/dictionaries-service.mock';
import { Language } from '@shared/enumerators';
import { getMockPipe } from '@shared/pipes/pipe.mock';
import { GeoService, LocalizationService } from '@shared/services';

describe('CityAutocompleteComponent', () => {
    let component: CityAutocompleteComponent;
    let fixture: ComponentFixture<CityAutocompleteComponent>;
    let citiesSearchService: CitiesSearchService;
    let dictService: DictionariesService;
    let elementRef: ElementRef;
    let geoService: GeoService;
    let localizationService: LocalizationService;

    beforeEach(async () => {
        dictService = getDictionariesServiceMock();
        localizationService = {
            Language: Language.ru,
        } as any;
        citiesSearchService = new CitiesSearchService(
            localizationService,
            dictService,
        );
        elementRef = {
            nativeElement: {
                contains: jasmine.createSpy(),
            },
        };
        geoService = {
            getPosition: () => {
                return Promise.resolve({
                    latitude: 1,
                    longitude: 1,
                });
            },
        } as any;

        TestBed.configureTestingModule({
            declarations: [CityAutocompleteComponent, getMockPipe('translate')],
            providers: [
                { provide: LocalizationService, useValue: localizationService },
                { provide: DictionariesService, useValue: dictService },
                { provide: CitiesSearchService, useValue: citiesSearchService },
                { provide: ElementRef, useValue: elementRef },
                { provide: GeoService, useValue: geoService },
            ],
            schemas: [NO_ERRORS_SCHEMA],
        });

        await dictService.ready$;
    });

    beforeEach(() => {
        fixture = TestBed.createComponent(CityAutocompleteComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();

        component.autocomplite = {
            hide: jasmine.createSpy(),
        } as any;
    });

    it('should set items', () => {
        const event = { query: 'Мос' };
        component.filterCity(event);

        expect(component.items.length).not.toBe(0);
    });

    it("should set items even if keyboard layout isn't russian", () => {
        const event = { query: 'Vjc' }; // Vjc === Мос
        component.filterCity(event);

        expect(component.items.length).not.toBe(0);
    });

    it('should city if there is full match', () => {
        const event = { query: 'Москва' }; // Vjc === Мос
        jasmine.clock().install();

        component.filterCity(event);
        jasmine.clock().tick(100);

        expect(component.items).toBe(undefined);
        expect(component.city.name).toBe(event.query);

        jasmine.clock().uninstall();
    });
});
