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/protobuf/protobuf/rpc/error_pb';

import {
  CheckUsernameForDuplicationRequest,
  CheckUsernameForDuplicationResponse,
  CheckNicknameForDuplicationRequest,
  CheckNicknameForDuplicationResponse,
  CaptchaRequest,
  CaptchaResponse,
} from 'app/modules/protobuf/c2se/common/identity_pb';
import {
  SUBJECT_CHECK_USERNAME_FOR_DUPLICATION,
  SUBJECT_CHECK_NICKNAME_FOR_DUPLICATION,
  SUBJECT_CAPTCHA,
  SUBJECT_SIGNIN,
  SigninRequest,
  SigninResponse,
} from 'app/modules/protobuf/c2se/backend/identity_pb';

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

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

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

  checkUsernameForDuplication(username: string): Promise<boolean> {
    return new Promise<boolean>((resolve, reject) => {
      let req = new CheckUsernameForDuplicationRequest();
      req.setUsername(username);

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

  checkNicknameForDuplication(nickname: string): Promise<boolean> {
    return new Promise<boolean>((resolve, reject) => {
      let req = new CheckNicknameForDuplicationRequest();
      req.setNickname(nickname);

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

  captcha(): Promise<CaptchaResponse.Result> {
    return new Promise<CaptchaResponse.Result>((resolve, reject) => {
      let req = new CaptchaRequest();

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

  signin(
    securityCodeHash: string,
    securityCode: string,
    username: string,
    password: string
  ): Promise<SigninResponse.Result> {
    return new Promise<SigninResponse.Result>((resolve, reject) => {
      let req = new SigninRequest();
      req.setSecurityCodeHash(securityCodeHash);
      req.setSecurityCode(securityCode);
      req.setUsername(username);
      req.setPassword(password);

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