/****************************************************************************
 * This file is part of the project AqHome.
 * AqHome (c) by 2024 Martin Preuss, all rights reserved.
 *
 * The license for this file can be found in the file COPYING which you
 * should have received along with this file.
 ****************************************************************************/

/* This file is included by "path.c" */


#include <gwenhywfar/testframework.h>
#include "vars-t.h"


#ifdef AQHOME_ENABLE_TESTCODE


/* ------------------------------------------------------------------------------------------------
 * forward declarations
 * ------------------------------------------------------------------------------------------------
 */

static int GWENHYWFAR_CB test1(GWEN_TEST_MODULE *mod);
static int GWENHYWFAR_CB test2(GWEN_TEST_MODULE *mod);
static int GWENHYWFAR_CB test3(GWEN_TEST_MODULE *mod);

static int _checkSingleVariableWithSingleStringValue(AQH_VARS *vtRoot, const char *var, const char *value);
static int _checkSingleVariableWithTwoStringValues(AQH_VARS *vtRoot, const char *var, const char *value1, const char *value2);
static AQH_VARS *_checkSingleVariable(AQH_VARS *vtRoot, const char *var);



/* ------------------------------------------------------------------------------------------------
 * implementations
 * ------------------------------------------------------------------------------------------------
 */

int AQH_Vars_AddTests(GWEN_TEST_MODULE *mod)
{
  GWEN_TEST_MODULE *newMod;

  newMod=GWEN_Test_Module_AddModule(mod, "AQH_Vars", NULL);

  GWEN_Test_Module_AddTest(newMod, "test 1: setCharValue", test1, NULL);
  GWEN_Test_Module_AddTest(newMod, "test 2: setCharValue twice - overwrite vars", test2, NULL);
  GWEN_Test_Module_AddTest(newMod, "test 3: setCharValue twice - don't overwrite vars", test3, NULL);

  return 0;
}



int GWENHYWFAR_CB test1(GWEN_UNUSED GWEN_TEST_MODULE *mod)
{
  AQH_VARS *vtRoot;
  int rv;

  vtRoot=AQH_Vars_CreateGroup("Root");
  rv=AQH_Vars_SetCharValue(vtRoot, 0, "var1", "value1");
  if (rv<0) {
    DBG_ERROR(AQH_LOGDOMAIN, "here (%d)", rv);
    return rv;
  }

  rv=_checkSingleVariableWithSingleStringValue(vtRoot, "var1", "value1");
  if (rv<0) {
    DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
    AQH_Vars_free(vtRoot);
    return GWEN_ERROR_GENERIC;
  }
  AQH_Vars_free(vtRoot);

  return 0;
}


int GWENHYWFAR_CB test2(GWEN_UNUSED GWEN_TEST_MODULE *mod)
{
  AQH_VARS *vtRoot;
  int rv;

  vtRoot=AQH_Vars_CreateGroup("Root");
  rv=AQH_Vars_SetCharValue(vtRoot, 0, "var1", "value1");
  if (rv<0) {
    DBG_ERROR(AQH_LOGDOMAIN, "here (%d)", rv);
    return rv;
  }

  rv=_checkSingleVariableWithSingleStringValue(vtRoot, "var1", "value1");
  if (rv<0) {
    DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
    AQH_Vars_free(vtRoot);
    return GWEN_ERROR_GENERIC;
  }

  rv=AQH_Vars_SetCharValue(vtRoot, AQH_VARS_PATHFLAGS_OVERWRITE_VARS, "var1", "value2");
  if (rv<0) {
    DBG_ERROR(AQH_LOGDOMAIN, "here (%d)", rv);
    return rv;
  }

  rv=_checkSingleVariableWithSingleStringValue(vtRoot, "var1", "value2");
  if (rv<0) {
    DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
    AQH_Vars_free(vtRoot);
    return GWEN_ERROR_GENERIC;
  }
  AQH_Vars_free(vtRoot);

  return 0;
}



int GWENHYWFAR_CB test3(GWEN_UNUSED GWEN_TEST_MODULE *mod)
{
  AQH_VARS *vtRoot;
  int rv;

  vtRoot=AQH_Vars_CreateGroup("Root");
  rv=AQH_Vars_SetCharValue(vtRoot, 0, "var1", "firstValue");
  if (rv<0) {
    DBG_ERROR(AQH_LOGDOMAIN, "here (%d)", rv);
    return rv;
  }

  rv=_checkSingleVariableWithSingleStringValue(vtRoot, "var1", "firstValue");
  if (rv<0) {
    DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
    AQH_Vars_free(vtRoot);
    return GWEN_ERROR_GENERIC;
  }

  rv=AQH_Vars_SetCharValue(vtRoot, 0, "var1", "secondValue");
  if (rv<0) {
    DBG_ERROR(AQH_LOGDOMAIN, "here (%d)", rv);
    AQH_Vars_free(vtRoot);
    return rv;
  }

  rv=_checkSingleVariableWithTwoStringValues(vtRoot, "var1", "firstValue", "secondValue");
  if (rv<0) {
    DBG_ERROR(AQH_LOGDOMAIN, "here (%d)", rv);
    AQH_Vars_free(vtRoot);
    return GWEN_ERROR_GENERIC;
  }

  AQH_Vars_free(vtRoot);

  return 0;
}



