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

namespace Kufer\Web\Classes\Export\ExportCalculation;

class ExportCalcAdresse extends ExportCalc
{
  
  private $exportCalcBlaettern;

  public function __construct(ExportCalcBlaettern $exportCalcBlaettern)
  {
    $this->exportCalcBlaettern = $exportCalcBlaettern;
  }

  public function resolveVar(string $varname, ?\Datamodul $datamodul = null): string
  {
    $istVorbelegt = \StringOperations::strpos($varname, TRENNER_VORBELEGUNG) > 0;
    if ($istVorbelegt) {
      $parts = explode(TRENNER_VORBELEGUNG, $varname);
      $varname = array_shift($parts);
    }

    $param = '';
    $isParametrisiert = \StringOperations::strpos($varname, ':');
    if ($isParametrisiert !== false) {
      $param = \StringOperations::substr($varname, $isParametrisiert + 1);
      $varname = \StringOperations::substr($varname, 0, $isParametrisiert);
    }
    if (!is_a($datamodul, '\\Adresse')) {
      return '';
    }
    if (\StringOperations::startsWith($varname, 'sortierung')) {
      $link = \Kufer\Web\Classes\Addfunctions\UrlOperations::getSortierungBlaetternLink(
        \StringOperations::substr($varname, 11)
      );

      return \CleanOperations::cleanStringForHtml($link);
    }
    if (\StringOperations::startsWith($varname, 'blaettern') || \StringOperations::startsWith($varname, 'seitenauswahl')) {
      return $this->exportCalcBlaettern->resolveVar($varname);
    }
    switch (\StringOperations::strtolower($varname)) {
      default:
        return $this->getDefaultVarValue($varname, $datamodul);
      case 'nr':
        return \CleanOperations::cleanStringForHtml($datamodul->getVarValue('id'));
      case 'detaillink':
        return \CleanOperations::cleanStringForHtml(\Kufer\Web\Classes\Addfunctions\UrlOperations::getDetailLink(272, $datamodul->getVarValue('id')));
      case 'kommunikationsliste':
        return \KommunikationOperations::getKommunikationsliste($datamodul, \Kommunikation::TYP_ADRESSE, $param);
      case 'alle_im_bundesland_alternativ':
        $sightId = '741';
        
      case 'alle_im_bundesland':
        if (!isset($sightId)) {
          $sightId = '739';
        }
        if ($datamodul->bundesland != '') {
          $dbwa = new \DBWorkAdresse();
          $bodyinhalt = $dbwa->getAdressenForBundesland(
            $datamodul->bundesland,
            \RequestOperations::getVarValue('knr'),
            false,
            \RequestOperations::getVarValue('orderby', 'name'),
            \RequestOperations::getVarValue('order', 'asc')
          );

          return \CreateSightFassade::createListe($datamodul, $sightId, $bodyinhalt, new \ExpAdresse());
        }

        return '';
    }
  }
}