import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {
  BehaviorSubject,
  filter,
  map,
  Observable,
  of,
  switchMap,
  take,
  tap,
  throwError,
} from 'rxjs';

import { Casinomoney } from '../models/casinomoney';
import { CasinomoneyPagination } from '../models/casinomoney-pagination';

@Injectable({
  providedIn: 'root',
})
export class CasinomoneyService {
  // Private
  private __pagination = new BehaviorSubject<CasinomoneyPagination | undefined>(
    undefined
  );
  private __casinomoney = new BehaviorSubject<Casinomoney | undefined>(
    undefined
  );
  private __casinomoneys = new BehaviorSubject<Casinomoney[] | undefined>(
    undefined
  );

  /**
   * Constructor
   */
  constructor(private _httpClient: HttpClient) {}

  // -----------------------------------------------------------------------------------------------------
  // @ Accessors
  // -----------------------------------------------------------------------------------------------------

  /**
   * Getter for pagination
   */
  get pagination$(): Observable<CasinomoneyPagination | undefined> {
    return this.__pagination.asObservable();
  }

  /**
   * Getter for casinomoney
   */
  get casinomoney$(): Observable<Casinomoney | undefined> {
    return this.__casinomoney.asObservable();
  }

  /**
   * Getter for casinomoneys
   */
  get casinomoneys$(): Observable<Casinomoney[] | undefined> {
    return this.__casinomoneys.asObservable();
  }

  // -----------------------------------------------------------------------------------------------------
  // @ Public methods
  // -----------------------------------------------------------------------------------------------------

  /**
   * Get casinomoneys
   *
   *
   * @param page
   * @param size
   * @param sort
   * @param order
   * @param search
   */
  getCasinomoneys(
    page: number = 0,
    size: number = 10,
    sort: string = 'name',
    order: 'asc' | 'desc' | '' = 'asc',
    search: string = ''
  ): Observable<{
    pagination: CasinomoneyPagination;
    casinomoneys: Casinomoney[];
  }> {
    return this._httpClient
      .get<{ pagination: CasinomoneyPagination; casinomoneys: Casinomoney[] }>(
        'api/apps/member/casinomoney/casinomoneys',
        {
          params: {
            page: '' + page,
            size: '' + size,
            sort,
            order,
            search,
          },
        }
      )
      .pipe(
        tap((response) => {
          this.__pagination.next(response.pagination);
          this.__casinomoneys.next(response.casinomoneys);
        })
      );
  }

  /**
   * Get product by id
   */
  getCasinomoneyById(id: string | null): Observable<Casinomoney> {
    return this.__casinomoneys.pipe(
      take(1),
      map((casinomoneys) => {
        // Find the product
        const casinomoney =
          casinomoneys?.find((item) => item.id === id) || undefined;

        // Update the product
        this.__casinomoney.next(casinomoney);

        // Return the product
        return casinomoney;
      }),
      switchMap((product) => {
        if (!product) {
          return throwError('Could not found product with id of ' + id + '!');
        }

        return of(product);
      })
    );
  }

  /**
   * Create product
   */
  createCasinomoney(): Observable<Casinomoney> {
    return this.casinomoneys$.pipe(
      take(1),
      switchMap((casinomoneys) =>
        this._httpClient
          .post<Casinomoney>('api/apps/member/casinomoney/product', {})
          .pipe(
            map((newCasinomoney) => {
              // Update the casinomoneys with the new product
              if (!!casinomoneys) {
                this.__casinomoneys.next([newCasinomoney, ...casinomoneys]);
              }

              // Return the new product
              return newCasinomoney;
            })
          )
      )
    );
  }
}
