import { Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Observable } from 'rxjs';
import { concatMap, tap } from 'rxjs/operators';
import { TestingInformationService } from 'src/app/core/service/testing-information/testing-information.service';
import { CandidatePaymentStatusService } from 'src/app/core/service/testing-register/candidate-payment-status.service';

@Component({
  selector: 'app-candidate-payment-status',
  templateUrl: './candidate-payment-status.container.html',
  styleUrls: ['./candidate-payment-status.container.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class CandidatePaymentStatusContainer implements OnInit {
  uid;
  testingInfoData;
  candidateList$ = new Observable()
  constructor(
    private activatedRoute: ActivatedRoute,
    private candidatePaymentStatus: CandidatePaymentStatusService,
    private testingInformationSV: TestingInformationService,
    private cdr: ChangeDetectorRef
  ) { }

  ngOnInit(): void {
    this.activatedRoute.params.pipe(
      tap(x => this.uid = x.id),
      tap(x => {
        this.candidateList$ = this.candidatePaymentStatus.search(`?testing_information_uid=${x.id}`);
      }),
      concatMap(x => this.testingInformationSV.get(this.uid)),
      tap(x => this.testingInfoData = x),
      tap(x => this.cdr.detectChanges())
    ).subscribe();
  }

  search(query){
    if(query) return this.candidateList$ = this.candidatePaymentStatus.search(`?${query}`);
    if(!query) return this.candidateList$ = this.candidatePaymentStatus.search(`?testing_information_uid=${this.uid}`);
  }

}
