import {Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef} from '@angular/core';
import {Observable, throwError} from 'rxjs';
import {Router} from '@angular/router';
import {TestingInformationService} from '../../../../../core/service/testing-information/testing-information.service';
import {SweetalertService} from '../../../../../core/service/sweetalert/sweetalert';
import {catchError, concatMap, filter, tap} from "rxjs/operators";
import {
  ConfirmRejectExamDialogComponent
} from '../../../reject-exam/presenter/dialog/confirm-reject-exam-dialog/confirm-reject-exam-dialog.component';
import {ConfirmApproveExamDialogComponent} from '../../presenter/dialog/confirm-approve-exam-dialog/confirm-approve-exam-dialog.component';
import {MatDialog} from '@angular/material/dialog';
import {ExamNotificationService} from '../../../../../core/service/testing-information/exam-notification.service';
import Swal from 'sweetalert2';

@Component({
  selector: 'app-approve-exam',
  templateUrl: './approve-exam.container.html',
  styleUrls: ['./approve-exam.container.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ApproveExamContainer implements OnInit {
  subjectList$ = new Observable();
  constructor(
    private router: Router,
    private TestinforSV: TestingInformationService,
    private ExamNotiSV: ExamNotificationService,
    private cdr: ChangeDetectorRef,
    private swSV: SweetalertService,
    private dialog: MatDialog,
  ) { }

  ngOnInit(): void {
    this.subjectList$ = this.TestinforSV.getisOpenNullandMaintrue();
  }

  search(query){
    if(query) return this.subjectList$ = this.TestinforSV.searchisOpenNullFalseandMaintrue(`?${query}`);
    if(!query) return this.subjectList$ = this.TestinforSV.getisOpenNullandMaintrue();
  }

  notification(){
    // Swal.fire({
    //   title: 'ส่งอัพเดทการแจ้งเตือน?',
    //   showDenyButton: true,
    //   showCancelButton: true,
    //   confirmButtonText: 'รูปแบบPOST',
    //   denyButtonText: `รูปแบบGET`,
    // }).then((result) => {
    //   if (result.isConfirmed) {
    //     console.log(1)
    //     this.ExamNotiSV.sendNotfiPost().pipe(
    //       tap(x => this.swSV.success('อัพเดทการแจ้งเตือนเสร็จสิ้น')),
    //       catchError(err => {
    //         this.swSV.errOpenExamDetailText(err.error);
    //         return throwError(err);
    //       })
    //     ).subscribe();
    //   } else if (result.isDenied) {
    //     console.log(2)
    //     // this.ExamNotiSV.sendNotiTest().pipe(
    //     //   tap(x => this.swSV.success('อัพเดทการแจ้งเตือนเสร็จสิ้น')),
    //     //   catchError(err => {
    //     //     this.swSV.errOpenExamDetailText(err.error);
    //     //     return throwError(err);
    //     //   })
    //     // ).subscribe();
    //   }
    // });
    this.ExamNotiSV.sendNotiTest();

  }

  edit(id){
    this.router.navigate(['create-exam-information/edit', id]);
  }

  open(id){
    this.swSV.confirmOpenExam().pipe(
      filter(x => x.isConfirmed),
      concatMap(x => {
        return this.TestinforSV.checkandopentest(id).pipe(
          tap(x => this.swSV.success('รอบสอบเปิดแล้ว')),
          tap(x => this.ExamNotiSV.sendNotiTest()),
          tap(x => this.subjectList$ = this.TestinforSV.getisOpenNullandMaintrue()),
          tap(x => this.cdr.detectChanges()),
          catchError(err => {
            this.swSV.errOpenExamDetailText(err.error);
            return throwError(err);
          })
        );
      })
    ).subscribe();
  }

  open2(id){
    const dialogRef = this.dialog.open(ConfirmApproveExamDialogComponent, {
      width: '550px',
      height: '250px',
      data: id,
      panelClass: 'custom-modalbox'
    });
    dialogRef.afterClosed().subscribe(result => {
      this.subjectList$ = this.TestinforSV.getisOpenNullandMaintrue();
    });
  }

}
