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 {
  ListMemberLevelsRequest,
  ListMemberLevelsResponse,
  CreateMemberLevelRequest,
  CreateMemberLevelResponse,
  UpdateMemberLevelRequest,
  UpdateMemberLevelResponse,
  DeleteMemberLevelRequest,
  DeleteMemberLevelResponse,
  GetMemberLevelResponse,
  GetMemberLevelRequest,
} from 'app/modules/proto/c2se/member_level_pb';
import {
  SUBJECT_LIST_MEMBER_LEVELS,
  SUBJECT_CREATE_MEMBER_LEVEL,
  SUBJECT_UPDATE_MEMBER_LEVEL,
  SUBJECT_DELETE_MEMBER_LEVEL,
  SUBJECT_GET_MEMBER_LEVEL,
} from 'app/modules/proto/c2se/backend/member_level_pb';

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

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

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

  createMemberLevel(): Promise<CreateMemberLevelResponse.Result> {
    return new Promise<CreateMemberLevelResponse.Result>((resolve, reject) => {
      let req = new CreateMemberLevelRequest();

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

  listMemberLevels(): Promise<ListMemberLevelsResponse.Result> {
    return new Promise<ListMemberLevelsResponse.Result>((resolve, reject) => {
      let req = new ListMemberLevelsRequest();

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

  getMemberLevel(): Promise<GetMemberLevelResponse.Result> {
    return new Promise<GetMemberLevelResponse.Result>((resolve, reject) => {
      let req = new GetMemberLevelRequest();

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

  updateMemberLevel(): Promise<UpdateMemberLevelResponse.Result> {
    return new Promise<UpdateMemberLevelResponse.Result>((resolve, reject) => {
      let req = new UpdateMemberLevelRequest();

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

  deleteMemberLevel(): Promise<DeleteMemberLevelResponse.Result> {
    return new Promise<DeleteMemberLevelResponse.Result>((resolve, reject) => {
      let req = new DeleteMemberLevelRequest();

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