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 {
  ListMemberReferrersRequest,
  ListMemberReferrersResponse,
  CreateMemberReferrerRequest,
  CreateMemberReferrerResponse,
  UpdateMemberReferrerRequest,
  UpdateMemberReferrerResponse,
  DeleteMemberReferrerRequest,
  DeleteMemberReferrerResponse,
  GetMemberReferrerResponse,
  GetMemberReferrerRequest,
  GetMemberReferrerByCodeResponse,
  GetMemberReferrerByCodeRequest,
} from 'app/modules/proto/c2se/member_referrer_pb';
import {
  SUBJECT_LIST_MEMBER_REFERRERS,
  SUBJECT_CREATE_MEMBER_REFERRER,
  SUBJECT_UPDATE_MEMBER_REFERRER,
  SUBJECT_DELETE_MEMBER_REFERRER,
  SUBJECT_GET_MEMBER_REFERRER,
  SUBJECT_GET_MEMBER_REFERRER_BY_CODE,
} from 'app/modules/proto/c2se/backend/member_referrer_pb';

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

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

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

  createMemberReferrer(
    memberId: string
  ): Promise<CreateMemberReferrerResponse.Result> {
    return new Promise<CreateMemberReferrerResponse.Result>(
      (resolve, reject) => {
        let req = new CreateMemberReferrerRequest();
        req.setMemberId(memberId);

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

  listMemberReferrers(): Promise<ListMemberReferrersResponse.Result> {
    return new Promise<ListMemberReferrersResponse.Result>(
      (resolve, reject) => {
        let req = new ListMemberReferrersRequest();

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

  getMemberReferrer(id: string): Promise<GetMemberReferrerResponse.Result> {
    return new Promise<GetMemberReferrerResponse.Result>((resolve, reject) => {
      let req = new GetMemberReferrerRequest();

      req.setId(id);

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

  getMemberReferrerByCode(
    code: string
  ): Promise<GetMemberReferrerByCodeResponse.Result> {
    return new Promise<GetMemberReferrerByCodeResponse.Result>(
      (resolve, reject) => {
        let req = new GetMemberReferrerByCodeRequest();

        req.setCode(code);

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

  updateMemberReferrer(): Promise<UpdateMemberReferrerResponse.Result> {
    return new Promise<UpdateMemberReferrerResponse.Result>(
      (resolve, reject) => {
        let req = new UpdateMemberReferrerRequest();

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

  deleteMemberReferrer(
    id: string
  ): Promise<DeleteMemberReferrerResponse.Result> {
    return new Promise<DeleteMemberReferrerResponse.Result>(
      (resolve, reject) => {
        let req = new DeleteMemberReferrerRequest();

        req.setId(id);

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