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

namespace Kufer\Web\Classes\Sichten;

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

  protected function renderSightPart(string $sightCode, \Exportadapter $exportAdapter, array $datamodulArray, array $pSightVars = array())
  {
    return parent::renderSightPart($this->entferneBetreffTag($sightCode), $exportAdapter, $datamodulArray, $pSightVars);
  }

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

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

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

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

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

  private function getEmailSubject(string $sightCode, \Exportadapter $exportAdapter, array $datamodulArray, array $pSightVars = array())
  {
    
    $matches = array();
    if (preg_match($this->getBetreffPattern(), $sightCode, $matches)) {
      $subject = $this->renderSightPart($matches[1], $exportAdapter, $datamodulArray, $pSightVars);

      return \StringOperations::removeLinebreaks($subject);
    }

    return '';
  }

  private function getBetreffPattern()
  {
    return '/<betreff>(.*)<\\/betreff>/';
  }

  private function entferneBetreffTag(string $sichtpart)
  {
    return preg_replace($this->getBetreffPattern(), '', $sichtpart);
  }
}