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

namespace Kufer\KuferTools\Dozent\KursUebersicht\Detailsreiter\FeedbackFormularPdf;

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

class FeedbackFormular
{
  private $kurs;
  private $dozent;

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

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

    return $html2pdf->Output('feedbackformular.pdf');
  }

  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($html2pdf)));
    }

    return $html2pdf;
  }

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

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

  
  private function textOnOneLine($pText, $pHtml2pdf)
  {
    $encodeCharset = new \EncodeCharsetFromTo(\CodingOperations::getInternalEncoding(), UTF_8);
    $breite = $pHtml2pdf->pdf->GetStringWidth($encodeCharset->encode($pText));
    $right = 126.09;
    if ($breite > $right) {
      return false;
    }

    return true;
  }

  private function replaceVars($html, $pHtml2pdf)
  {
    $pfadZumFile = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'bilder';
    $smileys = $this->getSmileys();
    $kursObj = $this->kurs;
    $dozentObj = $this->dozent;
    $checkboxen = $this->getOtherBild('checkbox');
    $logo = $this->getLogoBild();
    $kursTitelAndKnr = $kursObj->kurzbez . ' ' . $kursObj->nr_anzeige;
    $dozentNameAndVorname = $dozentObj->name . ' ' . $dozentObj->vorname;

    $kursTitelAndKnrPruefen = $this->textOnOneLine($kursTitelAndKnr, $pHtml2pdf);
    $kursortPruefen = $this->textOnOneLine($kursObj->ort, $pHtml2pdf);
    $dozentNameAndVornamePruefen = $this->textOnOneLine($dozentNameAndVorname, $pHtml2pdf);

    $kursort_topMarginHeightOverflow = 'margin-top: 300px; height: 20px; overflow:hidden; ';
    $doz_topMarginHeightOverflow = 'margin-top: 320px; height: 20px; overflow:hidden; ';
    $tabelle_data_topMargin = ' 345px; ';
    $beurteilung_topMargin = ' 670px; ';
    $weitere_anmerkungen_topMargin = ' 700px; ';
    $text_style2_topMargin = ' 1025px; ';

    if ($kursTitelAndKnrPruefen === true || $kursortPruefen === true || $dozentNameAndVornamePruefen == true) {
      if ($kursortPruefen === true) {
        $kursort_topMarginHeightOverflow = 'margin-top: 277px; ';
      }
      if ($dozentNameAndVornamePruefen == true) {
        $doz_topMarginHeightOverflow = 'margin-top: 298px; ';
      }
      $tabelle_data_topMargin = ' 325px; ';
      $beurteilung_topMargin = ' 650px; ';
      $weitere_anmerkungen_topMargin = ' 680px; ';
      $text_style2_topMargin = ' 1005px; ';
    }
    $map = array(
      '{%k_kursTitelAndKnr%}' => $kursTitelAndKnr,
      '{%k_titel%}' => $kursObj->getVarValue('kurzbez_titel'),
      '{%k_nr%}' => $kursObj->nr_anzeige,
      '{%k_kursort%}' => $kursObj->ort,
      '{%d_nameAndvorname%}' => $dozentNameAndVorname,
      '{%d_vorname%}' => $dozentObj->vorname,
      '{%d_name%}' => $dozentObj->name,
      '{%fb_smiley1%}' => $pfadZumFile . $smileys[1],
      '{%fb_smiley2%}' => $pfadZumFile . $smileys[2],
      '{%fb_smiley3%}' => $pfadZumFile . $smileys[3],
      '{%fb_checkbox%}' => $pfadZumFile . $checkboxen,
      '{%fb_logo%}' => $pfadZumFile . $logo,
      '{%k_kursort_topMarginHeightOverflow%}' => $kursort_topMarginHeightOverflow,
      '{%k_doz_topMarginHeightOverflow%}' => $doz_topMarginHeightOverflow,
      '{%k_tabelle_data_topMargin%}' => $tabelle_data_topMargin,
      '{%k_beurteilung_topMargin%}' => $beurteilung_topMargin,
      '{%k_weitere_anmerkungen_topMargin%}' => $weitere_anmerkungen_topMargin,
      '{%k_text_style2_topMargin%}' => $text_style2_topMargin
      );

    $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 getSmileys()
  {
    $result = array();
    for ($i = 1; $i <= 3; $i++) {
      $result[$i] = DIRECTORY_SEPARATOR . 'smiley' . $i . '.gif';
    }

    return $result;
  }

  private function getOtherBild($pBildbezeichnung)
  {
    return DIRECTORY_SEPARATOR . $pBildbezeichnung . '.gif';
  }

  private function getLogoBild(): string
  {
    return DIRECTORY_SEPARATOR . 'logo.png';
  }
}