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

namespace Kufer\KuferTools\Utils\StatusMessages;


class StatusMessageContainer
{
  protected $statusMessageObjAry = array();

  
  public static function getSharedInstance()
  {
    static $singelton = null;

    if (is_null($singelton)) {
      $singelton = new StatusMessageContainer();
    }

    return $singelton;
  }

  
  public function addStatusMessageObj(StatusMessage $pStatusMessageObj)
  {
    $this->statusMessageObjAry[] = $pStatusMessageObj;
  }

  
  public function addStatusMessageByOldObject($oldStatusCode)
  {
    if (isset($oldStatusCode['message']) && trim($oldStatusCode['message']) !== '') {
      switch ($oldStatusCode['process']) {
        default:
        case NO_UPLOAD:
          $type = MSG_BOX_INFO;
          break;
        case UPLOAD_OK:
          $type = MSG_BOX_SUCCESS;
          break;
        case UPLOAD_NOT_OK:
          $type = MSG_BOX_ERROR;
          break;
      }

      $statusMessageObj = new StatusMessage($type, $oldStatusCode['message']);
      $this->addStatusMessageObj($statusMessageObj);
    }
  }

  
  public function getStatusMessage($pMsgBoxType)
  {
    $result = '';
    foreach ($this->statusMessageObjAry as $statusMessageObj) {
      if ($statusMessageObj->getMessageBoxType() == $pMsgBoxType) {
        $result .= ($result != '' ? '<br />' : '') . $statusMessageObj->getStatusMessage();
      }
    }

    return $result;
  }

  public function getStatusMessages4Html()
  {
    
    if (count($this->statusMessageObjAry) === 0) {
      return '';
    }

    $msgBoxTypes = array(MSG_BOX_SUCCESS, MSG_BOX_ERROR, MSG_BOX_WARNING, MSG_BOX_INFO);

    $statusMeldungen = '';
    foreach ($msgBoxTypes as $msgBoxType) {
      $viewModel = new StatusMessageViewModel();
      $viewModel->msgBoxType = $msgBoxType;
      $viewModel->message = '';
      foreach ($this->statusMessageObjAry as $message) {
        if ($message->getMessageBoxType() == $msgBoxType) {
          $viewModel->message .= ($viewModel->message != '' ? '<br />' : '') . $message->getStatusMessage();
        }
      }
      if ($viewModel->message != '') {
        $view = new StatusMessageView($viewModel);
        $statusMeldungen .= $view->show();
      }
    }

    return $statusMeldungen;
  }

  public function clear()
  {
    $this->statusMessageObjAry = array();
  }

  public function getStatusMessageObjAry()
  {
    return $this->statusMessageObjAry;
  }

  
  public function setStatusMessages($pObject)
  {
    $pObject->messageSuccess = $this->getMessageStringSuccess();
    $pObject->messageError = $this->getMessageStringError();
    $pObject->messageWarning = $this->getMessageStringWarning();
    $pObject->messageInfo = $this->getMessageStringInfo();
  }

  public function getMessageStringSuccess()
  {
    return $this->getStatusMessage(MSG_BOX_SUCCESS);
  }

  public function getMessageStringWarning()
  {
    return $this->getStatusMessage(MSG_BOX_WARNING);
  }

  public function getMessageStringError()
  {
    return $this->getStatusMessage(MSG_BOX_ERROR);
  }

  public function getMessageStringInfo()
  {
    return $this->getStatusMessage(MSG_BOX_INFO);
  }
}