import { Component, Input, Output, EventEmitter, OnInit } from '@angular/core';
import { Observable, of } from 'rxjs';
import { catchError, map, tap, take } from 'rxjs/operators';
import { NoAuthProbeService } from '../service/noauth-probe.service';
import { NoAuthProbe, InfraHostIP } from '@overflow/commons-typescript';
import { ConfirmationService } from 'primeng/primeng';

@Component({
  selector: 'of-noauth-probe-detail',
  templateUrl: './noauth-probe-detail.component.html',
  providers: [ConfirmationService]
})
export class NoAuthProbeDetailComponent implements OnInit {

  @Input() id: number;
  pending$: Observable<boolean>;
  noAuthProbe: NoAuthProbe;
  error$: Observable<any>;
  selectedNIC: InfraHostIP;
  @Output() back = new EventEmitter();

  constructor(
    private noAuthProbeService: NoAuthProbeService,
    private confirmationService: ConfirmationService,
  ) {
  }

  ngOnInit() {
    this.noAuthProbeService.read(this.id)
      .pipe(
        tap(() => {
          this.pending$ = of(true);
        }),
        map((noauthProbe: NoAuthProbe) => {
          noauthProbe.infraHost = JSON.parse(noauthProbe.infraHostMeta);
          this.noAuthProbe = noauthProbe;
        }),
        catchError(error => {
          this.error$ = of(error);
          return of();
        }),
        tap(() => {
          this.pending$ = of(false);
        }),
        take(1),
    ).subscribe();
  }

  onNICSelect(infraHostIP: InfraHostIP) {
    this.selectedNIC = infraHostIP;
  }

  accept() {
    console.log(this.selectedNIC);

    this.selectedNIC.address = '192.168.1.0/24';

    let message = 'Start collecting data as a Probe.\n';
    message += this.selectedNIC.iface + '\n';
    message += this.selectedNIC.address;

    this.confirmationService.confirm({
      header: 'Are you sure to accept this Probe?',
      message: message,
      icon: 'fa-check',
      accept: () => {
        this.noAuthProbeService.acceptNoAuthProbe(this.id, this.selectedNIC.address)
          .pipe(
            tap(() => {
              this.pending$ = of(true);
            }),
            map((noauthProbes: NoAuthProbe) => {
              console.log(noauthProbes);
              this.back.emit();
            }),
            catchError(error => {
              this.error$ = of(error);
              return of();
            }),
            tap(() => {
              this.pending$ = of(false);
            }),
            take(1),
        ).subscribe();
      },
      reject: () => {

      }
    });
  }

  deny() {
    this.confirmationService.confirm({
      header: 'Are you sure to deny this Probe',
      message: 'It will be permanently deleted.',
      icon: 'fa fa-trash',
      accept: () => {
        this.noAuthProbeService.denyNoauthProbe(this.id)
          .pipe(
            tap(() => {
              this.pending$ = of(true);
            }),
            map((noauthProbes: NoAuthProbe) => {
              this.back.emit();
            }),
            catchError(error => {
              this.error$ = of(error);
              return of();
            }),
            tap(() => {
              this.pending$ = of(false);
            }),
            take(1),
        ).subscribe();
      },
      reject: () => {

      }
    });
  }
}
