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

namespace Kufer\KuferTools\Dozent\Anwesenheiten\KurstageUebersicht;

use Kufer\KuferTools\Dozent\Anwesenheiten\Utils\KurstageUtils as KurstageUtils;

class KurstageUebersichtUseCase extends \Kufer\KuferTools\Basis\UseCase\ToolUseCase
{
  public function __construct($request)
  {
    parent::__construct($request, new KurstageUebersichtResponse());
  }

  public static function getToolId()
  {
    return 18;
  }

  public static function getToolSectionId()
  {
    return 291;
  }

  public function action()
  {
    $this->handleResponseStrategies();
    $datum = $this->getDate();
    $this->response->kurstage = $this->getKurstage($datum);
    $this->response->datamodule = $this->response->kurstage;
    
    $this->response->datum = $datum;
    $this->response->datumvor = $this->getNextDate($datum);
    $this->response->datumzurueck = $this->getPreviousDate($datum);

    $this->response->buttons = $this->getButtons();
    $this->response->isDateOutOfRange = !$this->isDateInRange($this->request->datum);
    $this->response->dateOutOfRange = $this->request->datum;

    if ($this->request->einstieg) {
      \Kufer\KuferTools\Utils\Referrer::resetReferrerInSession($this->getToolId());
    } else {
      \Kufer\KuferTools\Utils\Referrer::aktualisiereReferrerInSession($this->getToolId(), $this->request->referer, $this->request->refererLabel);
    }

    \Kufer\KuferTools\Utils\Referrer::resetReferrerInSession($this->getToolId(), 'back_anwesenheitseingabe');

    $this->response->backButtonLabel = \Kufer\KuferTools\Utils\Referrer::getReferrerLabelFromSession($this->getToolId(), 'Zurück zu Kursdetail');
    $this->response->backLink = \Kufer\KuferTools\Utils\Referrer::getReferrerFromSession($this->getToolId(), '');
    $this->response->showBackLink = trim($this->response->backLink) != '';

    $this->response->uebersichtAlsTabelle = $this->toolSettings->kurstageUebersichtAlsTabelle;

    return $this->response;
  }

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

  
  private function getKurstage($datum)
  {
    return KurstageUtils::getKurstageFromUebersicht($this->toolSettings, $datum);
  }

  private function getDate()
  {
    $datum = $this->request->datum;
    if (!$this->isDateInRange($datum)) {
      if (!\DateTimeOperations::isDateBeforeNow($datum, -1)) {
        
        $datum = \DateTimeOperations::getDateNow();
      } else {
        
        $datum = $this->getEarliestDate();
      }
    }

    return $datum;
  }

  private function getNextDate($datum)
  {
    $nextDate = \DateTimeOperations::getNextDate($datum, 1);
    if (!$this->isDateInRange($nextDate)) {
      return '';
    }

    return $nextDate;
  }

  private function getPreviousDate($datum)
  {
    $previousDate = \DateTimeOperations::getDateDaysBefore($datum, 1);
    if (!$this->isDateInRange($previousDate)) {
      return '';
    }

    return $previousDate;
  }

  private function getMaxDaysBack()
  {
    $maxDaysBack = $this->toolSettings->aktKurstageBackScroll;

    if (KurstageUtils::isDozentLeiter($this->toolSettings)) {
      $maxDaysBack = max($this->toolSettings->aktKurstageBackScroll4Leiter, $maxDaysBack);
    }

    return $maxDaysBack;
  }

  private function isDateInRange($datum)
  {
    if (\DateTimeOperations::isDatebeforeNow($datum, $this->getMaxDaysBack()) 
       || !\DateTimeOperations::isDateBeforeNow($datum, -1)) { 
      return false;
    }

    return true;
  }

  
  private function getEarliestDate(): string
  {
    return \DateTimeOperations::getDateDaysBefore(
      \DateTimeOperations::getDateNow(),
      $this->getMaxDaysBack()
    );
  }

  private function getButtons()
  {
    $buttons = array();
    if ($this->isRechtAllowed('273.0.3')) {
      $buttons['anwesenheiten'] = 'Anwesenheiten';
    }
    if ($this->isRechtAllowed('273.0.4')) {
      $buttons['noten'] = 'Noten';
    }
    if ($this->isRechtAllowed('273.0.5')) {
      $buttons['feedback'] = 'Feedback';
    }

    return $buttons;
  }
}

class KurstageUebersichtRequest
{
  public $seite = 1;
  public $datum;

  public $referer;
  public $refererLabel;
}

class KurstageUebersichtResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $menuPunkte;
  public $kurstage = array();
  public $datamodule = array();
  public $user;
  public $columns;
  
  
  public $datum;
  
  public $datumvor = '';
  public $datumzurueck = '';
  public $buttons;
  public $isDateOutOfRange = false;
  public $dateOutOfRange = '';
  public $backLink;
  public $getShowBacklink = false;

  public $verlinkePanelTitle = false;
  public $showCheckbox = false;
  public $uebersichtAlsTabelle = false;
}