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

namespace Kufer\KuferTools\Dozent\KursUebersicht\Detailsreiter\Termine\SmsKursort;

class SendSmsKursortUseCase extends \Kufer\KuferTools\Dozent\KursUebersicht\EinzelKursUseCase
{
  public $dbwKurseTage;

  public const SMSVERSAND_SUCCESS = 0;
  public const SMSVERSAND_FEHLER_BEIM_VERSENDEN = 1;

  public function __construct($request)
  {
    parent::__construct($request, new SendSmsKursortResponse());
    $this->dbwKurseTage = new \DBWorkKurseTageWT(1, false);
  }

  public static function getToolId()
  {
    return \ToolsAPI::TOOL_KURSUEBERSICHT_DOZ;
  }

  public static function getToolSectionId()
  {
    return 4;
  }

  public function hasAccess($pToolSectionId = null)
  {
    if (!parent::hasAccess()) {
      return false;
    } elseif (!$this->request->isXsrfOk && !$this->request->isRefresh) {
      return false;
    } elseif (!$this->isKursTagFromKurs()) {
      return false;
    } elseif (!$this->columnSmsGrafikExists()) {
      return false;
    } elseif (!$this->isKurstagAllowed()) {
      return false;
    }

    return true;
  }

  public function action()
  {
    $dbwKurseTage = new \DBWorkKurseTageWT(1, true);
    $kursTag = $dbwKurseTage->getKurstag($this->request->kursTagId);

    if (!$this->request->isRefresh) {
      $mobilNummer = $this->getFilteredMobilNummer($kursTag->kursort->mobiltel);
      $this->sendSms($mobilNummer);
      $this->response->smsVersandt = true;
    } else {
      $this->response->smsVersandt = false;
    }

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

  private function sendSms($pMobilNummer)
  {
    $success = \SmsUserAPI::sendSmsAnHausmeister($pMobilNummer, $this->request->smsBody);
    if ($success) {
      $this->response->statusCode = self::SMSVERSAND_SUCCESS;
    } else {
      $this->response->statusCode = self::SMSVERSAND_FEHLER_BEIM_VERSENDEN;
    }
  }

  
  private function getFilteredMobilNummer($pMobilNummer)
  {
    $pMobilNummer = trim($pMobilNummer);
    $pMobilNummer = filter_var($pMobilNummer, FILTER_SANITIZE_NUMBER_INT);

    return str_replace('-', '', str_replace('+', '', $pMobilNummer));
  }

  protected function getResponse($pKursTag)
  {
    $this->handleResponseStrategies();
    $this->response->kursTag = $pKursTag;

    return $this->response;
  }

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

  private function columnSmsGrafikExists()
  {
    $columns = self::getColumns();
    foreach ($columns as $column) {
      if ($column->fieldname === 'smsgrafik') {
        return true;
      }
    }

    return false;
  }

  private function isKursTagFromKurs()
  {
    $kursTag = $this->dbwKurseTage->getKurstag($this->request->kursTagId);
    if ($kursTag->kurs->id == $this->request->knr) {
      return true;
    }

    return false;
  }

  private function isKurstagAllowed()
  {
    $kursTag = $this->dbwKurseTage->getKurstag($this->request->kursTagId);
    if ($kursTag->nodoztool) {
      return false;
    }

    return true;
  }
}

class SendSmsKursortRequest
{
  public $knr;
  public $smsBody;

  public $isRefresh;
  public $isXsrfOk;
  public $kursTagId;
}

class SendSmsKursortResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $kursTag;
  public $statusCode;
  public $smsVersandt;
  public $user;
  public $menuBar;
}