import { Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';

import { RPCSubscriber } from '@loafer/ng-rpc/decorator';
import { LoggerService } from '@loafer/ng-logger/service';

import * as DiscoverStore from '../store/discover';

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

@Injectable()
export class DiscoverySubscriber {

  public constructor(
    private store: Store<DiscoverStore.State>,
    private loggerService: LoggerService,
  ) {
  }

  @RPCSubscriber({method: 'DiscoveryService.discoveryStart'})
  public discoveryStart(startDate: Date): void {
    this.loggerService.debug('DiscoverySubscriber.discoveryStart startDate:', startDate);

    this.store.dispatch(new DiscoverStore.DiscoveryStart(startDate));
  }

  @RPCSubscriber({method: 'DiscoveryService.discoveryStop'})
  public discoveryStop(stopDate: Date): void {
    this.loggerService.debug('DiscoverySubscriber.discoveryStop stopDate:', stopDate);

    this.store.dispatch(new DiscoverStore.DiscoveryStop(stopDate));
  }


  @RPCSubscriber({method: 'DiscoveryService.discoveredZone'})
  public discoveredZone(zone: Zone): void {
    this.loggerService.debug('DiscoverySubscriber.discoveredZone zone:', zone);

    this.store.dispatch(new DiscoverStore.DiscoveredZone(zone));
  }
  @RPCSubscriber({method: 'DiscoveryService.discoveredHost'})
  public discoveredHost(host: Host): void {
    this.loggerService.debug('DiscoverySubscriber.discoveredHost host:', host);

    this.store.dispatch(new DiscoverStore.DiscoveredHost(host));
  }
  @RPCSubscriber({method: 'DiscoveryService.discoveredPort'})
  public discoveredPort(port: Port): void {
    this.store.dispatch(new DiscoverStore.DiscoveredPort(port));
  }
  @RPCSubscriber({method: 'DiscoveryService.discoveredService'})
  public discoveredService(service: Service): void {
    this.loggerService.debug('DiscoverySubscriber.discoveredService service:', service);
    this.store.dispatch(new DiscoverStore.DiscoveredService(service));
  }
}
