import { take } from 'rxjs/operators';

import { Injectable, Inject, NgZone } from '@angular/core';

import { MatDialog } from '@angular/material/dialog';

import { Store, select } from '@ngrx/store';

import { NativeService } from '@ucap/native';

import { UCAP_NATIVE_SERVICE } from '@ucap/ng-native';
import { LoginSelector } from '@ucap/ng-store-authentication';

@Injectable({
  providedIn: 'root'
})
export class AppNativeService {
  constructor(
    @Inject(UCAP_NATIVE_SERVICE) private nativeService: NativeService,
    private store: Store<any>,
    private matDialog: MatDialog,
    private ngZone: NgZone
  ) {}

  subscribe(): void {
    this.nativeService.logout().subscribe(() => {
      this.ngZone.run(() => {
        this.matDialog.closeAll();

        this.store
          .pipe(take(1), select(LoginSelector.loginRes))
          .subscribe((loginRes) => {});
      });
    });

    this.nativeService.changeStatus().subscribe((statusCode) => {});

    this.nativeService.showSetting().subscribe(() => {
      this.ngZone.run(() => {
        // this.store.dispatch(SettingsStore.showDialog());
      });
    });
  }
}
