import { Component, Inject, OnInit } from '@angular/core';
import { FindedFile } from '../../../../interfaces/finded-files.interface';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';

@Component({
    selector: 'app-attached-list',
    templateUrl: './attached-list.component.html',
    styleUrl: './attached-list.component.css',
    standalone: false
})
export class AttachedListComponent implements OnInit {
  attachedFiles: FindedFile[];

  constructor(
    @Inject(MAT_DIALOG_DATA) private _data: any,
    private _dialogRef: MatDialogRef<AttachedListComponent>,
  ) {
    this.attachedFiles = [];
  }

  ngOnInit(): void {
    this.attachedFiles = this._data.files;
  }

  unattach(file: FindedFile){
    let attachedAux: FindedFile[] = [];
    this.attachedFiles.forEach(item => {
      let itemStr = `${item.CODIGO}=${item.VERSION}=${item.NOMBRE}`;
      let fileStr = `${file.CODIGO}=${file.VERSION}=${file.NOMBRE}`;
      
      if(itemStr != fileStr){
        attachedAux.push(item);
      }
    });

    this.attachedFiles = attachedAux;
    if(this.attachedFiles.length == 0){
      let attached = JSON.stringify(this.attachedFiles);
      this._dialogRef.close(attached);
    }
  }

  closeList(){
    let attached = JSON.stringify(this.attachedFiles);
    this._dialogRef.close(attached);
  }
}
