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

namespace Kufer\KuferTools\Mitarbeiter\AdressInfo\UpdateAdress;

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

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

  public static function getToolSectionId()
  {
    return 58;
  }

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

    if (empty(\DBWorkAdresse::getDefaultInstance()->getAdresse($this->getRequestedIdentifier())->id)) {
      return false;
    }

    return true;
  }

  protected function getRequestedId()
  {
    return $this->request->anr;
  }

  protected function getRequestedGuid(): ?\Kufer\Web\Classes\Addfunctions\Guid
  {
    return !empty($this->request->aguid) ? new \Kufer\Web\Classes\Addfunctions\Guid($this->request->aguid) : null;
  }

  protected function getRequestedIdentifier(): ?\Kufer\Web\Classes\Identifier\Identifier
  {
    return new \Kufer\Web\Classes\Identifier\Identifier($this->getRequestedId(), $this->getRequestedGuid());
  }

  public function action()
  {
    $dbwAdresse = new \DBWorkAdresse();
    $adress = $dbwAdresse->getAdresse($this->getRequestedIdentifier());

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

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

    return $this->response;
  }

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

class UpdateAdressRequest
{
  public $anr;
  public $aguid;
}

class UpdateAdressResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $adress;
  public $columns;
}