import {
  Component, Input, OnDestroy, OnInit, ViewChild,
} from '@angular/core';


import { Observable, of, Subscription } from 'rxjs';
import { catchError, map, tap, take } from 'rxjs/operators';

import { ProbeHost } from '@overflow/commons-typescript/model/probe';
import { DiscoverZone, Zone, Host, Port, Service } from '@overflow/commons-typescript/model/discovery';

import { Anim } from './animation';
import { DiscoveryService } from '../service/discovery.service';
import { DiscoverySubscriber, DiscoveryNotify } from '../subscriber/discovery.subscriber';
import { SearchFilterComponent } from './search-filter.component';
import { DiscoveryInfraTreeComponent } from './discovery-infra-tree.component';

@Component({
  selector: 'of-discovery',
  templateUrl: './discovery.component.html',
  animations: Anim,
})

export class DiscoveryComponent implements OnInit, OnDestroy {

  @Input() probeHostID;
  pending$: Observable<boolean>;
  error$: Observable<any>;
  discoverySubscription: Subscription;
  selectedProbe: ProbeHost;
  discoverZone: DiscoverZone;

  startDate: Date;
  stopDate: Date;

  filterWord: string;
  filterServices: Service[];

  @ViewChild('infraTree') infraTree: DiscoveryInfraTreeComponent;

  constructor(
    private discoveryService: DiscoveryService,
    private discoverySubscriber: DiscoverySubscriber,
  ) {
    this.discoverySubscription = null;
    this.startDate = null;
    this.stopDate = null;
  }

  ngOnInit(): void {
  }


  ngOnDestroy(): void {
    if (null !== this.discoverySubscription) {
      this.discoverySubscription.unsubscribe();
    }
  }

  onRequestDiscovery(dz: DiscoverZone) {
    this.discoverZone = dz;
    const zone: Zone = {
      network: this.selectedProbe.infraHost.infraZone.network, // this.selectedProbe.probe.cidr
      address: this.selectedProbe.infraHost.infraZone.address.split('/')[0],
      iface: this.selectedProbe.infraHost.infraZone.iface,
      mac: this.selectedProbe.infraHost.infraZone.mac,
      metaIPType: this.selectedProbe.infraHost.infraZone.metaIPType,
    };

    this.discoveryService.discoverHost(this.selectedProbe.probe.probeKey, zone, dz.discoverHost);

    this.discoverySubscription = this.discoverySubscriber.observable().pipe(
      map((discoveryNotify: DiscoveryNotify) => {
        switch (discoveryNotify.method) {
          case 'DiscoveryService.discoveryStart': {
            const startDate = discoveryNotify.params as Date;
            this.startDate = startDate;
            this.infraTree.discoveryStarted(startDate);
            break;
          }
          case 'DiscoveryService.discoveryStop': {
            const stopDate = discoveryNotify.params as Date;
            this.stopDate = stopDate;
            this.discoverySubscription.unsubscribe();
            this.discoverySubscription = null;
            this.infraTree.discoveryStopped(stopDate);
            break;
          }
          case 'DiscoveryService.discoveredZone': {
            const _zone = discoveryNotify.params as Zone;
            break;
          }
          case 'DiscoveryService.discoveredHost': {
            const host = discoveryNotify.params as Host;
            this.infraTree.addHost(host);
            break;
          }
          case 'DiscoveryService.discoveredPort': {
            const port = discoveryNotify.params as Port;
            // this.discoveryResult.addPort(port);
            break;
          }
          case 'DiscoveryService.discoveredService': {
            const service = discoveryNotify.params as Service;
            this.infraTree.addService(service);
            break;
          }
          default: {
            break;
          }
        }
      }),
      catchError(error => {
        return of();
      }),
    ).subscribe();
  }

}
