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

namespace Kufer\KuferTools\Basis\Kursdetails\DetailsReiter\EMails\Anzeigen;


abstract class EMailAnzeigenUseCase extends \Kufer\KuferTools\Basis\UseCase\ToolUseCase
{
  
  protected $datDoc;

  public function __construct($request, $response = null)
  {
    parent::__construct($request, $response ?? new EMailAnzeigenResponse());
  }

  
  abstract protected function getMainReiterId();

  public function hasAccess($pToolSectionId = null)
  {
    
    if (!\is_numeric($this->request->lfdnr) || $this->request->knr === '') {
      return false;
    }
    
    if (!\in_array($this->request->knr, $this->getAllowedKursnummern())) {
      return false;
    }
    
    if (\is_null($this->getDatDoc())) {
      return false;
    }
    
    if (!$this->isRechtAllowed($this->getReiterRecht())) {
      return false;
    }

    return parent::hasAccess($pToolSectionId);
  }

  protected function getKursHelper()
  {
    return \Kufer\KuferTools\Utils\DatamodulHelper\KursHelper\KursHelperFactory::getKursHelperByToolID($this->getToolId(), $this->toolSettings);
  }

  public function getAllowedKursnummern()
  {
    $kurshelper = $this->getKursHelper();

    return $kurshelper->getIdsOhneFilter();
  }

  
  protected function getReiterRecht(): string
  {
    return $this->getMainReiterId() . '.1.10';
  }

  public function action()
  {
    $this->handleResponseStrategies();
    $datdoc = $this->getDatDoc();

    return $this->getResponse($datdoc);
  }

  protected function getResponseStrategies()
  {
    return array(
        new \Kufer\KuferTools\Utils\ResponseStrategies\ResponseMitRahmen($this)
    );
  }

  
  public function getResponse($datdoc)
  {
    $this->response->datdoc = $datdoc;
    $this->response->knr = $this->request->knr;

    return $this->response;
  }

  
  protected function getSearchDatDoc()
  {
    $datDoc = new \Dat_doc();
    $datDoc->lfdnr = $this->request->lfdnr;
    $datDoc->typen = \implode(',', array('E_T', 'E_D'));

    return $datDoc;
  }

  
  protected function getDatDoc()
  {
    if (\is_null($this->datDoc)) {
      try {
        $searchDatDoc = $this->getSearchDatDoc();
        $datDoc = \DatDocAPI::getEmailFromDmz($searchDatDoc);
        $realDatDocs = \DatDocAPI::getDokumentenListeFromDmz($searchDatDoc);
        if (\count($realDatDocs) !== 1) {
          return null;
        }
        $realDatDoc = $realDatDocs[0];
        $datDoc->nr = $realDatDoc->nr;
        $datDoc->typ = $realDatDoc->typ;
        $datDoc->knr = $realDatDoc->knr;
        if ($datDoc->knr !== $this->request->knr) {
          return null;
        }
        $this->datDoc = $datDoc;
      } catch (\Exception $ex) {
        \Logger::loggException($ex, 20);

        return null;
      }
    }

    return $this->datDoc;
  }
}

class EMailAnzeigenRequest
{
  
  public $lfdnr;

  
  public $knr;
}

class EMailAnzeigenResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  
  public $datdoc;
}