import { Component, OnInit, Input, AfterContentInit, Output, EventEmitter, OnDestroy, OnChanges, SimpleChanges } from '@angular/core';
import { DiscoverZone } from '@overflow/commons-typescript/model/discovery';

@Component({
  selector: 'of-discovery-filter-summary',
  templateUrl: './filter-summary.component.html',
})
export class FilterSummaryComponent implements OnInit, AfterContentInit, OnDestroy, OnChanges {

  @Output() click = new EventEmitter();
  @Input() data: DiscoverZone;

  private hostRange: string;
  private portRange: string;
  private includeTCP: boolean;
  private includeUDP: boolean;
  private services: string;

  constructor(
  ) {
    this.services = '';
  }

  ngOnInit() {
  }

  ngAfterContentInit() {
  }

  ngOnChanges(changes: SimpleChanges): void {
    if (!this.data) {
      return;
    }
    this.hostRange = this.data.discoverHost.firstScanRangev4 + ' ~ ' + this.data.discoverHost.lastScanRangev4;
    if (this.data.discoverHost.discoverPort) {
      this.portRange = this.data.discoverHost.discoverPort.firstScanRange + ' ~ ' + this.data.discoverHost.discoverPort.lastScanRange;
      this.includeTCP = this.data.discoverHost.discoverPort.includeTCP;
      this.includeUDP = this.data.discoverHost.discoverPort.includeUDP;
    }

    if (this.data.discoverHost.discoverPort.discoverService) {
      const services = this.data.discoverHost.discoverPort.discoverService.includeServices;
      if (services.length > 3) {
        this.services = String(services.length) + ' services selected.';
        return;
      }
      for (const service of services) {
        this.services += service;
      }
    }
  }

  ngOnDestroy() {
  }

  onClick() {
    this.click.emit();
  }
}
