import { AfterViewInit, Component, Inject, OnInit, ViewChild } from '@angular/core';
import { MatTableDataSource } from '@angular/material/table';
import { GetHistoryNotificateTask, ResponseDataGetHistoryNotificateTask } from '../../../../interfaces/process-managementv/workflow-management.interface';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { MAT_DIALOG_DATA } from '@angular/material/dialog';
import { ResourcesService } from '../../../../services/resources.service';
import { InternationalizationService } from '../../../../services/internationalization/internationalization.service';
import { ProcessManagementService } from '../../../../services/process-management/process-management.service';
import { EncService } from '../../../../services/enc.service';
import { lastValueFrom } from 'rxjs';
import { HttpErrorResponse } from '@angular/common/http';

@Component({
    selector: 'app-history-notificate-task',
    templateUrl: './history-notificate-task.component.html',
    styleUrl: './history-notificate-task.component.css',
    standalone: false
})
export class HistoryNotificateTaskComponent implements OnInit, AfterViewInit {
  public dataSourceValidate: MatTableDataSource<GetHistoryNotificateTask>;
  public dataSourceNotificate: MatTableDataSource<GetHistoryNotificateTask>;
  public displayedColumns: Array<string>;
  @ViewChild('paginatorValidator') paginatorValidator!: MatPaginator;
  @ViewChild('sortValidator') sortValidator!: MatSort;
  
  @ViewChild('paginatorNotificate') paginatorNotificate!: MatPaginator;
  @ViewChild('sortNotificate') sortNotificate!: MatSort;
  
  public isLoading: boolean;
  
  constructor(
    @Inject(MAT_DIALOG_DATA) public idExecuteTask: number,
    public resourcesService: ResourcesService,
    public interService: InternationalizationService,
    private _processManagementService: ProcessManagementService,
    private _encService: EncService,
  ) {
    this.dataSourceValidate = new MatTableDataSource<GetHistoryNotificateTask>();
    this.dataSourceNotificate = new MatTableDataSource<GetHistoryNotificateTask>();
    this.displayedColumns = [
      'ID_EJECUCION_NOTIFICACION',
      'USUARIO_RESPONSABLE',
      'ESTADO',
      'USUARIO_MODIFICA',
      'FECHA_MODIFICA',
    ];
    this.isLoading = false;
  }
  
  async ngOnInit() {
    await this.getHistoryNotificateTask();
  }

  ngAfterViewInit(): void {
    this.dataSourceValidate.paginator = this.paginatorValidator;
    this.dataSourceValidate.sort = this.sortValidator;

    this.dataSourceNotificate.paginator = this.paginatorNotificate;
    this.dataSourceNotificate.sort = this.sortNotificate;
  }

  public async getHistoryNotificateTask() {
    this.isLoading = true;
    const encExecuteTask: string = await this._encService.encrypt(this.idExecuteTask.toString());
    const user: string = this.resourcesService.getUser();
    const line: number = this.resourcesService.getLineNumber();
    await lastValueFrom(this._processManagementService.getHistoryNotificateTask(encExecuteTask, user, line)).then(
      (responseData: ResponseDataGetHistoryNotificateTask) => {
        if (!responseData.error) {
          const arrValidateTask:Array<GetHistoryNotificateTask> =  responseData.response.filter(
            (historyNotificateTask: GetHistoryNotificateTask) => historyNotificateTask.TIPO_NOTIFICACION === 'Para validación'
          );
          const arrNotificateTask:Array<GetHistoryNotificateTask> =  responseData.response.filter(
            (historyNotificateTask: GetHistoryNotificateTask) => historyNotificateTask.TIPO_NOTIFICACION === 'Solo notificación'
          );

          this.dataSourceValidate.data = arrValidateTask;
          this.dataSourceNotificate.data = arrNotificateTask;
        } else {
          this.resourcesService.openSnackBar(`${responseData.msg}`);
        }
      }, async (httpErrorResponse: HttpErrorResponse) => {
        let response = this.resourcesService.checkErrors(httpErrorResponse)
        if (response !== null && response.reload) {
          await this.getHistoryNotificateTask();
        }
      }
    );
    this.isLoading = false;
  }
}
