/****************************************************************************
 * This file is part of the project AqDiagram.
 * AqDiagram (c) by 2023 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.
 ****************************************************************************/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "o_hlayout-t.h"
#include "o_hlayout.h"

#include <aqdiagram/aqdg_api.h>

#include <gwenhywfar/debug.h>



#ifdef AQDIAGRAM_ENABLE_TESTCODE



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

static int GWENHYWFAR_CB test1(GWEN_TEST_MODULE *mod);

static AQDG_OBJECT *_createBox10x10(uint32_t opts);
static int _getDefaultWidth10(AQDG_OBJECT *o);
static int _getDefaultHeight10(AQDG_OBJECT *o);



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

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

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

  GWEN_Test_Module_AddTest(newMod, "test1", test1, NULL);

  return 0;
}



int test1(GWEN_UNUSED GWEN_TEST_MODULE *mod)
{
  AQDG_OBJECT *hlayout;
  AQDG_OBJECT *box1;
  AQDG_OBJECT *box2;
  AQDG_OBJECT *box3;
  int x;
  int y;
  int w;
  int h;
  int errors=0;

  hlayout=AQDG_HLayoutObject_new(NULL, 0);
  AQDG_Object_SetWidth(hlayout, 100);
  AQDG_Object_SetHeight(hlayout, 50);
  AQDG_Object_SetBorderLeft(hlayout, 2);
  AQDG_Object_SetBorderRight(hlayout, 4);
  AQDG_Object_SetBorderTop(hlayout, 3);
  AQDG_Object_SetBorderBottom(hlayout, 5);
  AQDG_Object_SetHSpacing(hlayout, 2);
  AQDG_Object_SetVSpacing(hlayout, 3);

  box1=_createBox10x10(AQDG_OBJECT_OPTIONS_STRETCHY);
  AQDG_Object_Tree2_AddChild(hlayout, box1);

  box2=_createBox10x10(AQDG_OBJECT_OPTIONS_STRETCHX | AQDG_OBJECT_OPTIONS_VALIGNCENTER);
  AQDG_Object_Tree2_AddChild(hlayout, box2);

  box3=_createBox10x10(AQDG_OBJECT_OPTIONS_VALIGNBOTTOM);
  AQDG_Object_Tree2_AddChild(hlayout, box3);

  AQDG_Object_Layout(hlayout);

  x=AQDG_Object_GetRelativeX(box1);
  y=AQDG_Object_GetRelativeY(box1);
  w=AQDG_Object_GetWidth(box1);
  h=AQDG_Object_GetHeight(box1);
  DBG_INFO(AQDG_LOGDOMAIN, "x=%d, y=%d, w=%d, h=%d", x, y, w, h);
  if (x!=2) {
    DBG_ERROR(AQDG_LOGDOMAIN, "X of box 1 is not 2 (%d)", x);
    errors++;
  }
  if (w!=10) {
    DBG_ERROR(AQDG_LOGDOMAIN, "WIDTH of box 1 is not 10 (%d)", w);
    errors++;
  }
  if (y!=3) {
    DBG_ERROR(AQDG_LOGDOMAIN, "Y of box 1 is not 3 (%d)", y);
    errors++;
  }
  if (h!=42) {
    DBG_ERROR(AQDG_LOGDOMAIN, "HEIGHT of box 1 is not 42 (%d)", y);
    errors++;
  }

  x=AQDG_Object_GetRelativeX(box2);
  y=AQDG_Object_GetRelativeY(box2);
  w=AQDG_Object_GetWidth(box2);
  h=AQDG_Object_GetHeight(box2);
  DBG_INFO(AQDG_LOGDOMAIN, "x=%d, y=%d, w=%d, h=%d", x, y, w, h);

  if (x!=14) {
    DBG_ERROR(AQDG_LOGDOMAIN, "X of box 2 is not 14 (%d)", x);
    errors++;
  }
  if (w!=70) {
    DBG_ERROR(AQDG_LOGDOMAIN, "WIDTH of box 2 is not 70 (%d)", w);
    errors++;
  }
  if (y!=19) {
    DBG_ERROR(AQDG_LOGDOMAIN, "Y of box 2 is not 19 (%d)", y);
    errors++;
  }
  if (h!=10) {
    DBG_ERROR(AQDG_LOGDOMAIN, "HEIGHT of box 2 is not 10 (%d)", y);
    errors++;
  }

  x=AQDG_Object_GetRelativeX(box3);
  y=AQDG_Object_GetRelativeY(box3);
  w=AQDG_Object_GetWidth(box3);
  h=AQDG_Object_GetHeight(box3);
  DBG_INFO(AQDG_LOGDOMAIN, "x=%d, y=%d, w=%d, h=%d", x, y, w, h);
  if (x!=86) {
    DBG_ERROR(AQDG_LOGDOMAIN, "X of box 3 is not 86 (%d)", x);
    errors++;
  }
  if (w!=10) {
    DBG_ERROR(AQDG_LOGDOMAIN, "WIDTH of box 3 is not 10 (%d)", w);
    errors++;
  }
  if (y!=35) {
    DBG_ERROR(AQDG_LOGDOMAIN, "Y of box 3 is not 35 (%d)", y);
    errors++;
  }
  if (h!=10) {
    DBG_ERROR(AQDG_LOGDOMAIN, "HEIGHT of box 3 is not 10 (%d)", y);
    errors++;
  }


  return errors?GWEN_ERROR_GENERIC:0;
}



AQDG_OBJECT *_createBox10x10(uint32_t opts)
{
  AQDG_OBJECT *box;

  box=AQDG_Object_new();
  AQDG_Object_SetOptions(box, opts);
  AQDG_Object_SetGetDefaultWidthFn(box, _getDefaultWidth10);
  AQDG_Object_SetGetDefaultHeightFn(box, _getDefaultHeight10);
  return box;
}



int _getDefaultWidth10(AQDG_OBJECT *o)
{
  return 10;
}


int _getDefaultHeight10(AQDG_OBJECT *o)
{
  return 10;
}


#else

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


#endif
