import { Component, OnInit } from '@angular/core';
import { Submodules, SubmodulesResponse } from '../../interfaces/submodules.interface';
import { ResourcesService } from '../../services/resources.service';
import { InternationalizationService } from '../../services/internationalization/internationalization.service';
import { EncService } from '../../services/enc.service';
import { ModulesService } from '../../services/modules.service';
import { MatDialog } from '@angular/material/dialog';
import { Router } from '@angular/router';
import { lastValueFrom } from 'rxjs';
import { HttpErrorResponse } from '@angular/common/http';
import { SelectedFunctionComponent } from '../resources/selected-function/selected-function.component';

@Component({
    selector: 'app-process-management',
    templateUrl: './process-management.component.html',
    styleUrl: './process-management.component.css',
    standalone: false
})
export class ProcessManagementComponent implements OnInit {
  public arrSubmodules: Array<Submodules>;
  public isLoading: boolean;
  public hasError: boolean;
  public errorStr: string;
  
  constructor( 
    public resourcesService: ResourcesService, 
    public interService: InternationalizationService,
    private _encService: EncService, 
    private _modulesService: ModulesService,
    private _dialog: MatDialog,
    private _router: Router,
  ) {
    this.arrSubmodules = [];
    this.isLoading = false;
    this.hasError = false;
    this.errorStr = '';
  }

  async ngOnInit() {
    this.isLoading = true;
    const moduleEnc = await this._encService.encrypt('S002V01M03GEPR');
    const user: string = this.resourcesService.getUser();
    const line: number = this.resourcesService.getLineNumber();
    await lastValueFrom(this._modulesService.getSubmodules(moduleEnc, user, line)).then(
      async (responseData: SubmodulesResponse) => {
        if (!responseData.error) {

          this.arrSubmodules = [];
          for (const submodules of responseData.response) {
            this.arrSubmodules.push({
              IDSUBMODULO: await this._encService.decrypt(submodules.IDSUBMODULO),
              NOMBRESUBMODULO: submodules.NOMBRESUBMODULO,
              ICONOSUBMODULO: submodules.ICONOSUBMODULO,
              IMAGENSUBMODULO: submodules.IMAGENSUBMODULO,
            })
          }
          
        } else {
          this.errorStr = `${responseData.msg}`;
        }
      }, async (httpErrorResponse: HttpErrorResponse) => {
        let response = this.resourcesService.checkErrors(httpErrorResponse)
        if (response !== null && response.reload) {
          await this.ngOnInit();
        }
      }
    );
    this.isLoading = false;
  }

  public async openMenu(submodule: Submodules){
    this._dialog.open(SelectedFunctionComponent, {
      width: '560px',
      maxWidth: '560px',
      data: {
        module: 'S002V01M03GEPR',
        submodule: submodule.IDSUBMODULO,
      },
    }).afterClosed().subscribe(async (result) => {
      if (result) {
        let idFunc = result.IDFUNCION.replace("S002V01F", "");
        idFunc = idFunc.substring(2);
        
        let idSub = submodule.IDSUBMODULO;
        idSub = idSub.replace("S002V01S", "");
        idSub = idSub.replace("S002V02S", "");
        idSub = idSub.substring(2); 
        
        this._router.navigate([`sam/GEPR/${idSub}/${idFunc}`]);
      }
    });
  }
}
