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

namespace Kufer\Web\Classes\Datamodul;

class DozUrlaub extends \Datamodul
{
  public $dnr;
  public $zeit_beg;
  public $zeit_ende;
  
  public $datum;

  
  public $wotag;

  
  public $grund = '';

  
  public $ganzer_tag = true;

  
  public $vormittags_oder_nachmittags = false;

  public function getTyp($pFieldName)
  {
    switch ($pFieldName) {
      default: return parent::getTyp($pFieldName);
      case 'id':
      case 'dnr':
        return \DatamodulVarType::TYPE_INT;
      case 'datum':
        return \DatamodulVarType::TYPE_DATE;
      case 'zeit_beg':
      case 'zeit_ende':
        return \DatamodulVarType::TYPE_TIME;
      case 'vormittags_oder_nachmittags':
        return \DatamodulVarType::TYPE_BOOL_VN;
      case 'ganzer_tag':
        return \DatamodulVarType::TYPE_BOOL_HALF_DEFAULT_TRUE;
    }
  }

  public function getVarValue($name)
  {
    switch ($name) {
      default:
        return parent::getVarValue($name);
      case 'zeit_beg_ende':
        return $this->zeit_beg . ' - ' . $this->zeit_ende;
      case 'datum_komplettlang':
        $wotaglang = \DateTimeOperations::getWeekDay($this->datum);

        return $wotaglang . ', ' . $this->datum;
      case 'datum_komplettkurz':
        $wotagkurz = \DateTimeOperations::getWeekDayShort($this->datum);

        return $wotagkurz . ', ' . $this->datum;
      case 'wotagkurz':
        return \DateTimeOperations::getWeekDayShort($this->datum);
      case 'wotaglang':
        return \DateTimeOperations::getWeekDay($this->datum);
    }
  }

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

  public function getKnr(): string
  {
    return '__DO' . ($this->dnr ?? '');
  }

  public function toKurstag(?\Kurstag $default = null): \Kurstag
  {
    $result = new \Kurstag();
    $result->id = $this->id;
    $result->knr = $this->getKnr();
    $result->dnr = $this->dnr;
    $result->datum = $this->datum;
    $result->wotag = \DateTimeOperations::getWeekDayFromDate(\DateTimeOperations::getValidDateString($this->datum));
    $result->zeit_beg = $this->zeit_beg;
    $result->zeit_ende = $this->zeit_ende;
    $result->ort = $this->grund;

    if (!is_null($default)) {
      $result->gh_versendung = ($this->vormittags_oder_nachmittags ?? false) ? 'V' : ($default->gh_versendung !== 'V' ? $default->gh_versendung : 'N');
      $result->bemerkungen = ($this->ganzer_tag ?? true) ? '1' : '0.5';
      if ($default->bemerkungen === '1.0') {
        $result->bemerkungen = $default->bemerkungen;
      }
    } else {
      $result->gh_versendung = ($this->vormittags_oder_nachmittags ?? false) ? 'V' : 'N';
      $result->bemerkungen = ($this->ganzer_tag ?? true) ? '1' : '0.5';
    }

    return $result;
  }
}