import {Component, Input, OnInit} from '@angular/core';
import {Calibrator} from "../../model/calibrator";
import {Flux} from "../../model/flux";
import {FormControl, FormGroup, Validators} from "@angular/forms";
import {CalibratorsService} from "../../services/calibrators.service";
import {verticleSlide} from "../../animations";
import {faTimes} from "@fortawesome/free-solid-svg-icons";

@Component({
  selector: 'app-calibrator',
  templateUrl: './calibrator.component.html',
  styleUrls: ['./calibrator.component.scss'],
  animations: [verticleSlide]
})
export class CalibratorComponent implements OnInit {

  @Input() calibrator: Calibrator;
  @Input() flux: Flux;

  public faTimes = faTimes;

  formGroup = new FormGroup({
    frequency: new FormControl('', [Validators.required, Validators.pattern('^[0-9\.]+$')]),
    flux: new FormControl('', [Validators.required, Validators.pattern('^[0-9\.]+$')]),
    spectralIndex: new FormControl('', [Validators.required, Validators.pattern('^[0-9\.]+$')]),
    uvmin: new FormControl('', Validators.pattern('^[0-9\.]+$')),
    uvmax: new FormControl('', Validators.pattern('^[0-9\.]+$'))
  });

  detailsExposed: boolean = false;

  constructor(private calibratorService: CalibratorsService) {
  }

  ngOnInit() {
    this.formGroup.setValue({
      frequency: this.flux.frequency,
      flux: this.flux.flux,
      spectralIndex: this.flux.spectralIndex,
      uvmin: this.flux.uvMin,
      uvmax: this.flux.uvMax
    });
  }

  onSubmit() {
    this.calibratorService.saveCalibratorFlux(
      this.calibrator.sctId,
      this.flux.id,
      this.formGroup.get('frequency').value,
      this.formGroup.get('flux').value,
      this.formGroup.get('spectralIndex').value,
      this.formGroup.get('uvmin').value,
      this.formGroup.get('uvmax').value
    ).subscribe(response => {
      console.log('comp resp: ', response);
      response.observingDate = this.flux.observingDate;
      this.flux = response;
      this.detailsExposed = false;
    });
  }

}