import { Component, inject } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DialogModule } from 'primeng/dialog';
import { ButtonModule } from 'primeng/button';
import { Subject } from 'rxjs';

export interface AlertDialogOptions {
  visible: boolean;
  title: string;
  message: string;
  icon: 'success' | 'error' | 'warning' | 'info' | 'question';
  showConfirmButton: boolean;
  confirmButtonText: string;
  showCancelButton: boolean;
  cancelButtonText: string;
}

@Component({
  selector: 'app-alert-dialog',
  standalone: true,
  imports: [CommonModule, DialogModule, ButtonModule],
  templateUrl: './alert-dialog.component.html',
  styleUrls: ['./alert-dialog.component.scss']
})
export class AlertDialogComponent {
  visible = false;
  title = '';
  message = '';
  icon: 'success' | 'error' | 'warning' | 'info' | 'question' = 'success';
  showConfirmButton = true;
  confirmButtonText = 'Aceptar';
  showCancelButton = false;
  cancelButtonText = 'Cancelar';

  private confirmSubject = new Subject<boolean>();
  public onConfirm = this.confirmSubject.asObservable();
  
  show(options: Partial<AlertDialogOptions> = {}): void {
    this.visible = true;
    this.title = options.title || this.title;
    this.message = options.message || this.message;
    this.icon = options.icon || this.icon;
    this.showConfirmButton = options.showConfirmButton !== undefined ? options.showConfirmButton : this.showConfirmButton;
    this.confirmButtonText = options.confirmButtonText || this.confirmButtonText;
    this.showCancelButton = options.showCancelButton !== undefined ? options.showCancelButton : this.showCancelButton;
    this.cancelButtonText = options.cancelButtonText || this.cancelButtonText;
  }

  confirm(): void {
    this.visible = false;
    this.confirmSubject.next(true);
  }

  cancel(): void {
    this.visible = false;
    this.confirmSubject.next(false);
  }

  getIconClass(): string {
    switch (this.icon) {
      case 'success':
        return 'pi-check-circle text-green-500';
      case 'error':
        return 'pi-times-circle text-red-500';
      case 'warning':
        return 'pi-exclamation-triangle text-yellow-500';
      case 'info':
        return 'pi-info-circle text-blue-500';
      case 'question':
        return 'pi-question-circle text-purple-500';
      default:
        return 'pi-check-circle text-green-500';
    }
  }
}