import { Component, Inject, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { ResourcesService } from '../../../../../services/resources.service';

@Component({
    selector: 'app-register-multiple-data',
    templateUrl: './register-multiple-data.component.html',
    styleUrl: './register-multiple-data.component.css',
    standalone: false
})
export class RegisterMultipleDataComponent implements OnInit {
  public arrItemsForm: Array<number>;
  public formGroup: FormGroup;
  
  constructor(
    @Inject(MAT_DIALOG_DATA) public registerSerial: RegisterSerial,
    public resourcesService: ResourcesService,
    private _dialogReg: MatDialogRef<RegisterSerial>,
  ) {
    this.arrItemsForm = [];
    this.formGroup = new FormGroup({ 'IS_UNIQUE': new FormControl(false) });
    if (this.registerSerial.type === 'SERIAL') {
      this.formGroup.addControl('SERIAL_0', new FormControl('', Validators.required));
      if (!this.registerSerial.arrModel.IS_UNIQUE) {
        for (let index = 1; index < registerSerial.numberItems; index++) {
          this.arrItemsForm.push(index);
          this.formGroup.addControl('SERIAL_'+index, new FormControl('', Validators.required));
        }
      }
      this.formGroup.setValue(registerSerial.arrModel);
    } else if (this.registerSerial.type === 'FECHA_VENCIMIENTO') {
      this.formGroup.addControl('DUE_DATE_0', new FormControl('', Validators.required));  
      if (!registerSerial.arrModel.IS_UNIQUE) {
        for (let index = 1; index < registerSerial.numberItems; index++) {
          this.arrItemsForm.push(index);
          this.formGroup.addControl('DUE_DATE_'+index, new FormControl('', Validators.required));
        }
      }
      this.formGroup.setValue(registerSerial.arrModel);
    } else if (this.registerSerial.type === 'BARCODE') {

      this.formGroup.addControl('BARCODE_0', new FormControl('', Validators.required));
      if (!this.registerSerial.arrModel.IS_UNIQUE) {
        for (let index = 1; index < registerSerial.numberItems; index++) {
          this.arrItemsForm.push(index);
          this.formGroup.addControl('BARCODE_'+index, new FormControl('', Validators.required));
        }
      }
      this.formGroup.setValue(registerSerial.arrModel);

    } else {
      this.resourcesService.openSnackBar(`Opción no disponible.`);
      this._dialogReg.close(false);
    }
  }

  ngOnInit(): void {
    this.formGroup.controls['IS_UNIQUE'].valueChanges.subscribe((result) => {
      if (this.registerSerial.type === 'SERIAL') {
        if (result) {
          for (let index = 1; index < this.registerSerial.numberItems; index++) {
            this.formGroup.removeControl('SERIAL_'+index);
          }
          this.arrItemsForm = [];
        } else {
          this.arrItemsForm = [];
          for (let index = 1; index < this.registerSerial.numberItems; index++) {
            this.arrItemsForm.push(index);
            this.formGroup.addControl('SERIAL_'+index, new FormControl('', Validators.required));
          }
        }
      } else if (this.registerSerial.type === 'FECHA_VENCIMIENTO') {
        if (result) {
          for (let index = 1; index < this.registerSerial.numberItems; index++) {
            this.formGroup.removeControl('DUE_DATE_'+index);
          }
          this.arrItemsForm = [];
        } else {
          this.arrItemsForm = [];
          for (let index = 1; index < this.registerSerial.numberItems; index++) {
            this.arrItemsForm.push(index);
            this.formGroup.addControl('DUE_DATE_'+index, new FormControl('', Validators.required));
          }
        }
      } else if (this.registerSerial.type === 'BARCODE') {
        if (result) {
          for (let index = 1; index < this.registerSerial.numberItems; index++) {
            this.formGroup.removeControl('BARCODE_'+index);
          }
          this.arrItemsForm = [];
        } else {
          this.arrItemsForm = [];
          for (let index = 1; index < this.registerSerial.numberItems; index++) {
            this.arrItemsForm.push(index);
            this.formGroup.addControl('BARCODE_'+index, new FormControl('', Validators.required));
          }
        }
      }
      this.formGroup.updateValueAndValidity();
    });
  }

  public save() {
    if (this.registerSerial.type === 'FECHA_VENCIMIENTO') {
      let arrObject: Array<string> = Object.keys(this.formGroup.value);
      for (let index = 0; index < arrObject.length - 1; index++) {
        let dueDate: Date = this.formGroup.controls['DUE_DATE_'+index].value;
        if (dueDate === null) {
          this.resourcesService.openSnackBar(`Ocurrió un error la obtener la fecha.`);
          return;
        }
        let strDueDate: string = this.resourcesService.getDateFormatCalendar(dueDate);        
        this.formGroup.controls['DUE_DATE_'+index].setValue(strDueDate);
      }
    }
    this._dialogReg.close({
      numberItems: this.registerSerial.numberItems,
      arrModel: this.formGroup.value
    });
  }
}

export interface RegisterSerial {
  type: 'SERIAL' | 'FECHA_VENCIMIENTO' | 'BARCODE';
  numberItems: number;
  arrModel: any;
  minDate: Date;
}