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

namespace Kufer\Web\KuferTools\Dozent\KursUebersicht\Detailsreiter\Honorar\Demo\VertragPdf;

include_once_ex('extern/composer/vendor/autoload.php');
use Spipu\Html2Pdf\Html2Pdf;

class Vertrag
{
  private $kurs;
  private $dozent;

  
  public function __construct($pKurs, $pDozent)
  {
    $this->kurs = $pKurs;
    $this->dozent = $pDozent;
  }

  public function output()
  {
    $html2pdf = $this->preparePdf();

    return $html2pdf->Output('vertrag_' . $this->kurs->id . '.pdf');
  }

  public function getFile()
  {
    $html2pdf = $this->preparePdf();

    return $html2pdf->Output('vertrag_' . $this->kurs->id . '.pdf', 's');
  }

  private function preparePdf()
  {
    static $html2pdf = null;
    if (is_null($html2pdf)) {
      $html2pdf = new Html2Pdf('P', 'A4', 'de');
      $html2pdf->setDefaultFont('Arial');
      $html2pdf->writeHTML(\StringOperations::utf8_encode($this->getHtmlTemplate()));
    }

    return $html2pdf;
  }

  private function getHtmlTemplate()
  {
    $html = file_get_contents(\FSOperations::path(dirname(__FILE__), 'Vertrag.html'));

    return $this->replaceVars($html);
  }

  private function replaceVars($html)
  {
    $pfadZuBilder = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'bilder';
    $logoPfad = $pfadZuBilder . $this->getBild('logo', 'png');
    $kursObj = $this->kurs;
    $dozentObj = $this->dozent;
    $dozKurs = $this->getDozKurs($kursObj->id, $dozentObj->id);

    $datum = date('d.m.Y');

    $map = array(
      '{%d_anrede%}' => $dozentObj->anrede,
      '{%d_titel%}' => $dozentObj->name_titel,
      '{%d_vorname%}' => $dozentObj->vorname,
      '{%d_name%}' => $dozentObj->name,
      '{%d_strasse_und_nr%}' => $dozentObj->strasse,
      '{%d_plz_ort%}' => $dozentObj->ort,

      '{%k_titel%}' => $kursObj->getVarValue('kurzbez_titel'),
      '{%k_nr%}' => $kursObj->nr_anzeige,
      '{%k_semester%}' => $kursObj->semester,
      '{%k_aussenstelle%}' => $kursObj->aussenst,
      '{%k_beginn_dat%}' => $kursObj->beginn_dat,
      '{%k_ende_dat%}' => $kursObj->ende_dat,
      '{%k_dauer%}' => $kursObj->dauer,
      '{%k_stundenanz%}' => $kursObj->stundenanz,
      '{%k_beginn%}' => $kursObj->beginn,
      '{%k_ende%}' => $kursObj->ende,
      '{%k_kursort%}' => $kursObj->ort,
      '{%k_honfaktor%}' => $dozKurs->getVarValue('honfaktorMitKomma'),
      '{%k_honorarsatz%}' => $dozKurs->honsatz,
      '{%k_honorar_gesamt%}' => $dozKurs->getVarValue('honGesamtMitKomma'),

      '{%fb_logo%}' => $logoPfad,
      '{%fb_datum_aktuell%}' => $datum
    );

    $needles = array_keys($map);
    $replacements = array_values($map);
    $replacements = array_map(function($string) {return \StringOperations::htmlentities($string);}, $replacements);

    return str_replace($needles, $replacements, $html);
  }

  private function getBild($pBezeichnung, $pFormat)
  {
    return DIRECTORY_SEPARATOR . $pBezeichnung . '.' . $pFormat;
  }

  
  private function getDozKurs($pKnr, $pDozId)
  {
    $dbworkDozKurs = new \DBWorkDozentenHonorar(1, true);

    return $dbworkDozKurs->getHonorarbyKNRDozentNR($pKnr, $pDozId);
  }
}