import { Component, OnInit, Inject, OnDestroy } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
import { NoticeList } from '@ucap-webmessenger/api-message';
import { Subscription } from 'rxjs';
import { select, Store } from '@ngrx/store';
import { tap } from 'rxjs/operators';
import * as AppStore from '@app/store';

export interface NoticeDetailDialogData {
  notice: NoticeList;
}

export interface NoticeDetailDialogResult {}

@Component({
  selector: 'app-notice-detail.dialog',
  templateUrl: './notice-detail.dialog.component.html',
  styleUrls: ['./notice-detail.dialog.component.scss']
})
export class NoticeDetailDialogComponent implements OnInit, OnDestroy {
  loginResSubscription: Subscription;

  constructor(
    public dialogRef: MatDialogRef<
      NoticeDetailDialogData,
      NoticeDetailDialogResult
    >,
    @Inject(MAT_DIALOG_DATA) public data: NoticeDetailDialogData,
    private store: Store<any>
  ) {}

  ngOnInit() {
    this.loginResSubscription = this.store
      .pipe(
        select(AppStore.AccountSelector.AuthenticationSelector.loginRes),
        tap(loginRes => {
          if (!loginRes) {
            this.onClickConfirm();
          }
        })
      )
      .subscribe();
  }

  ngOnDestroy(): void {
    if (!!this.loginResSubscription) {
      this.loginResSubscription.unsubscribe();
    }
  }

  onClickConfirm(): void {
    this.dialogRef.close();
  }
}
