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 {
  CreateMemberBankAccountRequest,
  CreateMemberBankAccountResponse,
  UpdateMemberBankAccountRequest,
  UpdateMemberBankAccountResponse,
  GetMemberBankAccountResponse,
  GetMemberBankAccountRequest,
  UpdateMemberBankAccountForExchangePasswordResponse,
} from 'app/modules/proto/c2se/member_bank_account_pb';
import {
  SUBJECT_CREATE_MEMBER_BANK_ACCOUNT,
  SUBJECT_UPDATE_MEMBER_BANK_ACCOUNT,
  SUBJECT_GET_MEMBER_BANK_ACCOUNT,
  SUBJECT_UPDATE_MEMBER_BANK_ACCOUNT_FOR_EXCHANGE_PASSWORD,
} from 'app/modules/proto/c2se/frontend/member_bank_account_pb';

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

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

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

  createMemberBankAccount(): Promise<CreateMemberBankAccountResponse.Result> {
    return new Promise<CreateMemberBankAccountResponse.Result>(
      (resolve, reject) => {
        let req = new CreateMemberBankAccountRequest();

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

  getMemberBankAccount(): Promise<GetMemberBankAccountResponse.Result> {
    return new Promise<GetMemberBankAccountResponse.Result>(
      (resolve, reject) => {
        let req = new GetMemberBankAccountRequest();

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

  updateMemberBankAccount(): Promise<UpdateMemberBankAccountResponse.Result> {
    return new Promise<UpdateMemberBankAccountResponse.Result>(
      (resolve, reject) => {
        let req = new UpdateMemberBankAccountRequest();

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

  updateMemberBankAccountForExchangePassword(): Promise<UpdateMemberBankAccountForExchangePasswordResponse.Result> {
    return new Promise<UpdateMemberBankAccountForExchangePasswordResponse.Result>(
      (resolve, reject) => {
        let req = new UpdateMemberBankAccountForExchangePasswordResponse();

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