import { Injectable } from '@angular/core';

import { tap } from 'rxjs/operators';

import { Actions, createEffect, ofType } from '@ngrx/effects';
import { Store } from '@ngrx/store';

import { NGXLogger } from 'ngx-logger';

import { DialogService } from '@ucap-webmessenger/ui';

import { showDialog } from './actions';
import {
  MessengerSettingsDialogComponent,
  MessengerSettingsDialogData,
  MessengerSettingsDialogResult
} from '@app/layouts/messenger/dialogs/settings/messenger-settings.dialog.component';
import { SessionStorageService } from '@ucap-webmessenger/web-storage';
import { LoginResponse } from '@ucap-webmessenger/protocol-authentication';
import { KEY_LOGIN_RES_INFO } from '@app/types/login-res-info.type';

@Injectable()
export class Effects {
  showDialog$ = createEffect(
    () =>
      this.actions$.pipe(
        ofType(showDialog),
        tap(async () => {
          const loginRes = this.sessionStorageService.get<LoginResponse>(
            KEY_LOGIN_RES_INFO
          );

          if (!!loginRes && loginRes.userSeq) {
            const result = await this.dialogService.open<
              MessengerSettingsDialogComponent,
              MessengerSettingsDialogData,
              MessengerSettingsDialogResult
            >(MessengerSettingsDialogComponent, {
              width: '800px',
              maxWidth: '800px',
              height: '800px',
              minHeight: '800px',
              disableClose: false,
              data: {}
            });
          }
        })
      ),
    { dispatch: false }
  );

  constructor(
    private actions$: Actions,
    private store: Store<any>,
    private dialogService: DialogService,
    private logger: NGXLogger,
    private sessionStorageService: SessionStorageService
  ) {}
}
