import { Component, Inject, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { MAT_DIALOG_DATA } from '@angular/material/dialog';
import { GetFailureByEquipment, GetFailureList } from 'src/app/interfaces/ANFA/failure.interface';

@Component({
  selector: 'app-failure-calculation',
  templateUrl: './failure-calculation.component.html',
  styleUrls: ['./failure-calculation.component.css']
})
export class FailureCalculationComponent implements OnInit {

  public formGroup: FormGroup;
  public arrYear: Array<number> = [];
  public arrMonth: Array<string> = ['Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'];

  constructor(@Inject(MAT_DIALOG_DATA) public failureList: GetFailureList) {
    for (let year = 2010; year < new Date().getFullYear() + 1; year++) {
      this.arrYear.push(year);
    }
    this.formGroup = this.createFormGroup();
  }

  ngOnInit(): void {
    this.setDataForm();

  }

  private setDataForm(){
    this.failure?.setValue(this.failureList.FALLA);
    let arrDataByAnnual: Array<number> = [];

    let arrYearRepeat:Array<number> = [];
    this.arrYear.forEach((year:number, indexYear) => {
      this.arrMonth.map((month:string, indexMonth:number) => {
        let contMonth:number = 0;
        this.failureList.LISTA.map((failure: GetFailureByEquipment) => {

          let arrDateFailure: Array<string> = failure.FECHA_FALLA.split('-');
          let yearFailure:number = parseInt(arrDateFailure[0]);
          let monthFailure:number = parseInt(arrDateFailure[1]) - 1;

          if (indexMonth === monthFailure) {
            contMonth++;
            if (yearFailure === year  && !(arrYearRepeat.includes(year)) ) {
              arrYearRepeat.push(year);
            }
          }

        });
        arrDataByAnnual.push(contMonth);
      });
    });


    let contMonth:number = 0;
    arrDataByAnnual.map((data:number) => {
      contMonth += data;
    });

    // Cálculo de Fallas al Año
    let calculateAnnual: number = ((contMonth * arrYearRepeat.length) / 12);
    let elementAnnual: string = `${ calculateAnnual.toFixed(2) } ${ calculateAnnual > 1 ? 'Fallas' : 'Falla'}`;
    this.annual?.setValue(elementAnnual);

    // Cálculo de Fallas al Mes
    let calculateMonthly:number = calculateAnnual / 12;
    let elementMonthly: string = `${ calculateMonthly.toFixed(2) } ${  calculateMonthly > 1 ? 'Fallas' : 'Falla'}`;
    this.monthly?.setValue( elementMonthly );

    // Cálculo de Fallas al la Semana
    let calculateWeek:number = (calculateAnnual / 12) / 4;
    let elementWeek: string = `${ calculateWeek.toFixed(2) } ${ calculateWeek > 1 ? 'Fallas' : 'Falla'}`;
    this.week?.setValue( elementWeek );

    // Cálculo de Fallas al Semestral
    let calculateBiannual:number = calculateAnnual / 2;
    let elementBiannual: string = `${ calculateBiannual.toFixed(2) } ${ calculateBiannual > 1 ? 'Fallas' : 'Falla'}`;
    this.biannual?.setValue( elementBiannual );

  }

  private createFormGroup() {
    return new FormGroup({
      failure: new FormControl('', Validators.required),
      week: new FormControl('', Validators.required),
      monthly: new FormControl('', Validators.required),
      biannual: new FormControl('', Validators.required),
      annual: new FormControl('', Validators.required)
    });
  }


  get failure()  { return this.formGroup.get('failure');}
  get week()  { return this.formGroup.get('week');}
  get monthly()  { return this.formGroup.get('monthly');}
  get biannual()  { return this.formGroup.get('biannual');}
  get annual()  { return this.formGroup.get('annual');}
}
