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

namespace Kufer\Web\Classes\Pdf\Helper;

class Border extends RenderObject
{
  
  private $child;

  
  public function __construct($fpdf, RenderObject $child, $border = true)
  {
    parent::__construct($fpdf);
    $this->child = $child;
    $this->setBorder($border);
  }

  public function getChild(): RenderObject
  {
    return $this->child;
  }

  public function render(): void
  {
    $this->context(function() {
      $startX = $this->fpdf->GetX();
      $startY = $this->fpdf->GetY();
      $width = $this->child->getComputedWidth();
      $height = $this->child->getComputedHeight();
      assert(!is_null($width));
      assert(!is_null($height));

      $this->child->render();

      
      $lines = array();
      $border = $this->getBorder();

      if ($border & RenderObject::BORDER_TOP !== 0) {
        $lines[] = new Line($this->fpdf, $startX, $startY, $startX + $width, $startY);
      }

      if ($border & RenderObject::BORDER_LEFT !== 0) {
        $lines[] = new Line($this->fpdf, $startX, $startY, $startX, $startY + $height);
      }

      if ($border & RenderObject::BORDER_BOTTOM !== 0) {
        $lines[] = new Line($this->fpdf, $startX, $startY + $height, $startX + $width, $startY + $height);
      }

      if ($border & RenderObject::BORDER_RIGHT !== 0) {
        $lines[] = new Line($this->fpdf, $startX + $width, $startY, $startX + $width, $startY + $height);
      }

      foreach ($lines as $line) {
        $line->setParent($this);
        $line->render();
      }
    });
  }
}