import {Component, OnInit} from '@angular/core';
import * as FA from '@fortawesome/free-solid-svg-icons';
import {BehaviorSubject} from 'rxjs';
import {Store} from '@ngxs/store';
import {ProjectState} from '@shared/stores/project-state/project-state';
import {UntilDestroy, untilDestroyed} from '@ngneat/until-destroy';
import {Pentest} from '@shared/models/pentest.model';
import {PentestService} from '@shared/services/api/pentest.service';
import {NotificationService} from '@shared/services/toaster-service/notification.service';
import {Router} from '@angular/router';
import {Route} from '@shared/models/route.enum';

@UntilDestroy()
@Component({
  selector: 'app-pentest-content',
  templateUrl: './pentest-content.component.html',
  styleUrls: ['./pentest-content.component.scss']
})
export class PentestContentComponent implements OnInit {
  // HTML only
  readonly fa = FA;

  pentest$: BehaviorSubject<Pentest> = new BehaviorSubject<Pentest>(null);
  currentNumberOfFindings$: BehaviorSubject<number> = new BehaviorSubject<number>(0);
  currentNumberOfComments$: BehaviorSubject<number> = new BehaviorSubject<number>(0);

  constructor(
    private readonly pentestService: PentestService,
    private notificationService: NotificationService,
    private router: Router,
    private store: Store) {
  }

  ngOnInit(): void {
    this.store.select(ProjectState.pentest).pipe(
      untilDestroyed(this)
    ).subscribe({
      next: (selectedPentest: Pentest) => {
        if (selectedPentest) {
          this.pentest$.next(selectedPentest);
          const findings = selectedPentest.findingIds ? selectedPentest.findingIds.length : 0;
          this.currentNumberOfFindings$.next(findings);
          const comments = selectedPentest.commentIds ? selectedPentest.commentIds.length : 0;
          this.currentNumberOfComments$.next(comments);
        } else {
          this.router.navigate([Route.PROJECT_OVERVIEW]);
        }
      },
      error: err => {
        console.error(err);
      }
    });
  }
}
