<?php

namespace ThurData\Servers\KerioEmail\Core\Traits;

use \ThurData\Servers\KerioEmail\Core\HandlerError\ErrorCodes\ErrorCodesLib;
use \ThurData\Servers\KerioEmail\Core\HandlerError\ErrorCodes\ErrorCodes;

trait ErrorCodesLibrary
{
    /** 
     * @var ErrorCodesLib
     */
    protected $errorCodesCoreHandler = null;

    /** 
     * @var ErrorCodesLib
     */
    protected $errorCodesAppHandler = null;
    
    public function loadErrorCodes()
    {
        if ($this->errorCodesCoreHandler === null)
        {
            $this->errorCodesCoreHandler = new ErrorCodesLib();
        }
        
        if ($this->errorCodesAppHandler === null)
        {
            $this->errorCodesAppHandler = new \ThurData\Servers\KerioEmail\App\Helpers\ErrorCodesLib();
        }        
    }

    public function genErrorCode($code = null)
    {
        $this->loadErrorCodes();

        if ($this->errorCodesAppHandler->errorCodeExists($code))
        {
            return $this->errorCodesAppHandler->getErrorMessageByCode($code);
        }
       
        return $this->errorCodesCoreHandler->getErrorMessageByCode($code);
    }
}
