import { Injectable } from '@angular/core';

import { Observable, Subject } from 'rxjs';

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

import { NoAuthProbe } from '@overflow/commons-typescript/model/noauth';

export interface NoAuthProbeNotify {
  method: string;
  params: NoAuthProbe;
}

export class NoAuthProbeSubscriberSubject extends Subject<NoAuthProbeNotify> {

}

@Injectable()
export class NoAuthProbeSubscriber {
  private noAuthProbeSubscriberSubject: NoAuthProbeSubscriberSubject;

  public constructor(
    private loggerService: LoggerService,
  ) {
    this.noAuthProbeSubscriberSubject = new NoAuthProbeSubscriberSubject();
  }

  public observable(): Observable<NoAuthProbeNotify> {
    return this.noAuthProbeSubscriberSubject.asObservable();
  }

  private publish(method: string, params: any): void {
    this.noAuthProbeSubscriberSubject.next({ method: method, params: params });
  }

  @RPCSubscriber({ method: 'NoAuthProbeService.onConnect' })
  public onConnect(noAuthProbe: NoAuthProbe): void {
    this.loggerService.debug('NoAuthProbeService.onConnect noAuthProbe:', noAuthProbe);

    this.publish('NoAuthProbeService.onConnect', noAuthProbe);
  }

  @RPCSubscriber({ method: 'NoAuthProbeService.onDisconnect' })
  public onDisconnect(noAuthProbe: NoAuthProbe): void {
    this.loggerService.debug('NoAuthProbeService.onDisconnect noAuthProbe:', noAuthProbe);

    this.publish('NoAuthProbeService.onDisconnect', noAuthProbe);
  }
}
