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

#pragma once
#include "../libs.h"
#include "../util/string.h"
#include "../file/asset.h"
#include "../file/csv.h"

/** Column name for the KEY within the CSV */
#define LANGUAGE_HEADER_KEY "Key"
/** Column name for the VALUE within the CSV */
#define LANGUAGE_HEADER_VALUE "Value"

/** Definition for a Language */
typedef struct {
  /** The buffer to read the asset from. */
  assetbuffer_t *asset;
  /** CSV Row for the header */
  csvrow_t header;
  /** The index in the header row that the key column is in. */
  int32_t headerIndexKey;
  /** The index in the header row that the value column is in. */
  int32_t headerIndexValue;
} language_t;

typedef struct {
  language_t *language;
  csvrow_t *row;
  char *key;
} languagecsvget_t;

/**
 * Initializes a language.
 * @param language Language to initialize.
 * @param filename The filename of the asset that the language uses.
 */
void languageInit(language_t *language, char *filename);

/**
 * Get the value for a given key out of the language buffer.
 * 
 * @param language Language to get from.
 * @param key Key to get.
 * @param output Output buffer to return.
 * @return Row within the CSV it was found, otherwise -1.
 */
int32_t languageGet(
  language_t *language, char *key, char output[CSV_CELL_SIZE_MAX]
);

/**
 * Returns the parsed handlebars out of the language CSV.
 * 
 * @param language Language to buffer from.
 * @param key Key to get out of the CSV.
 * @param variables Array of variables to parse the handlebars with.
 * @param variableCount Count of variables in the array.
 * @param buffer Buffer to store the output data in.
 * @return Forwarded result of stringHandlebarsBuffer.
 */
int32_t languageGetAndParse(language_t *language, char *key,
  stringhandlebarvariable_t *variables, int32_t variableCount, char *buffer
);

/**
 * Cleanup a previously initialized language.
 * @param language Language to dispose.
 */
void languageDispose(language_t *language);