import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { Observable, of } from 'rxjs';
import { catchError, exhaustMap, map, tap, take } from 'rxjs/operators';
import { FormBuilder } from '@angular/forms';
import { EmailAuthService } from '../service/email-auth.service';
import { EmailAuth } from '@overflow/commons-typescript/model/email/EmailAuth';

@Component({
  selector: 'of-member-confirm-reset-password',
  templateUrl: './member-confirm-reset-password.component.html',
})
export class MemberConfirmResetPasswordComponent implements OnInit {
  emailAuth$: Observable<EmailAuth>;
  pending$: Observable<boolean>;
  error$: Observable<any>;

  @Input() token: string;
  @Output() signin = new EventEmitter();
  @Output() modifyPassword = new EventEmitter();

  constructor(
    private store: Store<any>,
    private formBuilder: FormBuilder,
    private emailAuthService: EmailAuthService,
  ) { }
  ngOnInit(): void {
    this.emailAuthService.readByPwAuthKey(this.token)
      .pipe(
        tap(() => {
          this.pending$ = of(true);
        }),
        map((emailAuth: EmailAuth) => {
          this.emailAuth$ = of(emailAuth);
        }),
        catchError(err => {
          this.error$ = of(err);
          return of();
        }),
        tap(() => {
          this.pending$ = of(false);
        }),
        take(1),
    ).subscribe();
  }

  onModifyPassword() {
    this.modifyPassword.emit();
  }
}
