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

use Kufer\Web\Classes\Export\ExportCalculation;

class ExportGebaeude extends ExportBasis
{
  
  public function initiate()
  {
    ExportBasis::initiate();
    $this->dboperations = new DBWorkGebaeude();
  }

  
  public function export()
  {
    $this->clearResult();
    for ($i = 0; $i < count($this->exportsets); $i++) {
      $tmp = $this->dboperations->getGebaeudebyNr($this->exportsets[$i]->id);
      if (isset($tmp->id)) {
        array_push($this->exportresults, $tmp);
      }
    }
  }

  public function getVarValue($varname)
  {
    $exportCalc = $this->getExportCalc($this->getVarCategory($varname));
    if (is_null($exportCalc)) {
      return parent::getVarValue($varname);
    }
    $exportResult = $this->getCurrentExportResult();
    $origVarname = $varname;
    list($varname, $exportResult) = $this->handleFotoSonderfall($varname, $exportResult);

    
    return $exportCalc->resolveVar($this->getVarName($varname), $exportResult, $origVarname);
  }

  
  private function handleFotoSonderfall(string $varname, $exportResult): array
  {
    if ($this->getVarCategory($varname) == 'foto_' && !is_null($exportResult->getFotoTyp())) {
      $foto = FotoAPI::getFotoByVarname($varname, $exportResult->getFotoTyp(), $exportResult->id);
      if ($foto != null) {
        return array($foto['fotovarname'], $foto['foto']);
      }
    }

    return array($varname, $exportResult);
  }

  protected function getExportCalc(string $prefix): ?ExportCalculation\ExportCalc
  {
    switch ($prefix) {
      case 'haus_':
        return new ExportCalculation\ExportCalcGebaeude($this->getExportCalcBlaettern());
      case 'foto_':
        return new ExportCalculation\ExportCalcFotos();
    }

    return null;
  }
}