import {Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef} from '@angular/core';
import {concatMap, tap} from "rxjs/operators";
import {ActivatedRoute, Router} from "@angular/router";
import {VEligibleCandidatesService} from "../../../../../core/service/testing-register/v-eligible-candidates.service";
import {SubjectsService} from "../../../../../core/service/settings/subjects.service";
import {BranchService} from "../../../../../core/service/settings/branch.service";
import {TestingFieldService} from "../../../../../core/service/settings/testing-field.service";
import {TestingCenterService} from "../../../../../core/service/TestingCenter/testing-center.service";
import {MatDialog} from "@angular/material/dialog";
import {Observable} from "rxjs";
import {DialogSyncComponent} from "../../../candidate-list/presenter/dialog/dialog-sync/dialog-sync.component";
import { TestingInformationService } from 'src/app/core/service/testing-information/testing-information.service';

@Component({
  selector: 'app-seat-change',
  templateUrl: './seat-change.container.html',
  styleUrls: ['./seat-change.container.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class SeatChangeContainer implements OnInit {
  testinginformationuid;
  subjectList2 = [];
  subjectList = [];
  subjectList1$ = new Observable();
  subjectList3 = [];
  subjectList4 = [];
  testingInfoData;
  constructor(
    private router: Router,
    public activeRouter: ActivatedRoute,
    private VECandidateSV: VEligibleCandidatesService,
    private subjectSV: SubjectsService,
    private branchSV: BranchService,
    private testingFieldSV: TestingFieldService,
    private testcenterSV: TestingCenterService,
    private dialog: MatDialog,
    private cdr: ChangeDetectorRef,
    private testingInfoSV: TestingInformationService
  ) { }

  ngOnInit(): void {
    this.activeRouter.params.pipe(
      tap(x => this.testinginformationuid = x.id),
      tap(x => {
        this.subjectList1$ = this.VECandidateSV.getAllByTestinUid(x.id);
      }),
      concatMap(x => this.testingInfoSV.get(this.testinginformationuid)),
      tap(x => this.testingInfoData = x),
      tap(x => this.cdr.detectChanges())
    ).subscribe();
    this.subjectSV.getAll().subscribe(
      result => this.subjectList4 = result
    );
    this.branchSV.getAll().subscribe(
      result => this.subjectList = result
    );
    this.testingFieldSV.getAll().subscribe(
      result => {
        this.subjectList2 = result;
      }
    );
    this.testcenterSV.getAll().subscribe(
      result => this.subjectList3 = result
    );
  }

  search(query){
    query += `&testing_information_uid=${this.testinginformationuid}`;
    if(query) return this.subjectList1$ = this.VECandidateSV.search(`?${query}`);
    if(!query) return this.subjectList1$ = this.VECandidateSV.getAllByTestinUid(this.testinginformationuid);
  }

  syncData(data){
    // console.log(id);
    const dialogRef = this.dialog.open(DialogSyncComponent, {
      width: '500px',
      height: '340px',
    });
    dialogRef.afterClosed().pipe(
      tap(x => {
        this.VECandidateSV.getAll().pipe(
          tap(y => this.subjectList1$ = this.VECandidateSV.getAll()),
          tap(y => this.cdr.detectChanges())
        ).subscribe();
      }),
    ).subscribe();
  }

}
