import { Location } from '@angular/common';
import { Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';
import { Router, ActivatedRoute } from '@angular/router';
import { throwError } from 'rxjs';
import { tap, catchError, filter, switchMap } from 'rxjs/operators';
import { BaseForm } from 'src/app/core/base/base-form';
import { SubjectsService } from 'src/app/core/service/settings/subjects.service';
import { SweetalertService } from 'src/app/core/service/sweetalert/sweetalert';
import { GradingService } from 'src/app/core/service/testing-information/grading.service';

@Component({
  selector: 'app-view',
  templateUrl: './view.component.html',
  styleUrls: ['./view.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ViewComponent extends BaseForm implements OnInit {
  grade;
  constructor(
    public router: Router,
    public fb: FormBuilder,
    public activeRoute: ActivatedRoute,
    public location: Location,
    private subjectSV: SubjectsService,
    private cdr: ChangeDetectorRef,
    private gradingSV: GradingService,
    private swSV: SweetalertService,
  ) {
    super(router,activeRoute,fb,location)
   }

  ngOnInit(): void {
    this.gradingSV.get(this.id).pipe(
      tap(x => this.grade = x),
      tap(x => this.setupForm(x)),
      tap(x => this.cdr.detectChanges())
    ).subscribe()
  }

  setupForm(x){
    this.form.patchValue(x)
    this.form.setControl('grading_details',this.fb.array(x?.grading_details.map(z => this.createDetail(z))))
  }
  createForm(){
    return this.fb.group({
      status_id: null,
      created_by: null,
      created_datetime: null,
      updated_by: null,
      updated_datetime: null,
      deleted_by: null,
      deleted_datetime: null,
      grading_uid: null,
      testing_information_uid: null,
      subject_uid: null,
      grading_characteristic: null,
      grading_type: null,
      announcement_type : [null,Validators.required],
      announcement_date: null,
      grading_details: this.fb.array([])
    })
  }

  createDetail(x){
    return this.fb.group({
      status_id: x.status_id,
      created_by: x.created_by,
      created_datetime: x.created_datetime,
      updated_by: x.updated_by,
      updated_datetime: x.updated_datetime,
      deleted_by: x.deleted_by,
      deleted_datetime: x.deleted_datetime,
      grading_detail_uid: x.grading_detail_uid,
      grading_uid: x.grading_uid,
      grading_name: x.grading_name,
      grade_start: x.grade_start,
      grade_end: x.grade_end,
    })
  }

  save(){
    const form = this.form.getRawValue()
    this.gradingSV.add(form).pipe(
      tap(x => this.swSV.saveSuccess()),
      tap(x => this.location.back()),
      catchError(err => {
        this.swSV.err()
        return throwError(err)
      })
    ).subscribe()
  }

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

  onDelete(){
    this.swSV.confirm().pipe(
      filter(x => x.isConfirmed),
      switchMap(x => {
        return this.gradingSV.deleteData(this.id).pipe(
          tap(x => this.swSV.removeSuccess()),
          tap(x => this.location.back()),
          tap(x => this.cdr.detectChanges())
        )
      })
    ).subscribe()
  }

}
