import { Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef, OnChanges, AfterViewInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { concatMap, filter, switchMap, tap } from 'rxjs/operators';
import { BaseSubList } from 'src/app/core/base/base-sub-list';
import { SweetalertService } from 'src/app/core/service/sweetalert/sweetalert';
import { RandomQuestionsService } from 'src/app/core/service/testing-information/random-questions.service';
import { TestingInformationService } from 'src/app/core/service/testing-information/testing-information.service';

@Component({
  selector: 'app-random-questions-subject-list',
  templateUrl: './random-questions-subject-list.component.html',
  styleUrls: ['./random-questions-subject-list.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class RandomQuestionsSubjectListComponent extends BaseSubList implements OnInit,AfterViewInit {
  column = ['1','2','3','4','5','6','7','8','9']
  subjectList;
  data;
  constructor(
    public activeRoute: ActivatedRoute,
    private testingInfoSV: TestingInformationService,
    private cdr: ChangeDetectorRef,
    private router: Router,
    private randomQuestionSV: RandomQuestionsService,
    private swSV: SweetalertService
  ) {
    super(activeRoute)
   }

  ngOnInit(): void {

  }

  ngAfterViewInit(){
    this.testingInfoSV.get(this.id).pipe(
      tap(x => this.data = x),
      tap((x:any) => this.subjectList = x.testing_subjects),
      tap(x => this.subjectList = this.updateMatTable(this.subjectList? this.subjectList:[])),
      tap(x => this.cdr.detectChanges())
    ).subscribe()
  }

  applyFilter(event: Event) {
    const filterValue = (event.target as HTMLInputElement).value;
    this.subjectList.filter = filterValue.trim().toLowerCase();
  }

  onEdit(id){
    this.router.navigate(['random-questions/setting',id,this.id])
  }

  onView(id){
    let data;
    this.randomQuestionSV.search('?subject_uid='+id+'&testing_information_uid='+this.data.testing_information_uid).pipe(
      tap(x => data = x[0]),
      tap(x => this.router.navigate(['random-questions/view',data.random_question_uid]))
    ).subscribe()
  }

  randomList(id){
    this.router.navigate(['random-questions/random',id,this.id])
  }

  onDelete(id){
    let data;
    this.randomQuestionSV.search('?subject_uid='+id+'&testing_information_uid='+this.data.testing_information_uid).pipe(
      tap(x => data = x[0]),
      tap(x => this.swSV.confirm().pipe(
        filter(x => x.isConfirmed),
        switchMap(x => {
          return this.randomQuestionSV.deleteData(data.random_question_uid).pipe(
            tap(x => this.swSV.removeSuccess()),
            tap(x => this.testingInfoSV.get(this.id).subscribe((x:any) => {
              this.subjectList.data = x.testing_subjects
              this.cdr.detectChanges()
            })),
            tap(x => this.cdr.detectChanges())
          )
        })
      ).subscribe()),
    ).subscribe()
  }

  randomType(type){
    if(type == 1) return 'ไม่มีการสลับข้อใด ๆ เลย'
    if(type == 2) return 'สลับข้อคำถาม แต่ตัวเลือกไม่สลับ'
    if(type == 3) return 'สลับตัวเลือก แต่ข้อคำถามไม่สลับ'
    if(type == 4) return 'สลับทุกอย่าง ทั้งข้อคำถามและตัวเลือก'
    return '-'
  }

  randomQuestionType(type){
    if(type == 1) return 'สุ่มแบบชุด'
    if(type == 2) return 'สุ่มแยกบท'
    if(type == 3) return 'สุ่มโดยอาจารย์'
    return '-'
  }

}
