/************************************************************************************
 *
 * D++, A Lightweight C++ library for Discord
 *
 * Copyright 2021 Craig Edwards and D++ contributors
 * (https://github.com/brainboxdotcc/DPP/graphs/contributors)
 *
 * 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 <dpp/json_fwd.hpp>

namespace dpp {

/** @brief Returns a snowflake id from a json field value, if defined, else returns 0
 * @param j nlohmann::json instance to retrieve value from
 * @param keyname key name to check for a value
 * @return found value
 */
uint64_t SnowflakeNotNull(const nlohmann::json* j, const char *keyname);

/** @brief Sets a snowflake id from a json field value, if defined, else does nothing
 * @param j nlohmann::json instance to retrieve value from
 * @param keyname key name to check for a value
 * @param v Value to change
 */
void SetSnowflakeNotNull(const nlohmann::json* j, const char *keyname, uint64_t &v);

/** @brief Returns a string from a json field value, if defined, else returns an empty string.
 * @param j nlohmann::json instance to retrieve value from
 * @param keyname key name to check for a value
 * @return found value
 */
std::string StringNotNull(const nlohmann::json* j, const char *keyname);

/** @brief Sets a string from a json field value, if defined, else does nothing
 * @param j nlohmann::json instance to retrieve value from
 * @param keyname key name to check for a value
 * @param v Value to change
 */
void SetStringNotNull(const nlohmann::json* j, const char *keyname, std::string &v);

/** @brief Returns a 64 bit unsigned integer from a json field value, if defined, else returns 0.
 * DO NOT use this for snowflakes, as usually snowflakes are wrapped in a string!
 * @param j nlohmann::json instance to retrieve value from
 * @param keyname key name to check for a value
 * @return found value
 */
uint64_t Int64NotNull(const nlohmann::json* j, const char *keyname);

/** @brief Sets an unsigned 64 bit integer from a json field value, if defined, else does nothing
 * @param j nlohmann::json instance to retrieve value from
 * @param keyname key name to check for a value
 * @param v Value to change
 */
void SetInt64NotNull(const nlohmann::json* j, const char *keyname, uint64_t &v);

/** @brief Returns a 32 bit unsigned integer from a json field value, if defined, else returns 0
 * @param j nlohmann::json instance to retrieve value from
 * @param keyname key name to check for a value
 * @return found value
 */
uint32_t Int32NotNull(const nlohmann::json* j, const char *keyname);

/** @brief Sets an unsigned 32 bit integer from a json field value, if defined, else does nothing
 * @param j nlohmann::json instance to retrieve value from
 * @param keyname key name to check for a value
 * @param v Value to change
 */
void SetInt32NotNull(const nlohmann::json* j, const char *keyname, uint32_t &v);

/** @brief Returns a 16 bit unsigned integer from a json field value, if defined, else returns 0
 * @param j nlohmann::json instance to retrieve value from
 * @param keyname key name to check for a value
 * @return found value
 */
uint16_t Int16NotNull(const nlohmann::json* j, const char *keyname);

/** @brief Sets an unsigned 16 bit integer from a json field value, if defined, else does nothing
 * @param j nlohmann::json instance to retrieve value from
 * @param keyname key name to check for a value
 * @param v Value to change
 */
void SetInt16NotNull(const nlohmann::json* j, const char *keyname, uint16_t &v);

/** @brief Returns an 8 bit unsigned integer from a json field value, if defined, else returns 0
 * @param j nlohmann::json instance to retrieve value from
 * @param keyname key name to check for a value
 * @return found value
 */
uint8_t Int8NotNull(const nlohmann::json* j, const char *keyname);

/** @brief Sets an unsigned 8 bit integer from a json field value, if defined, else does nothing
 * @param j nlohmann::json instance to retrieve value from
 * @param keyname key name to check for a value
 * @param v Value to change
 */
void SetInt8NotNull(const nlohmann::json* j, const char *keyname, uint8_t &v);

/** @brief Returns a boolean value from a json field value, if defined, else returns false
 * @param j nlohmann::json instance to retrieve value from
 * @param keyname key name to check for a value
 * @return found value
 */
bool BoolNotNull(const nlohmann::json* j, const char *keyname);

/** @brief Sets a boolean from a json field value, if defined, else does nothing
 * @param j nlohmann::json instance to retrieve value from
 * @param keyname key name to check for a value
 * @param v Value to change
 */
void SetBoolNotNull(const nlohmann::json* j, const char *keyname, bool &v);

/** @brief Returns a time_t from an ISO8601 timestamp field in a json value, if defined, else returns
 * epoch value of 0.
 * @param j nlohmann::json instance to retrieve value from
 * @param keyname key name to check for a value
 * @return found value
 */
time_t TimestampNotNull(const nlohmann::json* j, const char *keyname);

/** @brief Sets an timestamp from a json field value containing an ISO8601 string, if defined, else does nothing
 * @param j nlohmann::json instance to retrieve value from
 * @param keyname key name to check for a value
 * @param v Value to change
 */
void SetTimestampNotNull(const nlohmann::json* j, const char *keyname, time_t &v);

/** @brief Base64 encode data into a string.
 * @param buf Raw binary buffer
 * @param buffer_length Buffer length to encode
 * @return The base64 encoded string
 */
std::string base64_encode(unsigned char const* buf, unsigned int buffer_length);

};
