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

declare(strict_types = 1);

namespace Kufer\Mail\Sender\Result;

use Kufer\Mail\Address\IEMailAddress;

final class EMailSentRecipient
{
  
  protected $statusCode;

  
  protected $recipient;

  
  protected $recipientGroupName;

  
  public function __construct(int $statusCode, IEMailAddress $recipient, string $recipientGroupName)
  {
    $this->statusCode = $statusCode;
    $this->recipient = $recipient;
    $this->recipientGroupName = $recipientGroupName;
  }

  
  public function getStatusCode(): int
  {
    return $this->statusCode;
  }

  
  public function getRecipient(): IEMailAddress
  {
    return $this->recipient;
  }

  
  public function getRecipientGroupName(): string
  {
    return $this->recipientGroupName;
  }

  
  public function setRecipientPosition(string $position): void
  {
    $this->recipientGroupName = $position;
  }

  
  public function isError(): bool
  {
    return EMailSentStatusCode::hasError($this->getStatusCode());
  }

  
  public function isWarning(): bool
  {
    return EMailSentStatusCode::isWarning($this->getStatusCode());
  }

  
  public function isSuccess(): bool
  {
    return EMailSentStatusCode::isSuccess($this->getStatusCode());
  }

  
  public function getReadableMessage(?string $position = null): string
  {
    $position = $position ?? $this->getRecipientGroupName();
    switch ($this->getStatusCode()) {
      default:
      case EMailSentStatusCode::FAILURE_UNKNOWN_ERROR:
        return 'Fehler beim Versenden einer E-Mail an ' . $position . ' aufgetreten';
      case EMailSentStatusCode::SUCCESSFULL:
        return 'E-Mail erfolgreich an ' . $position . ' versendet';
      case EMailSentStatusCode::WARNING_EMAIL_SENDER_NOT_FOUND:
        return 'E-Mail erfolgreich an ' . $position . ' versendet, '
          . 'jedoch ist E-Mail Adresse beim Versender eingetragen';
    }
  }

  
  public function getReadableMessageWithRecipientName(bool $showEmail = false): string
  {
    $sender = $this->getRecipient()->getEMailName();
    if ($showEmail
        && !\is_null($this->getRecipient()->getEMailAddress())
        && \trim($this->getRecipient()->getEMailAddress()) !== '') {
      $sender .= ' <' . $this->getRecipient()->getEMailAddress() . '>';
    }

    return $this->getReadableMessage($sender);
  }
}