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

namespace Kufer\Web\Classes\Datamodul;

use Kufer\Web\Classes\Identifier\Identifier;

class Wunschtermin extends \Datamodul
{
  
  public $nr;
  
  public $typ;

  
  public $datvon;

  
  public $datbis;

  
  public $dattxt;

  
  public $wotag;

  
  public $zeit_beg;

  
  public $zeit_ende;

  
  public $zeittxt;

  
  public $bemerkung;

  
  private $cachePerson;

  public function setValue2Var($var, $val)
  {
    switch ($var) {
      default:
        parent::setValue2Var($var, $val);
        break;
      case 'wotag':
        $this->wotag = \DateTimeOperations::getValidWoTagWithAlleString($val);
        break;
      case 'nr':
        $this->cachePerson = null;
        $this->nr = $val;
        break;
    }
  }

  public function getVarValue($name)
  {
    switch ($name) {
      default:
        return parent::getVarValue($name);
      case 'wotag':
        return \DateTimeOperations::getValidWoTagWithAlleString($this->wotag ?? '');
      case 'datvon_komplettkurz':
        $wotagkurz = \DateTimeOperations::getWeekDayShort($this->datvon);

        return $wotagkurz . ', ' . $this->datvon;
      case 'datbis_komplettkurz':
        $wotagkurz = \DateTimeOperations::getWeekDayShort($this->datbis);

        return $wotagkurz . ', ' . $this->datbis;
    }
  }

  public function getDatamodulByName($datamodulname)
  {
    switch ($datamodulname) {
    default:
      return parent::getDatamodulByName($datamodulname);
    case 'person':
    case 'personn':
      $person = $this->getPerson();
      if (is_null($person)) {
        return new \Dozent();
      }

      return $person;
    }
  }

  public function getTyp($pFieldName)
  {
    switch ($pFieldName) {
      default:
        return parent::getTyp($pFieldName);
      case 'id':
      case 'nr':
        return \DatamodulVarType::TYPE_INT;
      case 'datvon':
      case 'datbis':
        return \DatamodulVarType::TYPE_DATE;
    }
  }

  public function getPerson(): ?\Personn
  {
    if (!is_null($this->cachePerson)) {
      return $this->cachePerson;
    }

    switch ($this->typ) {
      default:
        return null;
      case 'D':
        $dbwDozenten = \DBWorkDozenten::getDefaultInstance();

        $this->cachePerson = $dbwDozenten->getDozent(Identifier::fromId($this->nr));

        return $this->cachePerson;
    }
  }

  public function getDMZValue($fieldname)
  {
    if ($fieldname === 'nr') {
      return $this->nr;
    }

    return parent::getDMZValue($fieldname);
  }

  public function getDatamodulName(): string
  {
    return 'wunschtermin';
  }
}