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

namespace Kufer\KuferTools\Basis\View;


abstract class ViewSight extends ViewTemplate
{
  protected $sightApi;

  
  protected $sight;

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

  protected function getSight()
  {
    if (\is_null($this->sight)) {
      $this->sight = $this->sightApi->getSightFromFilePath($this->getTemplatePathAbsolute(), array($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->getSight(), $this->getHeaderDataModulArray(), $this->getHeaderExportAdapter());
  }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    $encodeSights = new \EncodeCharset4KuferToolsSights($encoding);

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