import { HttpErrorResponse } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { MatTableDataSource } from '@angular/material/table';
import { lastValueFrom } from 'rxjs';
import { AlarmEmissionFromFailures, ResponseDataGetAlarmEmissionFromFailures } from 'src/app/interfaces/failure-analysis/failure.interface';
import { FailureLogService } from 'src/app/services/failure-analysis/failure-log.service';
import { ResourcesService } from 'src/app/services/resources/resources.service';

@Component({
  selector: 'app-alarm-emission',
  templateUrl: './alarm-emission.component.html',
  styleUrls: ['./alarm-emission.component.css']
})
export class AlarmEmissionComponent implements OnInit{
  public dataSource: MatTableDataSource<AlarmEmissionFromFailures>;
  public displayedColumns: Array<string>;

  public isLoading: boolean;
  public isLoadingForm: boolean;

  constructor(
    public resourcesService: ResourcesService,
    private _dialog: MatDialog,
    private _failureLogService: FailureLogService,
  ) {
    this.dataSource = new MatTableDataSource<AlarmEmissionFromFailures>();
    this.displayedColumns = [
      'NUMERO_FALLA',
      'CODIGO_EQUIPAMIENTO',
      'ID_ACTIVADOR',
      'FECHA_FALLA',
      'PRIORIDAD',
      'TIPO_ACTIVADOR',
    ];
    this.isLoading = false;
    this.isLoadingForm = false;
  }
  
  async ngOnInit() {
    this.isLoading = true;
    this.isLoadingForm = true;
    await this.getAlarmEmissionFromFailures()
    this.isLoading = false;
    this.isLoadingForm = false;
  }

  public async getAlarmEmissionFromFailures() {
    await lastValueFrom(this._failureLogService.getAlarmEmissionFromFailures()).then(
      (responseData: ResponseDataGetAlarmEmissionFromFailures) => {
        if ( !responseData.error ) {
          this.dataSource.data = responseData.response;
        } else {
          this.resourcesService.openSnackBar(`${responseData.msg}`);
        }
      }, (httpErrorResponse: HttpErrorResponse) => this.resourcesService.checkErrors(httpErrorResponse)
    )
  }
}
