import { Injectable } from '@angular/core';
import { NatsService } from 'app/core/nats/services/nats.service';

import { Error } from 'app/modules/proto/protobuf/rpc_pb';

import {
  ListVendorsRequest,
  ListVendorsResponse,
} from 'app/modules/proto/c2se/api/vendor_pb';
import { SUBJECT_LIST_VENDORS } from 'app/modules/proto/c2se/frontend/api/vendor_pb';

@Injectable({
  providedIn: 'root',
})
export class VendorService {
  /**
   * Constructor
   */
  constructor(private __natsService: NatsService) {}

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

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

  listVendors(): Promise<ListVendorsResponse.Result> {
    return new Promise<ListVendorsResponse.Result>((resolve, reject) => {
      let req = new ListVendorsRequest();

      this.__natsService
        .request<ListVendorsResponse.Result>(
          SUBJECT_LIST_VENDORS,
          req.serializeBinary(),
          ListVendorsResponse.deserializeBinary
        )
        .then((result) => {
          console.log('success', result, result.getVendorsList());
          return resolve(result);
        })
        .catch((e: Error) => {
          console.log('failed', e);
          reject(e);
        });
    });
  }
}
