import { Injectable } from '@angular/core';
import { NativeContainerService } from 'native-container';
import { Observable } from 'rxjs';
import { filter, map, take } from 'rxjs/operators';
import { ScanAdapter } from './scan-adapter';

@Injectable()
export class NativeScanAdapter implements ScanAdapter {
  constructor(private readonly nativeContainerService: NativeContainerService) {}

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

  isPrimary(): boolean {
    return true;
  }

  isReady(): boolean {
    // TODO: Fix Login Keycard Dauerschleife
    return this.nativeContainerService.isUiWebview().isNative || this.nativeContainerService.isIpadMini6();
    // return false;
  }

  scan(): Observable<string> {
    return this.nativeContainerService.openScanner('scanBook').pipe(
      filter((result) => result.status === 'SUCCESS'),
      map((result) => result.data),
      take(1)
    );
  }
}
