/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { ShowHideAlertEntriesComponent, ShowHideStateModel } from './show-hide-alert-entries.component';
import { ComponentFixture, async, TestBed } from '@angular/core/testing';
import { SwitchComponent } from 'app/shared/switch/switch.component';
import { By } from '@angular/platform-browser';
import { Spy } from 'jasmine-core';
import { ShowHideService } from './show-hide.service';

describe('ShowHideAlertEntriesComponent', () => {

  let component: ShowHideAlertEntriesComponent;
  let fixture: ComponentFixture<ShowHideAlertEntriesComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        ShowHideAlertEntriesComponent,
        SwitchComponent
      ],
      providers: [
        { provide: ShowHideService, useClass: () => {
          return {
            hideDismissed: false,
            hideResolved: false,
            setFilterFor: jasmine.createSpy('setFilterFor'),
            isAvailable: () => true,
          }
        } },
      ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(ShowHideAlertEntriesComponent);
    component = fixture.componentInstance;
  });

  it('should have ShowHideService injected', () => {
    expect(component.showHideService).toBeTruthy();
  });

  it('should have ShowHideService.hideDismissed bounded to the dismissed toggle', () => {
    expect(fixture.debugElement.query(By.css('[data-qe-id="hideResolvedAlertsToggle"] input')).nativeElement.checked).toBe(false);

    component.showHideService.hideResolved = true;
    fixture.detectChanges();

    expect(fixture.debugElement.query(By.css('[data-qe-id="hideResolvedAlertsToggle"] input')).nativeElement.checked).toBe(true);
  });

  it('should have ShowHideService.hideResolved bounded to the resolved toggle', () => {
    expect(fixture.debugElement.query(By.css('[data-qe-id="hideDismissedAlertsToggle"] input')).nativeElement.checked).toBe(false);

    component.showHideService.hideDismissed = true;
    fixture.detectChanges();

    expect(fixture.debugElement.query(By.css('[data-qe-id="hideDismissedAlertsToggle"] input')).nativeElement.checked).toBe(true);
  });

  it('should listen to change event on hide resolved toggle', () => {
    fixture.detectChanges(); // triggering ngInit to not disturb this test
    spyOn(component, 'onVisibilityChanged');

    fixture.debugElement.query(By.css('[data-qe-id="hideResolvedAlertsToggle"] input')).nativeElement.click();
    fixture.detectChanges();

    // it set true by localStorage.getItem, so after first click is false
    expect(component.onVisibilityChanged).toHaveBeenCalledWith('RESOLVE', true);

    fixture.debugElement.query(By.css('[data-qe-id="hideResolvedAlertsToggle"] input')).nativeElement.click();
    fixture.detectChanges();

    expect(component.onVisibilityChanged).toHaveBeenCalledWith('RESOLVE', false);
  });

  it('should listen to change event on hide dismissed toggle', () => {
    fixture.detectChanges(); // triggering ngInit to not disturb this test
    spyOn(component, 'onVisibilityChanged');

    fixture.debugElement.query(By.css('[data-qe-id="hideDismissedAlertsToggle"] input')).nativeElement.click();
    fixture.detectChanges();

    expect(component.onVisibilityChanged).toHaveBeenCalledWith('DISMISS', true);

    fixture.debugElement.query(By.css('[data-qe-id="hideDismissedAlertsToggle"] input')).nativeElement.click();
    fixture.detectChanges();

    expect(component.onVisibilityChanged).toHaveBeenCalledWith('DISMISS', false);
  });

  it('should trigger changed event on any toggle changes and propagate state', () => {
    spyOn(component.changed, 'emit');
    fixture.detectChanges();

    component.showHideService.hideResolved = false;
    component.showHideService.hideDismissed = true;

    fixture.debugElement.query(By.css('[data-qe-id="hideDismissedAlertsToggle"] input')).nativeElement.click();
    fixture.detectChanges();

    expect((component.changed.emit as Spy).calls.argsFor(0)[0]).toEqual({ hideResolved: false, hideDismissed: true });

    component.showHideService.hideResolved = true;
    component.showHideService.hideDismissed = true;

    fixture.debugElement.query(By.css('[data-qe-id="hideResolvedAlertsToggle"] input')).nativeElement.click();
    fixture.detectChanges();

    expect((component.changed.emit as Spy).calls.argsFor(1)[0]).toEqual({ hideResolved: true, hideDismissed: true });
  });

  it('should disable toggles when isAvailable returns false', () => {
    component.showHideService.isAvailable = () => false;
    fixture.detectChanges();

    expect(fixture.debugElement.queryAll(By.directive(SwitchComponent))[0].componentInstance.disabled).toBe(true);
    expect(fixture.debugElement.queryAll(By.directive(SwitchComponent))[1].componentInstance.disabled).toBe(true);

    component.showHideService.isAvailable = () => true;
    fixture.detectChanges();

    expect(fixture.debugElement.queryAll(By.directive(SwitchComponent))[0].componentInstance.disabled).toBe(false);
    expect(fixture.debugElement.queryAll(By.directive(SwitchComponent))[1].componentInstance.disabled).toBe(false);
  });

  it('should show warning about the reason of disabled state', () => {
    component.showHideService.isAvailable = () => false;
    fixture.detectChanges();

    expect(fixture.debugElement.query(By.css('.warning'))).toBeTruthy();

    component.showHideService.isAvailable = () => true;
    fixture.detectChanges();

    expect(fixture.debugElement.query(By.css('.warning'))).toBeFalsy();
  });

});
