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 {
  ListMemberClassesRequest,
  ListMemberClassesResponse,
  CreateMemberClassRequest,
  CreateMemberClassResponse,
  UpdateMemberClassRequest,
  UpdateMemberClassResponse,
  DeleteMemberClassRequest,
  DeleteMemberClassResponse,
  GetMemberClassResponse,
  GetMemberClassRequest,
} from 'app/modules/proto/c2se/member_class_pb';
import {
  SUBJECT_LIST_MEMBER_CLASSES,
  SUBJECT_CREATE_MEMBER_CLASS,
  SUBJECT_UPDATE_MEMBER_CLASS,
  SUBJECT_DELETE_MEMBER_CLASS,
  SUBJECT_GET_MEMBER_CLASS,
} from 'app/modules/proto/c2se/backend/member_class_pb';

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

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

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

  createMemberClass(): Promise<CreateMemberClassResponse.Result> {
    return new Promise<CreateMemberClassResponse.Result>((resolve, reject) => {
      let req = new CreateMemberClassRequest();

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

  listMemberClasss(): Promise<ListMemberClassesResponse.Result> {
    return new Promise<ListMemberClassesResponse.Result>((resolve, reject) => {
      let req = new ListMemberClassesRequest();

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

  getMemberClass(): Promise<GetMemberClassResponse.Result> {
    return new Promise<GetMemberClassResponse.Result>((resolve, reject) => {
      let req = new GetMemberClassRequest();

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

  updateMemberClass(): Promise<UpdateMemberClassResponse.Result> {
    return new Promise<UpdateMemberClassResponse.Result>((resolve, reject) => {
      let req = new UpdateMemberClassRequest();

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

  deleteMemberClass(): Promise<DeleteMemberClassResponse.Result> {
    return new Promise<DeleteMemberClassResponse.Result>((resolve, reject) => {
      let req = new DeleteMemberClassRequest();

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