import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { VulnerabilitySummary } from '../service/index';

import { ResultBarChartComponent } from './result-bar-chart.component';
import { ResultTipComponent } from './result-tip.component';
import {
  ScanningResultService,
  ScanningResultDefaultService,
  TagService,
  TagDefaultService,
  JobLogService,
  JobLogDefaultService
} from '../service/index';
import { SERVICE_CONFIG, IServiceConfig } from '../service.config';
import { ErrorHandler } from '../error-handler/index';
import { SharedModule } from '../shared/shared.module';
import { VULNERABILITY_SCAN_STATUS } from '../utils';
import { ChannelService } from '../channel/index';
import { ResultTipHistogramComponent } from "./result-tip-histogram/result-tip-histogram.component";
import { HistogramChartComponent } from "./histogram-chart/histogram-chart.component";

describe('ResultBarChartComponent (inline template)', () => {
  let component: ResultBarChartComponent;
  let fixture: ComponentFixture<ResultBarChartComponent>;
  let serviceConfig: IServiceConfig;
  let testConfig: IServiceConfig = {
    vulnerabilityScanningBaseEndpoint: "/api/vulnerability/testing"
  };
  let mockData: VulnerabilitySummary = {
    scan_status: VULNERABILITY_SCAN_STATUS.SUCCESS,
    severity: "High",
    end_time: new Date(),
    summary: {
      total: 124,
      summary: {
        "High": 5,
        "Low": 5
      }
    }
  };

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        SharedModule
      ],
      declarations: [
        ResultBarChartComponent,
        ResultTipComponent,
        ResultTipHistogramComponent,
        HistogramChartComponent],
      providers: [
        ErrorHandler,
        ChannelService,
        { provide: SERVICE_CONFIG, useValue: testConfig },
        { provide: TagService, useValue: TagDefaultService },
        { provide: ScanningResultService, useValue: ScanningResultDefaultService },
        { provide: JobLogService, useValue: JobLogDefaultService}
      ]
    }).compileComponents();

  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(ResultBarChartComponent);
    component = fixture.componentInstance;
    component.tagId = "mockTag";
    component.summary = mockData;

    serviceConfig = TestBed.get(SERVICE_CONFIG);

    fixture.detectChanges();
  });

  it('should be created', () => {
    expect(component).toBeTruthy();
    expect(serviceConfig).toBeTruthy();
    expect(serviceConfig.vulnerabilityScanningBaseEndpoint).toEqual("/api/vulnerability/testing");
  });

  it('should show "not scanned" if status is STOPPED', () => {
    component.summary.scan_status = VULNERABILITY_SCAN_STATUS.STOPPED;
    fixture.detectChanges();
    fixture.whenStable().then(() => {
      fixture.detectChanges();
      let el: HTMLElement = fixture.nativeElement.querySelector('span');
      expect(el).toBeTruthy();
      expect(el.textContent).toEqual('VULNERABILITY.STATE.OTHER_STATUS');
    });
  });

  it('should show progress if status is SCANNING', () => {
    component.summary.scan_status = VULNERABILITY_SCAN_STATUS.RUNNING;
    fixture.detectChanges();

    fixture.whenStable().then(() => {
      fixture.detectChanges();

      let el: HTMLElement = fixture.nativeElement.querySelector('.progress');
      expect(el).toBeTruthy();
    });
  });

  it('should show QUEUED if status is QUEUED', () => {
    component.summary.scan_status = VULNERABILITY_SCAN_STATUS.PENDING;
    fixture.detectChanges();
    fixture.whenStable().then(() => {
      fixture.detectChanges();

      let el: HTMLElement = fixture.nativeElement.querySelector('.bar-state');
      expect(el).toBeTruthy();
      let el2: HTMLElement = el.querySelector('span');
      expect(el2).toBeTruthy();
      expect(el2.textContent).toEqual('VULNERABILITY.STATE.QUEUED');

    });
  });

  it('should show summary bar chart if status is COMPLETED', () => {
    component.summary.scan_status = VULNERABILITY_SCAN_STATUS.SUCCESS;
    fixture.detectChanges();

    fixture.whenStable().then(() => {
      fixture.detectChanges();
      let el: HTMLElement = fixture.nativeElement.querySelector('hbr-result-tip-histogram');
      expect(el).not.toBeNull();
    });
  });

});
