import { Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { Observable } from 'rxjs';
import { filter, concatMap, tap } from 'rxjs/operators';
import { UserOpenidService } from 'src/app/core/service/openid/user.service';
import { SweetalertService } from 'src/app/core/service/sweetalert/sweetalert';

@Component({
  selector: 'app-create-user',
  templateUrl: './create-user.container.html',
  styleUrls: ['./create-user.container.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class CreateUserContainer implements OnInit {
  userList$ = new Observable()
  constructor(
    private userOpenidSV: UserOpenidService,
    private swSV: SweetalertService,
    private cdr: ChangeDetectorRef
  ) { }

  ngOnInit(): void {
    this.userList$ = this.userOpenidSV.getAll()
  }

  search(query){
    if(query) return this.userList$ = this.userOpenidSV.searchUser(`?${query}`)
    if(!query) return this.userList$ = this.userOpenidSV.getAll()
  }

  delete(uid){
    this.swSV.confirm().pipe(
      filter(x => x.isConfirmed),
      concatMap(x => {
        return this.userOpenidSV.deleteData(uid).pipe(
          tap(x => this.swSV.removeSuccess()),
          tap(x => this.userList$ = this.userOpenidSV.getAll()),
          tap(x => this.cdr.detectChanges())
        )
      })
    ).subscribe()
  }

}
