import { Injectable } from '@angular/core';
import { PromptModalData, UiModalService, UiPromptModalComponent } from '@ui/modal';
import { Observable } from 'rxjs';
import { ScanAdapter } from './scan-adapter';

@Injectable()
export class DevScanAdapter implements ScanAdapter {
  constructor(private _modal: UiModalService) {}

  getName(): string {
    return 'Dev Scanner';
  }

  isPrimary(): boolean {
    return true;
  }

  isReady(): boolean {
    return true;
  }

  scan(): Observable<string> {
    return new Observable((observer) => {
      const modalRef = this._modal.open({
        content: UiPromptModalComponent,
        title: 'Scannen',
        data: {
          message: 'Diese Eingabemaske dient nur zu Entwicklungs und Testzwecken.',
          placeholder: 'Scan Code',
          confirmText: 'weiter',
          cancelText: 'abbrechen',
        } as PromptModalData,
      });

      const sub = modalRef.afterClosed$.subscribe((result) => {
        observer.next(result.data);
        observer.complete();
      });

      return () => {
        modalRef.close();
        sub.unsubscribe();
      };
    });
  }
}
