/**
 * Copyright (c) 2021 Dominic Masters
 * 
 * This software is released under the MIT License.
 * https://opensource.org/licenses/MIT
 */

#pragma once
#include "dawnsharedlibs.hpp"
#include "file.hpp"

#define XML_DOING_NOTHING 0x00
#define XML_PARSING_TAG_NAME 0x01
#define XML_LOOKING_FOR_ATTRIBUTE 0x02
#define XML_PARSING_ATTRIBUTE_NAME 0x03
#define XML_LOOKING_FOR_ATTRIBUTE_VALUE 0x04
#define XML_PARSING_ATTRIBUTE_VALUE 0x05
#define XML_PARSING_VALUE 0x06
#define XML_PARSING_CHILD 0x07
#define XML_PARSING_CLOSE 0x08
#define XML_PARSING_COMMENT 0x09

#define XML_TEXT_BUFFER_MAX 2048
#define XML_CHILD_COUNT_MAX 128
#define XML_ATTRIBUTE_MAX 128

typedef struct _xml_t xml_t;

typedef struct _xml_t {
  char *node;
  char *value;

  char *attributeNames[XML_ATTRIBUTE_MAX];
  char *attributeDatas[XML_ATTRIBUTE_MAX];
  uint8_t attributeCount;

  xml_t *children;
  uint8_t childrenCount;
} xml_t;

/**
 * Load an XML child from a string buffer.
 * 
 * @param xml XML to load.
 * @param data Data to parse
 * @param i Character index within the data
 * @return The index in the data string this XML node ends.
 */
int32_t xmlLoadChild(xml_t *xml, char *data, int32_t i);

/**
 * Load an XML String into an XML memory.
 * 
 * @param xml XML to load into.
 * @param data XML string.
 */
void xmlLoad(xml_t *xml, char *data);

/**
 * Dispose a previously loaded XML.
 * 
 * @param xml XML to dispose.
 */
void xmlDispose(xml_t *xml);

/**
 * Find an attribute index by its name.
 * 
 * @param xml Xml node to get the attribute from.
 * @param name The name of the attribute you're trying to find.
 * @return -1 if not found, otherwise the index of the attribute within array.
 */
int16_t xmlGetAttributeByName(xml_t *xml, char *name);

/**
 * Checks if a given character is a whitespace character or not.
 * 
 * @param c Character to check if a whitespace.
 * @return True if whitespace, otherwise false.
 */
bool xmlIsWhitespace(char c);