#ifndef ARC_STD_STRING_H_
#define ARC_STD_STRING_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include "bool.h"

/**
 * @brief substring position within a string
*/
typedef struct ARC_String {
    char     *data;
    uint64_t  length;
} ARC_String;

/**
 * @brief creates ARC_String type
 *
 * @param string  ARC_String to create
 * @param data    cstring that will be stored in ARC_String
 * @param length  length of ARC_String
 */
void ARC_String_Create(ARC_String **string, char *data, uint64_t length);

/**
 * @brief creates ARC_String type with strinlen
 *
 * @param string  ARC_String to create
 * @param data    cstring that will be stored in ARC_String
 */
void ARC_String_CreateWithStrlen(ARC_String **string, char *data);

/**
 * @brief creates an empty ARC_String type, useful if you want to add to the string over time without having to keep adding to the size
 *
 * @param string  ARC_String to create
 * @param length  length of ARC_String
 */
void ARC_String_CreateEmpty(ARC_String **string, uint64_t length);

/**
 * @brief destroys ARC_String type
 * 
 * @param string string that will be destroyed
 */
void ARC_String_Destroy(ARC_String *string);

/**
 * @brief copy a ARC_String
 *
 * @param copy     copy of oldString, will be set to NULL on error
 * @param original original string that is being copied
*/
void ARC_String_Copy(ARC_String **copy, ARC_String *original);

/**
 * @brief copy a subtring from a givin ARC_String
 *
 * @param substring new coppied substring, will be null on error
 * @param original  string to copy substring from
 * @param start     starting index in relation on original
 * @param length    length of substring that is being created
*/
void ARC_String_CopySubstring(ARC_String **substring, ARC_String *original, uint64_t start, uint64_t length);

/**
 * @brief replaces a string with a section of itself
 *
 * @note this uses ARC_String_CopySubstring so errors logs will be thrown in that function not this one
 *
 * @param string the string to replace, will not change on error
 * @param start  the starting index of the substring
 * @param length the length of the substring
*/
void ARC_String_ReplaceWithSubstring(ARC_String **string, uint64_t start, uint64_t length);

/**
 * @brief copy a subtring from a givin ARC_String
 *
 * @param newString string that doesn't have substring in it, will be null on error
 * @param original  string to remove substring from
 * @param substring substring to remove
*/
void ARC_String_RemoveSubstring(ARC_String **newString, ARC_String *original, ARC_String *substring);

/**
 * @brief appends to an ARC_String with an ARC_String
 *
 * @note this uses ARC_String_AppendCString, so debug logs will be thrown in that function not this one
 *
 * @param string the string to add to, will not change on error
 * @param append the string that will be added to the back of string
*/
void ARC_String_Append(ARC_String **string, ARC_String *append);

/**
 * @brief appends to an ARC_String with an ARC_String
 *
 * @param string  the string to add to, will not change on error
 * @param cstring the cstring that will be added to the back of string
 * @param length  the length of the cstring that is being added
*/
void ARC_String_AppendCString(ARC_String **string, const char *cstring, uint64_t length);

/**
 * @brief appends to an ARC_String with an ARC_String
 *
 * @note this uses ARC_String_AppendCString, so debug logs will be thrown in that function not this one
 *
 * @param string the string to add to, will not change on error
 * @param cstring the cstring that will be added to the back of string
*/
void ARC_String_AppendCStringWithStrlen(ARC_String **string, const char *cstring);

/**
 * @brief checks if two strings are the same
 *
 * @param first  string to check against second
 * @param second string to check against first
 *
 * @return ARC_True if match, ARC_False if they don't match
 */
ARC_Bool ARC_String_Equals(ARC_String *first, ARC_String *second);

/**
 * @brief check if ARC_String and cstring match
 *
 * @param string  ARC_string to check
 * @param cstring cstring to check
 * @param length  length of cstring
 *
 * @return ARC_True if match, ARC_False if they don't match
*/
ARC_Bool ARC_String_EqualsCString(ARC_String *string, const char *cstring, uint64_t length);

