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

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

import {
  ListMemberBankDepositsRequest,
  ListMemberBankDepositsResponse,
  CreateMemberBankDepositRequest,
  CreateMemberBankDepositResponse,
  UpdateMemberBankDepositRequest,
  UpdateMemberBankDepositResponse,
  DeleteMemberBankDepositRequest,
  DeleteMemberBankDepositResponse,
  GetMemberBankDepositResponse,
  GetMemberBankDepositRequest,
  UpdateMemberBankDepositForStateResponse,
  UpdateMemberBankDepositForStateRequest,
} from 'app/modules/proto/c2se/member_bank_deposit_pb';
import {
  SUBJECT_LIST_MEMBER_BANK_DEPOSITS,
  SUBJECT_CREATE_MEMBER_BANK_DEPOSIT,
  SUBJECT_UPDATE_MEMBER_BANK_DEPOSIT,
  SUBJECT_DELETE_MEMBER_BANK_DEPOSIT,
  SUBJECT_GET_MEMBER_BANK_DEPOSIT,
  SUBJECT_UPDATE_MEMBER_BANK_DEPOSIT_FOR_STATE,
} from 'app/modules/proto/c2se/backend/member_bank_deposit_pb';

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

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

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

  createMemberBankDeposit(): Promise<CreateMemberBankDepositResponse.Result> {
    return new Promise<CreateMemberBankDepositResponse.Result>(
      (resolve, reject) => {
        let req = new CreateMemberBankDepositRequest();

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

  listMemberBankDeposits(): Promise<ListMemberBankDepositsResponse.Result> {
    return new Promise<ListMemberBankDepositsResponse.Result>(
      (resolve, reject) => {
        let req = new ListMemberBankDepositsRequest();

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

  getMemberBankDeposit(): Promise<GetMemberBankDepositResponse.Result> {
    return new Promise<GetMemberBankDepositResponse.Result>(
      (resolve, reject) => {
        let req = new GetMemberBankDepositRequest();

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

  updateMemberBankDeposit(): Promise<UpdateMemberBankDepositResponse.Result> {
    return new Promise<UpdateMemberBankDepositResponse.Result>(
      (resolve, reject) => {
        let req = new UpdateMemberBankDepositRequest();

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

  updateMemberBankDepositForState(): Promise<UpdateMemberBankDepositForStateResponse.Result> {
    return new Promise<UpdateMemberBankDepositForStateResponse.Result>(
      (resolve, reject) => {
        let req = new UpdateMemberBankDepositForStateRequest();

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

  deleteMemberBankDeposit(): Promise<DeleteMemberBankDepositResponse.Result> {
    return new Promise<DeleteMemberBankDepositResponse.Result>(
      (resolve, reject) => {
        let req = new DeleteMemberBankDepositRequest();

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