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

namespace Kufer\Web\KuferTools\Basis\View;


abstract class ViewSight extends ViewTemplate
{
  protected $sightApi;

  
  protected $sight;

  
  public function __construct($viewModel)
  {
    parent::__construct($viewModel);
    $this->sightApi = new \Kufer\Web\KuferTools\Utils\Sight();
  }

  protected function getSight()
  {
    if (\is_null($this->sight)) {
      $this->sight = $this->sightApi->getSightFromFilePath(
        $this->getTemplatePathRelative(),
        $this->getTemplatePathAbsolute(),
        $this->getTemplate(),
        \is_null($this->viewModel)
      );
    }

    return $this->sight;
  }

  
  public function show()
  {
    $result = '';
    $bodyDataModul = $this->getBodyDataModulArray();
    if (!empty($bodyDataModul)) {
      $result .= $this->getHeader();
      $result .= $this->getBody($bodyDataModul);
      $result .= $this->getFooter();
    } else {
      $result .= $this->getNoData();
    }

    return $result;
    
    
  }

  public function getHeader()
  {
    return $this->sightApi->getHeader($this->getTemplatePathRelative(), $this->getSight(), $this->getHeaderDataModulArray(), $this->getHeaderExportAdapter());
  }

  protected function getHeaderExportAdapter()
  {
    return $this->getDefaultExportAdapter();
  }

  protected function getHeaderDataModulArray()
  {
    return $this->getDefaultDataModulArray();
  }

  public function getBody(?array $bodyDataModulAry = null)
  {
    return $this->sightApi->getBody($this->getTemplatePathRelative(), $this->getSight(), $bodyDataModulAry ?? $this->getBodyDataModulArray(), $this->getBodyExportAdapter());
  }

  protected function getBodyExportAdapter()
  {
    return $this->getDefaultExportAdapter();
  }

  protected function getBodyDataModulArray()
  {
    return $this->getDefaultDataModulArray();
  }

  public function getFooter()
  {
    return $this->sightApi->getFooter($this->getTemplatePathRelative(), $this->getSight(), $this->getFooterDataModulArray(), $this->getFooterExportAdapter());
  }

  protected function getFooterExportAdapter()
  {
    return $this->getDefaultExportAdapter();
  }

  protected function getFooterDataModulArray()
  {
    return $this->getDefaultDataModulArray();
  }

  public function getNoData()
  {
    return $this->sightApi->getNoData($this->getTemplatePathRelative(), $this->getSight(), $this->getNoDataDataModulArray(), $this->getNoDataExportAdapter());
  }

  protected function getNoDataExportAdapter()
  {
    return $this->getDefaultExportAdapter();
  }

  protected function getNoDataDataModulArray()
  {
    return $this->getDefaultDataModulArray();
  }

  public function getNoColumn()
  {
    return $this->sightApi->getNoColumn($this->getTemplatePathRelative(), $this->getSight(), $this->getNoColumnDataModulArray(), $this->getNoColumnExportAdapter());
  }

  protected function getNoColumnExportAdapter()
  {
    return $this->getDefaultExportAdapter();
  }

  protected function getNoColumnDataModulArray()
  {
    return $this->getDefaultDataModulArray();
  }

  protected function getDefaultExportAdapter()
  {
    return new \ExpBasis();
  }

  protected function getDefaultDataModulArray()
  {
    return array(new \Datamodul());
  }

  protected function getTemplateFileContent($pfadAbsolutZuTemplateOrdner)
  {
    list($fnContent, $id) = parent::getTemplateFileContent($pfadAbsolutZuTemplateOrdner);
    $content = static function() use ($fnContent) {
      $txt = call_user_func(($fnContent));
      $encoding = CP1252;
      $encodingPraeffix = mb_convert_encoding('ENCODING=', 'ASCII');
      if (\StringOperations::startsWith($txt, $encodingPraeffix)) {
        $encodingValStart = \StringOperations::strlen($encodingPraeffix, 'ASCII');
        $encodingValEnd = \StringOperations::strpos($txt, '|', $encodingValStart, 'ASCII');
        $encoding = \StringOperations::substr($txt, $encodingValStart, $encodingValEnd - $encodingValStart, 'ASCII');
        $txt = \StringOperations::substr($txt, $encodingValEnd + 1, null, 'ASCII');
      }

      $encodeSights = new \EncodeCharset4KuferToolsSights($encoding);

      return $encodeSights->encode($txt);
    };

    return array($content, $id);
  }
}