import { Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import {BaseForm} from '../../../../../../core/base/base-form';
import {FormArray, FormBuilder, FormGroup, Validators} from '@angular/forms';
import {ActivatedRoute, Router} from '@angular/router';
import {formatDate, Location} from '@angular/common';
import {AcademicSemesterService} from '../../../../../../core/service/settings/academic-semester.service';
import {AcademicYearService} from '../../../../../../core/service/settings/academic-year.service';
import {TestingInformationService} from '../../../../../../core/service/testing-information/testing-information.service';
import {SweetalertService} from '../../../../../../core/service/sweetalert/sweetalert';
import {catchError, tap} from 'rxjs/operators';
import {throwError} from 'rxjs';

@Component({
  selector: 'app-create-exam-condition-form',
  templateUrl: './create-exam-condition-form.component.html',
  styleUrls: ['./create-exam-condition-form.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class CreateExamConditionFormComponent extends BaseForm implements OnInit {
  // MainForm
  setexamform: FormGroup;
  testingInfoData;
  public model = [
    // new Date('2/8/2022'),
    // new Date('2/9/2022'),
    // new Date('2/11/2022'),
  ];
  sectionsFakeFalse = [
    {
      status_id: 1,
      testing_center_period_uid: null,
      period_id: 1,
      is_check: false,
      start_time: '09:00'
    },
    {
      status_id: 1,
      testing_center_period_uid: null,
      period_id: 2,
      is_check: false,
      start_time: '12:00'
    },
    {
      status_id: 1,
      testing_center_period_uid: null,
      period_id: 3,
      is_check: false,
      start_time: '15:00'
    },
    {
      status_id: 1,
      testing_center_period_uid: null,
      period_id: 4,
      is_check: false,
      start_time: '18:00'
    }
  ];
  constructor(
    public fb: FormBuilder,
    public formBuilder: FormBuilder,
    public router: Router,
    public activeRoute: ActivatedRoute,
    public location: Location,
    private AcademicSemSV: AcademicSemesterService,
    private AcademicYearSV: AcademicYearService,
    private TestInformationSV: TestingInformationService,
    private swSV: SweetalertService,
    private cdr: ChangeDetectorRef
  ) {
    super(
      router,
      activeRoute,
      fb,
      location
    );
    this.setexamform = this.formBuilder.group({
      status_id: null,
      created_by: null,
      created_datetime: null,
      updated_by: null,
      updated_datetime: null,
      deleted_by: null,
      deleted_datetime: null,
      testing_information_uid: null,
      testing_information_name: [null, Validators.required],
      testing_information_code: null,
      academic_semester_code: [null, Validators.required],
      academic_year_code: [null, Validators.required],
      testing_information_status: null,
      type_of_examinee: [null, Validators.required],
      is_student_code: false,
      first_student_code: '',
      last_student_code: '',
      start_datetime: null,
      end_datetime: null,
      registration_start_datetime: null,
      registration_end_datetime: null,
      testing_start_datetime: null,
      testing_end_datetime: null,
      is_number_of_times_testing: false,
      is_online_register: true,
      number_of_times_testing: null,
      times_the_score_is_calculated: 1,
      is_main: true,
      main_testing_information_uid: null,
      registration_type: 1,
      is_open: null,
      is_upload_document: false,
      is_register_verify: false,
      is_register: false,
      is_counter: false,
      days_without_testings: this.formBuilder.array([
        this.block_days(null)
      ]),
      testing_calendars: this.formBuilder.array([
        this.testing_calendars(null)
      ]),
      testing_subjects: this.formBuilder.array([
        this.testing_subjects()
      ]),
      testing_rooms: this.formBuilder.array([
        this.testing_rooms()
      ]),
      testing_periods: this.formBuilder.array([
        this.testing_periods()
      ])
    });
  }

  block_days(blockdays?){
    return this.formBuilder.group({
      status_id: null,
      testing_information_uid: blockdays?.testing_information_uid,
      days_without_testing_uid: blockdays?.days_without_testing_uid,
      date_without_testing: blockdays?.date_without_testing,
    });
  }

  testing_calendars(calender?){
    return this.formBuilder.group({
      status_id: null,
      testing_calendar_uid: calender?.testing_calendar_uid,
      start_datetime: calender?.start_datetime,
      end_datetime: calender?.end_datetime,
      calendar_type_uid: calender?.calendar_type_uid,
      calendar_type_name_th: calender?.calendar_type_name_th,
      testing_information_uid: calender?.testing_information_uid,
    });
  }

  testing_subjects(data?){
    console.log(data)
    return this.formBuilder.group({
      status_id: null,
      // created_by: null,
      // created_datetime: null,
      // updated_by: null,
      // updated_datetime: null,
      // deleted_by: null,
      // deleted_datetime: null,
      testing_subject_uid: data?.testing_subject_uid,
      subject_uid: data?.subject_uid,
      subject_code: data?.subject_code,
      subject_name_th: data?.subject_name_th,
    });
  }

  testing_periods(data?){
    return this.formBuilder.group({
      status_id: null,
      // created_by: null,
      // created_datetime: null,
      // updated_by: null,
      // updated_datetime: null,
      // deleted_by: null,
      // deleted_datetime: null,
      // deleted_datetime: null,
      is_check: data?.is_check,
      testing_information_uid: data?.testing_information_uid,
      testing_period_uid: data?.testing_period_uid,
      period_id: data?.period_id,
      start_time: data?.start_time,
    });
  }

  testing_rooms(data?){
    return this.formBuilder.group({
      testing_room_uid: data?.testing_room_uid,
      testing_center_uid: data?.testing_center_uid,
      // testing_center_name_th: data?.testing_center_name_th,
      branch_uid: data?.branch_uid,
      branch_name_th: data?.branch_name_th,
      testing_field_uid: data?.testing_field_uid,
      testing_information_uid: data?.testing_information_uid,
      testing_field_name_th: data?.testing_field_name_th,
      testing_room_name_th: data?.testing_room_name_th,
      testing_auth_name: data?.testing_auth_name,
      is_random_seat: data?.is_random_seat,
      testing_seats: this.formBuilder.array([
        this.testing_seats(data?.testing_seats)
      ]),
    });
  }

  testing_seats(data?){
    return this.formBuilder.group({
      status_id: null,
      // created_by: null,
      // created_datetime: null,
      // updated_by: null,
      // updated_datetime: null,
      // deleted_by: null,
      // deleted_datetime: null,
      no_of_seats: data?.no_of_seats,
      row_name: data?.row_name,
      testing_seat_uid: data?.testing_seat_uid,
      testing_center_row_uid: data?.testing_center_row_uid,
      is_specify_subject: data?.is_specify_subject,
      testing_room_uid: data?.testing_room_uid,
      testing_seat_subjects: this.formBuilder.array([
        this.testing_seat_subjects(data?.testing_seat_subjects)
      ]),
    });
  }

  testing_seat_subjects(data?){
    return this.formBuilder.group({
      status_id: null,
      testing_seat_subject_uid: data?.testing_seat_subject_uid,
      subject_uid: data?.subject_uid,
      subject_code: data?.subject_code,
      subject_name_th: data?.subject_name_th,
      start_seat_no: data?.start_seat_no,
      end_seat_no: data?.end_seat_no,
      testing_seat_uid: data?.testing_seat_uid,
    });
  }

  ngOnInit(): void {
    const form = this.setexamform.get('testing_calendars') as FormArray;
    const form2 = this.setexamform.get('testing_subjects') as FormArray;
    const form3 = this.setexamform.get('testing_rooms') as FormArray;
    const form4 = this.setexamform.get('testing_periods') as FormArray;
    const form5 = this.setexamform.get('days_without_testings') as FormArray;
    switch (this.state) {
      case 'edit':
        this.TestInformationSV.get(this.id).pipe(
          tap(x => this.testingInfoData = x),
          tap(x =>  {
            form.clear();
            form2.clear();
            form3.clear();
            form4.clear();
            form5.clear();
          }),
          tap(x => {
            const data: any = x;
            console.log(data);
            this.setexamform.patchValue(data);
            if (data.type_of_examinee === 'บุคคลทั่วไป'){
              this.setexamform.get('registration_type').setValue(3)
            }else if(data.type_of_examinee === 'นักศึกษา') {
              this.setexamform.get('registration_type').setValue(1)

            }else if(data.type_of_examinee === 'นักศึกษาโครงการพิเศษ/บุคคลทั่วไปแบบโครงการ') {
              this.setexamform.get('registration_type').setValue(2)
            }
          }),
          tap(x => this.add3Edit(x)),
          tap(x => this.addCalendaEdit(x)),
          tap(x => this.addPeriodEdit(x)),
          tap(x => this.addSubject(x)),
          tap(x => this.addEditBlockadd(x)),
          tap(x => this.cdr.detectChanges())
        ).subscribe();
        break;
    }
  }

  add3Edit(data){
    const form = this.setexamform.get('testing_rooms') as FormArray;
    for (let i = 0; i < data.testing_rooms.length; i++) {
      form.push(this.testing_rooms(data.testing_rooms[i]));
      const formArray = form.controls[i].get('testing_seats') as FormArray;
      const FormArraySeat = form.controls[i].get('testing_seat_subjects') as FormArray;
      formArray.clear();
      for (let j = 0; j < data.testing_rooms[i].testing_seats.length; j++) {
        formArray.push(this.testing_seats(data.testing_rooms[i].testing_seats[j]));
        const formArraySeatSubject = formArray.controls[j].get('testing_seat_subjects') as FormArray;
        formArraySeatSubject.clear();
        for (let k = 0; k < data.testing_rooms[i].testing_seats[j].testing_seat_subjects.length; k++) {
          formArraySeatSubject.push(this.testing_seat_subjects(data.testing_rooms[i].testing_seats[j].testing_seat_subjects[k]));
        }
      }
    }
  }

  addCalendaEdit(x){
    const form2 = this.setexamform.get('testing_calendars') as FormArray;
    for (let i = 0; i < x.testing_calendars.length; i++) {
      x.testing_calendars[i].end_datetime = formatDate(x.testing_calendars[i].end_datetime, 'yyyy-MM-dd', 'en-US');
      x.testing_calendars[i].start_datetime = formatDate(x.testing_calendars[i].start_datetime, 'yyyy-MM-dd', 'en-US');
      form2.push(this.testing_calendars(x.testing_calendars[i]));
    }
  }

  addPeriodEdit(x){
    const form = this.setexamform.get('testing_periods') as FormArray;
    for (let i = 0; i < x.testing_periods.length; i++) {
      const foundIndex = this.sectionsFakeFalse.findIndex(v => v.period_id === x.testing_periods[i].period_id);
      this.sectionsFakeFalse[foundIndex].is_check = true;
      this.sectionsFakeFalse[foundIndex].start_time = x.testing_periods[i].start_time;
    }
    for (let i = 0; i < this.sectionsFakeFalse.length; i++) {
      form.push(this.testing_periods(this.sectionsFakeFalse[i]));
    }
  }

  addEditBlockadd(x){
    const form = this.setexamform.get('days_without_testings') as FormArray;
    for (let i = 0; i < x.days_without_testings.length; i++) {
      x.days_without_testings[i].date_without_testing = formatDate(x.days_without_testings[i].date_without_testing, 'yyyy-MM-dd', 'en-US')
      form.push(this.block_days(x.days_without_testings[i]));
      this.model.push(x.days_without_testings[i].date_without_testing);
    }
  }

  addSubject(x){
    const form = this.setexamform.get('testing_subjects') as FormArray;
    for (let i = 0; i < x.testing_subjects.length; i++) {
      form.push(this.testing_subjects(x.testing_subjects[i]));
    }
  }

  changeRegisterType(){
    const regisType = this.setexamform.get('registration_type').value;
    // this.setexamform.get('is_register_verify').setValue(false);
    // this.setexamform.get('is_register').setValue(false);
    // this.setexamform.get('is_counter').setValue(false);
    // this.setexamform.get('is_upload_document').setValue(false);
  }

  save(){
    switch (this.state) {
      case 'edit':
        const form1 = this.setexamform.getRawValue();
        form1.testing_periods = form1.testing_periods.filter((member) => {
          return member.is_check === true;
        });
        this.TestInformationSV.update2(form1).pipe(
          tap(x => this.swSV.updateSuccess()),
          tap(x => window.history.back()),
          catchError(err => {
            this.swSV.err();
            return throwError(err);
          })
        ).subscribe();
        break;
    }
  }

  back(){
    this.location.back()
  }

}
