<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

namespace Kufer\Utils\Pdf\Helper;

use Kufer\Web\Classes\Pdf\ExtFPDITcPDF;


class TextField extends RenderObject
{
  
  protected $text;

  
  protected $link;

  
  public function __construct($fpdf, $text)
  {
    parent::__construct($fpdf);
    $this->text = $text;
  }

  public function getText()
  {
    return $this->text;
  }

  public function setText($text): void
  {
    $this->text = $text;
  }

  
  public function setLink($link): void
  {
    $this->link = $link;
  }

  public function getLink(): ?string
  {
    return $this->link;
  }

  public function render(): void
  {
    $startX = null;
    if (!\is_null($this->getX())) {
      $startX = $this->getComputedX();
      $this->fpdf->SetX($startX);
    }
    $startY = null;
    if (!\is_null($this->getY())) {
      $startY = $this->getComputedY();
      $this->fpdf->SetY($startY);
    }

    $this->context(function() use (&$startX, &$startY) {
      if (static::DEBUG_ALL) {
        $this->drawOutline(200, 200, 50);
      }

      $startX = $startX ?? $this->fpdf->GetX();
      $startY = $startY ?? $this->fpdf->GetY();
      $renderX = $startX;
      $renderY = $startY;
      $width = $this->getComputedWidth() + $this->getTextMargin();
      $height = $this->getComputedHeight();
      $textWidth = $this->getAutoWidth() + $this->getTextMargin();
      $textHeight = $this->getAutoHeight() - $this->getTextMargin();

      $textOrientation = $this->getTextOrientation();
      if ($textWidth <= $width) {
        
        if ($textOrientation & static::TEXT_ORIENTATION_LEFT) {
        } elseif ($textOrientation & static::TEXT_ORIENTATION_RIGHT) {
          $renderX = $renderX + abs($width - $textWidth);
        } elseif ($textOrientation & static::TEXT_ORIENTATION_CENTER_HORIZONTAL) {
          $renderX = $renderX + abs($width - $textWidth) / 2;
        }
      }

      if ($textHeight <= $height) {
        
        if ($textOrientation & static::TEXT_ORIENTATION_TOP) {
        } elseif ($textOrientation & static::TEXT_ORIENTATION_BOTTOM) {
          $renderY = $renderY + abs($height - $textHeight);
        } elseif ($textOrientation & static::TEXT_ORIENTATION_CENTER_VERTICAL) {
          $renderY = $renderY + abs($height - $textHeight) / 2;
        }
      }

      $this->fpdf->SetXY($renderX, $renderY);
      $this->fpdf->CellExt(
        min($width, $textWidth) + $this->fpdf->GetStringWidth(' ') * 0.95,
        $this->getText(),
        (int)static::DEBUG_TEXT,
        min($height, $textHeight),
        $this->getFontStyle(), 
        $this->getFontSize(), 
        'L',
        !\ArrayOperations::any($this->getBackgroundColor(), '\\is_null'),
        $this->getLink() ?? ''
      );
    });

    $this->fpdf->SetX($startX + $this->getComputedWidth());
    $this->fpdf->SetY($startY, false);
  }

  public function getAutoWidth(): ?float
  {
    return $this->context(function() {
      return $this->fpdf->GetStringWidth($this->getText()) + $this->getTextMargin() * 2;
    });
  }

  public function getAutoHeight(): ?float
  {
    return $this->context(function() {
      return $this->fpdf->getFontSize() + $this->getTextMargin() * 2;
    });
  }
}