import { TestBed, ComponentFixture } from '@angular/core/testing';
import { UiSelectedFilterOptionsComponent } from './selected-filter-options.component';
import { ChangeDetectorRef, Renderer2 } from '@angular/core';
import { IconModule } from '@libs/ui';
import { FilterResetWordingPipe, SelectedFiltersPipe, CheckAllChildOptionsSelectedPipe } from '../pipe';
import { By } from '@angular/platform-browser';
import { SelectFilter } from '../../models';

fdescribe('SelectedFilterOptionsComponent', () => {
  let fixture: ComponentFixture<UiSelectedFilterOptionsComponent>;
  let component: UiSelectedFilterOptionsComponent;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [IconModule, IconModule],
      providers: [ChangeDetectorRef, Renderer2, UiSelectedFilterOptionsComponent],
      declarations: [UiSelectedFilterOptionsComponent, SelectedFiltersPipe, FilterResetWordingPipe, CheckAllChildOptionsSelectedPipe],
    }).compileComponents();
  });

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

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

  describe('#ResetFilters', () => {
    let button: HTMLButtonElement;

    beforeEach(() => {
      spyOn(component, 'resetFilters').and.callThrough();
      component.resetStrategy = 'all';
      component.showResetButton = true;

      button = fixture.debugElement.query(By.css('.isa-btn.isa-btn-primary-link.remove-all')).nativeElement;

      fixture.detectChanges();
    });

    it('should be shown by default', () => {
      expect(button).toBeTruthy();
    });

    it('should not be shwon if showResetButton is false', async () => {
      component.showResetButton = false;
      fixture.detectChanges();

      await fixture.whenStable();

      const debugButtonEl = fixture.debugElement.query(By.css('.isa-btn.isa-btn-primary-link.remove-all'));
      button = debugButtonEl && debugButtonEl.nativeElement;

      expect(button).toBeFalsy();
    });

    it('should deselect all filters when resetStrategy is all', () => {
      spyOn(component, 'deselectAllFilters').and.callThrough();
      button.click();
      fixture.detectChanges();

      expect(component.resetFilters).toHaveBeenCalled();
      expect(component.deselectAllFilters).toHaveBeenCalled();
    });

    it('should revert to the default selection of filters when resetStrategy is default', () => {
      spyOn(component, 'resetToDefault').and.callThrough();
      component.resetStrategy = 'default';
      fixture.detectChanges();

      button.click();
      fixture.detectChanges();

      expect(component.resetFilters).toHaveBeenCalled();
      expect(component.resetToDefault).toHaveBeenCalled();
    });
  });

  describe('resetToDefault', () => {
    /* let primaryFilters: PrimaryFilterOption[];
    let selectFilters: SelectFilter[];

    beforeEach(() => {
      primaryFilters = primaryFiltersMock;
      selectFilters = mockFilters;
    });

    it('should reset the filters to its initial state', () => {
      selectFilters[0].options[0].selected = true;
      selectFilters[0].options[0].initial_selected_state = false;

      spyOn(component, 'resetToDefault').and.callThrough();

      const result = component.resetToDefault(selectFilters);

      expect(result[0].options[0].selected).toBe(false);
      expect(result[0].options[0].initial_selected_state).toBe(false);
      expect(result.length).toEqual(selectFilters.length);
    }); */
  });

  describe('deselectAllFilters', () => {
    // let primaryFilters: PrimaryFilterOption[];
    let selectFilters: SelectFilter[];

    beforeEach(() => {
      // primaryFilters = primaryFiltersMock;
      // selectFilters = mockFilters;
    });

    it('should deselect all filters', () => {
      spyOn(component, 'deselectAllFilters').and.callThrough();

      const result = component.deselectAllFilters(selectFilters);

      expect(
        result.every((filter) => {
          if (filter.options) {
            return filter.options.every((o) => !o.selected);
          }

          return true;
        })
      );
      expect(result.length).toEqual(selectFilters.length);
    });
  });
});
