/***************************************************************************
 * Copyright 2013-2023 Aerospike, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

#pragma once

#include <node.h>
#include <nan.h>

extern "C" {
#include <aerospike/aerospike.h>
}

#include "log.h"

#define TYPE_CHECK_REQ(val, type, msg)                                         \
	if (!val->type())                                                          \
	return Nan::ThrowTypeError(msg)
#define TYPE_CHECK_OPT(val, type, msg)                                         \
	if (!(val->IsNull() || val->IsUndefined() || val->type()))                 \
	return Nan::ThrowTypeError(msg)

#define UDF_MAX_MODULE_NAME 255
#define UDF_MAX_FUNCTION_NAME 255

/*******************************************************************************
 *  CLASS
 ******************************************************************************/

class AerospikeClient : public Nan::ObjectWrap {

	/***************************************************************************
	 *  PUBLIC
	 **************************************************************************/

  public:
	static void Init();
	static v8::Local<v8::Value> NewInstance(v8::Local<v8::Object> config);

	aerospike *as;
	LogInfo *log;
	uv_async_t asyncEventCb;
	bool closed = false;

	char *report_dir = NULL;
	Nan::Persistent<v8::Function> enable_callback;
	Nan::Persistent<v8::Function> snapshot_callback;
	Nan::Persistent<v8::Function> node_close_callback;
	Nan::Persistent<v8::Function> disable_callback;

	/***************************************************************************
	 *  PRIVATE
	 **************************************************************************/

  private:
	AerospikeClient();
	~AerospikeClient();

	static inline Nan::Persistent<v8::Function> &constructor()
	{
		static Nan::Persistent<v8::Function> my_constructor;
		return my_constructor;
	}

	static NAN_METHOD(New);

	/***********************************************************************
		 *  CLIENT OPERATIONS
		 **********************************************************************/

	static NAN_METHOD(AddSeedHost);
	static NAN_METHOD(ApplyAsync);
	static NAN_METHOD(BatchExists);
	static NAN_METHOD(BatchGet);
	static NAN_METHOD(BatchReadAsync);
	static NAN_METHOD(BatchWriteAsync);
	static NAN_METHOD(BatchApply);
	static NAN_METHOD(BatchRemove);
	static NAN_METHOD(BatchSelect);
	static NAN_METHOD(ContextFromBase64);
	static NAN_METHOD(ContextToBase64);
	static NAN_METHOD(ChangePassword);
	static NAN_METHOD(Close);
	static NAN_METHOD(Connect);
	static NAN_METHOD(DisableMetrics);
	static NAN_METHOD(EnableMetrics);
	static NAN_METHOD(ExpressionToBase64);
	static NAN_METHOD(ExistsAsync);
	static NAN_METHOD(GetAsync);
	static NAN_METHOD(GetNodes);
	static NAN_METHOD(GetStats);
	static NAN_METHOD(HasPendingAsyncCommands);
	static NAN_METHOD(InfoAny);
	static NAN_METHOD(InfoForeach);
	static NAN_METHOD(InfoHost);
	static NAN_METHOD(InfoNode);
	static NAN_METHOD(IsConnected);
	static NAN_METHOD(JobInfo);
	static NAN_METHOD(OperateAsync);
	static NAN_METHOD(PrivilegeGrant);
	static NAN_METHOD(PrivilegeRevoke);
	static NAN_METHOD(PutAsync);
	static NAN_METHOD(QueryApply);
	static NAN_METHOD(QueryAsync);
	static NAN_METHOD(QueryBackground);
	static NAN_METHOD(QueryForeach);
	static NAN_METHOD(QueryPages);
	static NAN_METHOD(QueryRole);
	static NAN_METHOD(QueryRoles);
	static NAN_METHOD(QueryUser);
	static NAN_METHOD(QueryUsers);
	static NAN_METHOD(Register);
	static NAN_METHOD(RemoveAsync);
	static NAN_METHOD(RemoveSeedHost);
	static NAN_METHOD(RoleCreate);
	static NAN_METHOD(RoleDrop);
	static NAN_METHOD(RoleGrant);
	static NAN_METHOD(RoleRevoke);
	static NAN_METHOD(RoleSetWhitelist);
	static NAN_METHOD(RoleSetQuotas);
	static NAN_METHOD(ScanBackground);
	static NAN_METHOD(ScanAsync);
	static NAN_METHOD(ScanPages);
	static NAN_METHOD(SelectAsync);
	static NAN_METHOD(SetLogLevel);
	static NAN_METHOD(SetPassword);
	static NAN_METHOD(SetupEventCb);
	static NAN_METHOD(SetXDRFilter);
	static NAN_METHOD(TransactionAbort);
	static NAN_METHOD(TransactionCommit);
	static NAN_METHOD(Truncate);
	static NAN_METHOD(UDFRemove);
	static NAN_METHOD(UDFScan);
	static NAN_METHOD(UserCreate);
	static NAN_METHOD(UserCreatePKI);
	static NAN_METHOD(UserDrop);
	static NAN_METHOD(IndexCreate);
	static NAN_METHOD(IndexRemove);
};
