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

@Component({
  selector: 'app-random-list',
  templateUrl: './random-list.component.html',
  styleUrls: ['./random-list.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class RandomListComponent extends BaseList implements OnInit {
  randomList: any = [];
  data;
  constructor(
    public activeRoute: ActivatedRoute,
    private cdr: ChangeDetectorRef,
    private router: Router,
    private randomQuestionSV: RandomQuestionsService,
    private swSV: SweetalertService
  ) {
    super()
   }

  ngOnInit(): void {
    this.activeRoute.paramMap.pipe(
      concatMap((x:any) => this.randomQuestionSV.search('?subject_uid='+x.params.id+'&testing_information_uid='+x.params.testing_uid)),
      tap(x => this.data = x[0]),
      tap(x => this.randomList = this.updateMatTable(x))
    ).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 '-'
  }

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