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

namespace Kufer\KuferTools\Ausgaben\SammelSms;

use Kufer\KuferTools\Ausgaben\CommonTasks\SendPrepare as SendPrepare;

abstract class SammelSmsUseCase extends \Kufer\KuferTools\Basis\UseCase\EinzelKursUseCase
{
  protected $sendPreparer;
  protected $kontaktUser;
  protected static $toolId;
  protected static $toolSectionId;
  protected $rechteKey;

  public function __construct($request, $pToolId, $pToolSectionId, $pRechteKey)
  {
    
    
    static::$toolId = $pToolId;
    static::$toolSectionId = $pToolSectionId;
    parent::__construct($request, new SammelSmsResponse());
    $this->sendPreparer = new SendPrepare();
    $this->kontaktUser = array(); 
    $this->rechteKey = $pRechteKey;
  }

  
  abstract protected function getKontakte();

  public static function getToolId()
  {
    return static::$toolId;
  }

  public static function getToolSectionId()
  {
    return static::$toolSectionId;
  }

  protected function getRechteKey()
  {
    return $this->rechteKey; 
  }

  public function hasAccess($pToolSectionId = null)
  {
    
    
    if (!parent::hasAccess()) {
      return false;
    }
    
    if (!$this->isRechtAllowed($this->getRechteKey())) {
      return false;
    }

    return true;
  }

  public function action()
  {
    $user = $this->getUser();
    $errorMessage = '';
    try {
      $kurs = $this->getKurs($this->request->knr);
      
      $this->kontaktUser = $this->getKontakte();
      $fusszeile = $this->getSmsFusszeile($user, $kurs);
    } catch (\Exception $e) {
      $errorMessage = $e->getMessage();
      $fusszeile = '';
    }

    return $this->getResponse($kurs, $fusszeile, $errorMessage);
  }

  
  protected function getSmsFusszeile($pUser, $pKurs)
  {
    return '';
  }

  protected function getResponse($pKurs, $pFusszeile, $pErrorMessage = '')
  {
    $this->handleResponseStrategies();
    $this->response->kurs = $pKurs;
    $this->response->errorMessage = $pErrorMessage;
    
    $this->response->kontakte = $this->kontaktUser;
    $this->response->fusszeile = $pFusszeile;
    $this->response->toolId = $this->getToolId();

    return $this->response;
  }

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

class SammelSmsResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $kurs;
  public $kontakte;
  public $user;
  public $menuPunkte;
  public $fusszeile;
  public $toolId;
  public $errorMessage;
}