import { Component, Inject } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { InternationalizationService } from '../../../../services/internationalization/internationalization.service';
import { Workflow } from '../../../../interfaces/process-managementv/workflow-management.interface';

@Component({
  selector: 'app-workflow-details',
  templateUrl: './workflow-details.component.html',
  styleUrl: './workflow-details.component.css',
  standalone: false,
})
export class WorkflowDetailsComponent {
  public workflowData: Workflow;

  constructor(
    public dialogRef: MatDialogRef<WorkflowDetailsComponent>,
    @Inject(MAT_DIALOG_DATA) public data: { workflowData: Workflow },
    public interService: InternationalizationService
  ) {
    this.workflowData = data.workflowData;
    console.log(this.workflowData);
  }

  public close(): void {
    this.dialogRef.close();
  }

  public formatDate(dateString: string): string {
    // Formato: "15-01-2024 08:30:01 AM"
    const [datePart, timePart, ampm] = dateString.split(' ');
    const [day, month, year] = datePart.split('-');
    const [hour, minute, second] = timePart.split(':');
    
    let hour24 = parseInt(hour);
    if (ampm === 'PM' && hour24 !== 12) hour24 += 12;
    if (ampm === 'AM' && hour24 === 12) hour24 = 0;
    
    const date = new Date(parseInt(year), parseInt(month) - 1, parseInt(day), hour24, parseInt(minute), parseInt(second));
    const now = new Date();
    const diffTime = Math.abs(now.getTime() - date.getTime());
    const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));

    if (diffDays <= 7) {
      if (diffDays === 0) return this.interService.get('hoy');
      if (diffDays === 1) return this.interService.get('hace_1_dia');
      return this.interService.get('hace_dias').replace('{0}', diffDays.toString());
    } else {
      return `${year}-${month}-${day}`;
    }
  }
}
