import { Injectable } from '@angular/core';
import { Subject, Observable } from 'rxjs';
import { LoggerService } from './logger.service';

export interface ModalConfig {
  title?: string;
  message: string;
  type?: 'info' | 'warning' | 'error' | 'success' | 'confirm' | 'prompt';
  confirmText?: string;
  cancelText?: string;
  showCancel?: boolean;
  inputLabel?: string;
  inputPlaceholder?: string;
  inputType?: string;
  inputValue?: string;
}

export interface ModalResult {
  confirmed: boolean;
  inputValue?: string;
}

@Injectable({
  providedIn: 'root'
})
export class ModalService {
  private modalSubject = new Subject<ModalConfig | null>();
  private resultSubject = new Subject<ModalResult>();

  constructor(private logger: LoggerService) {}

  showModal(config: ModalConfig): Observable<ModalResult> {
    this.logger.debug('ModalService: showModal called with config:', config);
    this.modalSubject.next(config);
    return this.resultSubject.asObservable();
  }

  getModal(): Observable<ModalConfig | null> {
    return this.modalSubject.asObservable();
  }

  closeModal(result: ModalResult): void {
    this.modalSubject.next(null);
    this.resultSubject.next(result);
  }

  // Convenience methods
  alert(message: string, type: 'info' | 'warning' | 'error' | 'success' = 'info', title?: string): Promise<void> {
    return new Promise((resolve) => {
      const subscription = this.showModal({
        title: title || this.getDefaultTitle(type),
        message,
        type,
        showCancel: false,
        confirmText: 'OK'
      }).subscribe(() => {
        subscription.unsubscribe();
        resolve();
      });
    });
  }

  confirm(message: string, title: string = 'Confirm Action', confirmText: string = 'Confirm', cancelText: string = 'Cancel'): Promise<boolean> {
    return new Promise((resolve) => {
      const subscription = this.showModal({
        title,
        message,
        type: 'confirm',
        showCancel: true,
        confirmText,
        cancelText
      }).subscribe((result) => {
        subscription.unsubscribe();
        resolve(result.confirmed);
      });
    });
  }

  prompt(message: string, title: string = 'Enter Code', inputLabel: string = 'Code', inputPlaceholder: string = 'Enter code', inputType: string = 'text', confirmText: string = 'Confirm', cancelText: string = 'Cancel'): Promise<string | null> {
    return new Promise((resolve) => {
      this.showModal({
        title,
        message,
        type: 'prompt',
        showCancel: true,
        confirmText,
        cancelText,
        inputLabel,
        inputPlaceholder,
        inputType,
        inputValue: ''
      }).subscribe((result) => {
        if (result.confirmed) {
          resolve(result.inputValue || null);
        } else {
          resolve(null);
        }
      });
    });
  }

  private getDefaultTitle(type: string): string {
    switch (type) {
      case 'error': return 'Error';
      case 'warning': return 'Warning';
      case 'success': return 'Success';
      case 'info': return 'Information';
      default: return 'Notification';
    }
  }
}

