import { formatNumber } from '@angular/common';
import { Component, OnInit, ChangeDetectionStrategy, ElementRef, EventEmitter, Input, Output, ViewChild } from '@angular/core';
import { FormArray, FormGroup, FormBuilder } from '@angular/forms';
import { SweetalertService } from 'src/app/core/service/sweetalert/sweetalert';
import { environment } from 'src/environments/environment';

@Component({
  selector: 'teacher',
  templateUrl: './teacher.component.html',
  styleUrls: ['./teacher.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class TeacherComponent implements OnInit {
  getArr(){return this.form.get('arr') as FormArray}
  @ViewChild('input') input: ElementRef;
  @Output() addTeacher = new EventEmitter()
  @Output() removeTeacher = new EventEmitter()
  @Input() form: FormGroup;
  @Input() index;
  @Input() category;
  @Input() first;
  @Input() check;
  @Input() subject;
  @Output() isButtonDisabled = new EventEmitter()
  @ViewChild('porranaiEasy') multiple_choice_easy: ElementRef;
  @ViewChild('auttanaiEasy') subjective_easy: ElementRef;
  @ViewChild('porranaiMedium') multiple_choice_medium: ElementRef;
  @ViewChild('auttanaiMedium') subjective_medium: ElementRef;
  @ViewChild('porranaiHard') multiple_choice_hard: ElementRef;
  @ViewChild('auttanaiHard') subjective_hard: ElementRef;
  instructor;
  data;
  constructor(
    public fb: FormBuilder,
    private swSV: SweetalertService
  ) { }

  ngOnInit(): void {
    this.instructor = this.category[0].instructor_detail
    if(!environment.hideForm)console.log('ข้อมูลสุ่มโดยอาจารย์ : ',this.instructor)
  }

  add(){
    this.addTeacher.emit()
  }

  isMoreThan360(event){
    if(event.target.valueAsNumber > 360) return this.check.get('time_to_complete_exam').setValue(360)
  }

  remove(){
    this.removeTeacher.emit(this.index)
  }

  // totalQuestion(x){
  //   const total_question = this.form.get('num_question').value
  //   if(x > total_question){
  //     this.form.get('total_question').setValue(null)
  //     this.input.nativeElement.value = null
  //     this.swSV.errText('ใส่จำนวนคำถามเกินกำหนด')
  //   }
  //   if(x <= total_question) return this.form.get('total_question').setValue(+x)
  // }

  sumTotal(){
    return this.form.get('subjective_easy').value +
    this.form.get('multiple_choice_easy').value +
    this.form.get('subjective_medium').value +
    this.form.get('multiple_choice_medium').value +
    this.form.get('subjective_hard').value +
    this.form.get('multiple_choice_hard').value
  }

  checkValidation(type,input){
    const data = this.form.getRawValue()
    const level = type.split('_')[type.split('_').length-1]
    const isSubjective = type.split('_').includes('subjective')
    this.form.get('total_question').setValue(this.sumTotal())
    if(this.sumTotal() > data.num_question){
      this.swSV.errText('จำนวนสุ่มเกินกำหนด')
      this[type].nativeElement.value = 0
      this.form.get(type).setValue(0)
      this.form.get('total_question').setValue(this.sumTotal())
      this.form.get('total_random').setValue(this.sumTotal() == null? 0 : this.sumTotal())
    }else if (input > data[(isSubjective? 'total_question_auttanai_' : 'total_question_porranai_')+level]){
      this.swSV.errText('จำนวนสุ่มเกินกำหนด')
      this[type].nativeElement.value = 0
      this.form.get(type).setValue(0)
      this.form.get('total_question').setValue(this.sumTotal())
      this.form.get('total_random').setValue(this.sumTotal())
    }
  }

  inputCheck(input,type){
    this.form.get(type).setValue(+input)
    this.checkValidation(type,input)
    this.isButtonDisabled.emit(this.checkCondition())
  }

  selectInsturctor(x){
    const form = this.check.getRawValue()
    const dub = form.random_question_details.find(a => a.instructor_uid == x.instructor_uid)
    if(dub){
      this.swSV.errText('มีข้อมูลซ้ำ')
      this.remove()
    } else {
      this.form.patchValue(x)
      this.form.get('instructor_uid').setValue(x.instructor_uid)
      this.form.get('num_question').setValue(x.total_question)
      this.form.get('instructor_name').setValue(x.instructor_name_th)
      this.form.get('total_question').setValue(0)
      this.isButtonDisabled.emit(this.checkCondition())
    }
  }

  isNull(form){
    // const form = this.getArr().at(this.index).value
    // const form = this.getArr().value
    const isEnabledButton = form.total_question > 0
    // const multipleEasy = form.porranai_easy == 0? false : form.multiple_choice_easy == 0
    // const multipleMedium = form.porranai_medium == 0? false : form.multiple_choice_medium == 0
    // const multipleHard = form.porranai_hard == 0? false : form.multiple_choice_hard == 0
    // const subjectiveEasy = form.auttanai_easy == 0? false : form.subjective_easy == 0
    // const subjectiveMedium = form.auttanai_medium == 0? false : form.subjective_medium == 0
    // const subjectiveHard = form.auttanai_hard == 0? false : form.subjective_hard == 0
    // const conditionArray = [multipleEasy,multipleHard,multipleMedium,subjectiveEasy,subjectiveHard,subjectiveMedium]
    // const isEnabledButton = conditionArray.some(condition => condition == true)
    return isEnabledButton
  }

  checkCondition(){
    const form = this.check.get('random_question_details').value
    const conditionArray = form.map(data => this.isNull(data))
    const isEnabledButton = conditionArray.some(condition => condition == true)
    return !isEnabledButton
  }


}


