<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, D-84453 Mühldorf
 */

namespace Kufer\Web\Classes\Sichten;

class SightRenderer
{
  
  protected $sicht;

  public function __construct(\Sight $sicht)
  {
    $this->sicht = $sicht;
  }

  public function renderHeader(\Exportadapter $exportAdapter, array $datamodulArray, array $pSightVars = array())
  {
    return $this->renderSightPart($this->sicht->header, $exportAdapter, $datamodulArray, $pSightVars);
  }

  public function renderBody(\Exportadapter $exportAdapter, array $datamodulArray, array $pSightVars = array())
  {
    return $this->renderSightPart($this->sicht->body, $exportAdapter, $datamodulArray, $pSightVars);
  }

  public function renderFooter(\Exportadapter $exportAdapter, array $datamodulArray, array $pSightVars = array())
  {
    return $this->renderSightPart($this->sicht->footer, $exportAdapter, $datamodulArray, $pSightVars);
  }

  public function renderNoData(\Exportadapter $exportAdapter, array $datamodulArray, array $pSightVars = array())
  {
    return $this->renderSightPart($this->sicht->nodata, $exportAdapter, $datamodulArray, $pSightVars);
  }

  public function renderNoColumn(\Exportadapter $exportAdapter, array $datamodulArray, array $pSightVars = array())
  {
    return $this->renderSightPart($this->sicht->nocolumn, $exportAdapter, $datamodulArray, $pSightVars);
  }

  
  protected function renderSightPart(string $sightCode, \Exportadapter $exportAdapter, array $datamodulArray, array $pSightVars = array())
  {
    
    $exportAdapter->export->initiate();
    foreach ($pSightVars as $sgtVarName => $sgtVarInhalt) {
      $exportAdapter->setSightInfo($sgtVarName, $sgtVarInhalt);
    }

    $exportAdapter->export->exportresults = $datamodulArray;
    $exportAdapter->filestream = $sightCode;
    $exportAdapter->replaceVarNames();
    if (!empty($exportAdapter->resultlist)) {
      return implode('', array_slice($exportAdapter->resultlist, 0, $exportAdapter->export->resultCount()));
    } else {
      return '';
    }
  }
}