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

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

#include "./w_label.h"

#include "./w_drawable.h"

#include <gwenhywfar/debug.h>



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

static int _draw(AQDG_OBJECT *object);
static int _calcXPos(AQDG_OBJECT *object, uint32_t opts, int tw);
static int _calcYPos(AQDG_OBJECT *object, uint32_t opts, int th);



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



AQDG_OBJECT *AQDG_LabelWidget_new(AQDG_OBJECT *parent, uint32_t options, AQDG_DRAW_CONTEXT *drawContext, const char *text)
{
  AQDG_OBJECT *object;

  object=AQDG_DrawableWidget_new(parent, options, drawContext);
  AQDG_Object_SetName(object, "LabelWidget");
  AQDG_Object_SetOptions(object, options);
  AQDG_Object_SetGetDefaultWidthFn(object, AQDG_DrawableWidget_GetTextDefaultWidth);
  AQDG_Object_SetGetDefaultHeightFn(object, AQDG_DrawableWidget_GetTextDefaultHeight);
  AQDG_DrawableWidget_SetDrawFn(object, _draw);
  if (text)
    AQDG_DrawableWidget_SetText(object, text);
  return object;
}



int _draw(AQDG_OBJECT *object)
{
  if (object) {
    uint32_t opts;
    int x;
    int y;
    int tw;
    int th;

    AQDG_DrawableWidget_DrawBackground(object);
    opts=AQDG_Object_GetOptions(object);
    tw=AQDG_DrawableWidget_GetTextWidth(object);
    th=AQDG_DrawableWidget_GetTextHeight(object);

    x=_calcXPos(object, opts, tw);
    y=_calcYPos(object, opts, th);
    AQDG_DrawableWidget_DrawText(object, x, y, AQDG_DrawableWidget_GetText(object));
    return 1;
  }

  return 0;
}



int _calcXPos(AQDG_OBJECT *object, uint32_t opts, int tw)
{
  int x;
  int w;

  w=AQDG_Object_GetWidth(object)-AQDG_Object_GetBorderLeft(object)-AQDG_Object_GetBorderRight(object);
  if (w<0) {
    DBG_ERROR(AQDG_LOGDOMAIN, "Width smaller than borders (%d)", w);
    return AQDG_Object_GetBorderLeft(object);
  }

  if (opts & AQDG_DRAWABLE_OPTIONS_JUSTIFY_RIGHT) {
    /* right */
    x=(w-tw)+AQDG_Object_GetBorderLeft(object);
  }
  else if (opts & AQDG_DRAWABLE_OPTIONS_JUSTIFY_HCENTER) {
    /* hcenter */
    x=((w-tw)/2)+AQDG_Object_GetBorderLeft(object);
  }
  else {
    /* left */
    x=AQDG_Object_GetBorderLeft(object);
  }

  if (x<AQDG_Object_GetBorderLeft(object)) {
    DBG_INFO(AQDG_LOGDOMAIN, "x lower than left border pos");
    x=AQDG_Object_GetBorderLeft(object);
  }

  return x;
}



int _calcYPos(AQDG_OBJECT *object, uint32_t opts, int th)
{
  int y;
  int h;
  int oh=AQDG_Object_GetHeight(object);
  int borderBottom=AQDG_Object_GetBorderBottom(object);
  int borderTop=AQDG_Object_GetBorderTop(object);

  h=oh-(borderTop+borderBottom);
  if (h<0) {
    DBG_ERROR(AQDG_LOGDOMAIN, "Height smaller than borders (%d)", h);
    return borderTop;
  }

  if (opts & AQDG_DRAWABLE_OPTIONS_JUSTIFY_BOTTOM) {
    /* bottom */
    y=borderTop+(h-th);
  }
  else if (opts & AQDG_DRAWABLE_OPTIONS_JUSTIFY_VCENTER) {
    /* vcenter */
    y=borderTop+((h-th)/2);
  }
  else {
    /* top */
    y=borderTop;
  }

  if (y<borderTop) {
    DBG_INFO(AQDG_LOGDOMAIN, "y lower than top border pos");
    y=borderTop;
  }

  return y;
}