/**
 * @brief check if ARC_String and cstring match
 *
 * @note will use strlen to get the length of the cstring
 *
 * @param string  ARC_string to check
 * @param cstring cstring to check
 *
 * @return ARC_True if match, ARC_False if they don't match
*/
ARC_Bool ARC_String_EqualsCStringWithStrlen(ARC_String *string, const char *cstring);

/**
 * @brief check if substring of first equals second string
 *
 * @param first  string to check against second
 * @param offset postion based on first to start comparing against second
 * @param second string to check against first
 *
 * @return ARC_True if match, ARC_False if they don't match
*/
ARC_Bool ARC_String_SubstringEquals(ARC_String *first, uint64_t offset, ARC_String *second);

/**
 * @brief check if ARC_String and cstring match
 *
 * @param string  ARC_string to check
 * @param offset  postion based on string to start comparing against cstring
 * @param cstring cstring to check
 * @param length  length of cstring
 *
 * @return ARC_True if match, ARC_False if they don't match
*/
ARC_Bool ARC_String_SubstringEqualsCString(ARC_String *string, uint64_t offset, const char *cstring, uint64_t length);

/**
 * @brief checks if string is alphabetic
 *
 * @param string string to check
 *
 * @return ARC_True if alphabetic, ARC_False if not alphabetic
 */
ARC_Bool ARC_String_IsAlpha(ARC_String *string);

/**
 * @brief checks if string is made out of only numbers
 *
 * @param string string to check
 *
 * @return ARC_True if it is numeric, ARC_False if it is not numeric
 */
ARC_Bool ARC_String_IsNumeric(ARC_String *string);

/**
 * @brief converst substring from string to uint64_t
 *
 * @param string string to convert to uint64_t
 *
 * @return uint64_t converted number
*/
uint64_t ARC_String_ToUint64_t(ARC_String *string);

/**
 * @brief converst substring from string to int64_t
 *
 * @param string string to convert to int64_t
 *
 * @return int64_t converted number
*/
int64_t ARC_String_ToInt64_t(ARC_String *string);

/**
 * @brief converst substring from string to double
 *
 * @param string string to convert to double
 *
 * @return double converted number
*/
double ARC_String_ToDouble(ARC_String *string);

/**
 * @brief takes a given string, and assigns index and length for position of first matching substring
 *
 * @param string the string that will be searched
 * @param substr substring to find within string
 *
 * @return ~(uint64_t)0 on error, anything else on success
 */
uint64_t ARC_String_Find(ARC_String *string, ARC_String *substring);

/**
 * @brief takes given cstring and gives position of first matching
 *
 * @param string  the string that will be searched
 * @param cstring the cstring to find within string
 * @param length  the length of cstring
 *
 * @return ~(uint64_t)0 on error, anything else on success
*/
uint64_t ARC_String_FindCString(ARC_String *string, const char *cstring, uint64_t length);

/**
 * @brief takes given cstring and gives position of first matching
 *
 * @note will use strlen to get the length of the cstring
 *
 * @param string  the string that will be searched
 * @param cstring the cstring to find within string
 *
 * @return ~(uint64_t)0 on error, anything else on success
*/
uint64_t ARC_String_FindCStringWithStrlen(ARC_String *string, const char *cstring);

/**
 * @brief takes a given string, and assigns index and length for position of last matching substring
 *
 * @param string the string that will be searched
 * @param substr substring to find within string
 *
 * @return ~(uint64_t)0 on error, anything else on success
 */
uint64_t ARC_String_FindBack(ARC_String *string, ARC_String *substring);

/**
 * @brief takes a given cstring and give position of last matching
 *
 * @param string  the string that will be searched
 * @param cstring the cstring to find within string
 * @param length  the length of cstring
 *
 * @return ~(uint64_t)0 on error, anything else on success
 */
uint64_t ARC_String_FindBackCString(ARC_String *string, const char *cstring, uint64_t length);

/**
 * @brief takes a given cstring and give position of last matching
 *
 * @note will use strlen to get the length of the cstring
 *
 * @param string  the string that will be searched
 * @param cstring the cstring to find within string
 *
 * @return ~(uint64_t)0 on error, anything else on success
 */