int _checkSingleVariableWithSingleStringValue(AQH_VARS *vtRoot, const char *var, const char *value)
{
  AQH_VARS *vtVar;
  AQH_VARS *vtValue;

  vtVar=_checkSingleVariable(vtRoot, var);
  if (vtVar==NULL) {
    DBG_INFO(AQH_LOGDOMAIN, "here");
    return GWEN_ERROR_GENERIC;
  }

  vtValue=AQH_Vars_Tree2_GetFirstChild(vtVar);
  if (vtValue==NULL) {
    DBG_ERROR(AQH_LOGDOMAIN, "No value");
    return GWEN_ERROR_GENERIC;
  }

  if (AQH_Vars_Tree2_GetNext(vtValue)) {
    DBG_ERROR(AQH_LOGDOMAIN, "More than one value");
    return GWEN_ERROR_GENERIC;
  }

  if (vtValue->dataType!=AQH_Vars_DataType_ValueString) {
    DBG_ERROR(AQH_LOGDOMAIN, "First child of root node is not a string value");
    return GWEN_ERROR_GENERIC;
  }

  if (!(vtValue->data.dataString && -1!=strcasecmp(vtValue->data.dataString, value))) {
    DBG_ERROR(AQH_LOGDOMAIN, "First child of variable node is not the value we set");
    return GWEN_ERROR_GENERIC;
  }

  return 0;
}



int _checkSingleVariableWithTwoStringValues(AQH_VARS *vtRoot, const char *var, const char *value1, const char *value2)
{
  AQH_VARS *vtVar;
  AQH_VARS *vtValue;

  vtVar=_checkSingleVariable(vtRoot, var);
  if (vtVar==NULL) {
    DBG_INFO(AQH_LOGDOMAIN, "here");
    return GWEN_ERROR_GENERIC;
  }

  vtValue=AQH_Vars_Tree2_GetFirstChild(vtVar);
  if (vtValue==NULL) {
    DBG_ERROR(AQH_LOGDOMAIN, "No value");
    return GWEN_ERROR_GENERIC;
  }

  if (vtValue->dataType!=AQH_Vars_DataType_ValueString) {
    DBG_ERROR(AQH_LOGDOMAIN, "First child of root node is not a string value");
    return GWEN_ERROR_GENERIC;
  }

  if (!(vtValue->data.dataString && -1!=strcasecmp(vtValue->data.dataString, value1))) {
    DBG_ERROR(AQH_LOGDOMAIN, "First child of variable node is not the value we set");
    return GWEN_ERROR_GENERIC;
  }

  vtValue=AQH_Vars_Tree2_GetNext(vtValue);
  if (vtValue==NULL) {
    DBG_ERROR(AQH_LOGDOMAIN, "Only one value");
    return GWEN_ERROR_GENERIC;
  }

  if (vtValue->dataType!=AQH_Vars_DataType_ValueString) {
    DBG_ERROR(AQH_LOGDOMAIN, "Second child of var node is not a string value");
    return GWEN_ERROR_GENERIC;
  }

  if (!(vtValue->data.dataString && -1!=strcasecmp(vtValue->data.dataString, value2))) {
    DBG_ERROR(AQH_LOGDOMAIN, "First child of variable node is not the value we set");
    return GWEN_ERROR_GENERIC;
  }


  return 0;
}



AQH_VARS *_checkSingleVariable(AQH_VARS *vtRoot, const char *var)
{
  AQH_VARS *vtVar;

  vtVar=AQH_Vars_Tree2_GetFirstChild(vtRoot);
  if (vtVar==NULL) {
    DBG_ERROR(AQH_LOGDOMAIN, "No variable");
    return NULL;
  }

  if (AQH_Vars_Tree2_GetNext(vtVar)) {
    DBG_ERROR(AQH_LOGDOMAIN, "More than one child");
    return NULL;
  }

  if (vtVar->dataType!=AQH_Vars_DataType_Variable) {
    DBG_ERROR(AQH_LOGDOMAIN, "First child of root node is not a variable");
    return NULL;
  }

  if (!(vtVar->data.dataString && -1!=strcasecmp(vtVar->data.dataString, var))) {
    DBG_ERROR(AQH_LOGDOMAIN, "First child of root node is not the variable we set");
    return NULL;
  }

  return vtVar;
}




#else

int AQH_Vars_AddTests(GWEN_TEST_MODULE *mod)
{
  DBG_ERROR(GWEN_LOGDOMAIN, "AqHome was compiled without test code enabled.");
  return GWEN_ERROR_GENERIC;
}


#endif
