import { Component, OnInit, AfterViewInit, ViewChild } from '@angular/core';
import { AfterContentInit, OnDestroy } from '@angular/core/src/metadata/lifecycle_hooks';
import { Router } from '@angular/router';
import { Store, select } from '@ngrx/store';
import { AuthSelector } from 'packages/member/store';
import { RPCClientError } from '@loafer/ng-rpc/protocol';
import { Domain } from '@overflow/commons-typescript/model/domain';
import * as ListStore from '../../store/noauth-probe';
import { NoAuthProbeSelector } from '../../store';
import { NoAuthProbe } from '@overflow/commons-typescript/model/noauth';
import { ConfirmationService, Message } from 'primeng/primeng';
import { MessageService } from 'primeng/components/common/messageservice';
import { Subscription } from 'rxjs/Subscription';

class NoauthProbeDesc {
  host: {
    name: string;
    os: string;
    platform: string;
    platformFamily: string;
    platformVersion: string;
    kernelVersion: string;
    hostID: string;
  };
  network: {
    name: string;
    address: string;
    gateway: string;
    macAddress: string;
  };
}

class NoauthProbeResult {
  id: number;
  data: NoAuthProbe;
  meta: NoauthProbeDesc;
}


@Component({
  selector: 'of-noauth-list',
  templateUrl: './list.component.html',
  providers: [ConfirmationService, MessageService]
})
export class ListComponent implements OnInit, AfterContentInit, OnDestroy {
  noAuthProbesSubscription$: Subscription;
  noAuthProbes$ = this.store.pipe(select(NoAuthProbeSelector.select('noAuthProbes')));
  noauthProbes: NoauthProbeResult[];
  msgs: Message[];


  constructor(
    private router: Router,
    private store: Store<ListStore.State>,
    private confirmationService: ConfirmationService,
    private messageService: MessageService
  ) {
  }

  ngOnInit() {
    this.noAuthProbesSubscription$ = this.noAuthProbes$.subscribe(
      (result: NoAuthProbe[]) => {
        if (result) {
          this.arrangeData(result);
        }
      },
      (error: RPCClientError) => {
        console.log(error.response.message);
      }
    );
  }

  ngAfterContentInit() {
    this.store.select(AuthSelector.select('domain')).subscribe(
      (domain: Domain) => {
        this.store.dispatch(new ListStore.ReadAllByDomain(domain));
      }
    );
  }

  ngOnDestroy() {
    this.noAuthProbesSubscription$.unsubscribe();
  }

  arrangeData(list) {
    if (!list || list.length === 0) {
      return;
    }
    this.noauthProbes = [];
    for (const np of list) {
      const desc: NoauthProbeDesc = JSON.parse(np.description);
      const result: NoauthProbeResult = {
        id: np.id,
        data: np,
        meta: desc
      };
      this.noauthProbes.push(result);
    }
  }

  onAcceptOrDeny(isAccept: boolean, selected: NoAuthProbe) {
    console.log(selected);
    this.msgs = [];
    const title = isAccept ?
      'Are you sure to accept this Probe?' : 'Are you sure to deny this Probe';
    const message = isAccept ?
      'Start collecting data as a Probe.' : 'It will be permanently deleted.';

    this.confirmationService.confirm({
      header: title,
      message: message,
      icon: isAccept ? 'fa-check' : 'fa fa-trash',
      accept: () => {
        isAccept ? this.handleAccept(selected) : this.handleDeny(selected);
      },
      reject: () => {
      }
    });
  }

  handleAccept(selected) {
    this.store.dispatch(new ListStore.Accept(selected));
  }

  handleDeny(selected) {
    this.store.dispatch(new ListStore.Deny(selected));
  }
}