uint64_t ARC_String_FindBackCStringWithStrlen(ARC_String *string, const char *cstring);

/**
 * @brief strips the ends based on a given char
 *
 * @param stripped    where to store the string which has witespace stripped
 *                    will be null if there is an error
 * @param original    the string which whill have the matching char stripped from
 * @param charToStrip the char that will be stripped from the ends
*/
void ARC_String_StripEnds(ARC_String **stripped, ARC_String *original, char charToStrip);

/**
 * @brief strips whitespace from a ARC_String
 *
 * @param stripped where to store the string which has witespace stripped
 *                 will be null if there is an error
 * @param original the string which whill have whitespace stripped from
*/
void ARC_String_StripWhitespace(ARC_String **stripped, ARC_String *original);

/**
 * @brief strips the whitespace from the ends of a string
 *
 * @param stripped    where to store the string which has witespace stripped from the ends
 *                    will be null if there is an error
 * @param original    the string which whill have the whitespace stripped from its ends
*/
void ARC_String_StripEndsWhitespace(ARC_String **stripped, ARC_String *original);

/**
 * @brief merges two strings together
 *
 * @param combined new ARC_String of combined strings frist + second
 * @param first    first part of string to combine
 * @param second   second part of string to combine
*/
void ARC_String_Merge(ARC_String **combined, ARC_String *first, ARC_String *second);

/**
 * @brief copy a subtring from a givin ARC_String
 *
 * @param newString    new string without specified section, will be NULL on error
 * @param original     string to remove section from
 * @param removeIndex  starting index in relation on original of what is to be removed
 * @param removeLength length of section that is being removed
*/
void ARC_String_RemoveSection(ARC_String **newString, ARC_String *original, uint64_t removeIndex, uint64_t removeLength);

/**
 * @brief replaces characters in string matching the given pattern
 *
 * @note this uses ARC_String_CopyReplaceMatching, so debug logs will be thrown in that function not this one
 *
 * @param string      the string that will be modified, will discard changes and set arc_errno on fail
 * @param pattern     the pattern to replace in the string on match
 * @param replacement the string that will replace the matched pattern
*/
void ARC_String_ReplaceMatching(ARC_String **string, ARC_String *pattern, ARC_String *replacement);

/**
 * @brief replaces characters in a copy of a string matching the given pattern
 *
 * @note original will not be modified
 * @note newString will need to be destroyed if it is not set to NULL
 *
 * @param newString   an empty string that this function will fill with a copy with replacements, will be set to NULL and arc_errno set on fail
 * @param original    the original string that will be copied
 * @param pattern     the pattern to replace in the string on match
 * @param replacement the string that will replace the matched pattern
*/
void ARC_String_CopyReplaceMatching(ARC_String **newString, ARC_String *original, ARC_String *pattern, ARC_String *replacement);

/**
 * @brief replaces characters in string matching the given pattern
 *
 * @param string             the string that will be modified, will discard changes and set arc_errno on fail
 * @param patternCString     the cstring pattern to replace in the string on match
 * @param patternLength      the lenght of the cstring pattern
 * @param replacementCstring the cstring that will replace the matched pattern
 * @param replacementLength  the length of the cstring replacement
*/
void ARC_String_ReplaceMatchingCString(ARC_String **string, char *patternCString, uint64_t patternLength, char *replacementCString, uint64_t replacementLength);

/**
 * @brief replaces characters in string matching the given pattern
 *
 * @note this uses ARC_String_ReplaceMatchingCString, so debug logs will be thrown in that function not this one
 *
 * @param string             the string that will be modified, will discard changes and set arc_errno on fail
 * @param patternCString     the cstring pattern to replace in the string on match
 * @param replacementCstring the cstring that will replace the matched pattern
*/
void ARC_String_ReplaceMatchingCStringWithStrlen(ARC_String **string, char *patternCString, char *replacement);

#ifdef __cplusplus
}
#endif

#endif //ARC_STD_STRING_H_
