<?php

namespace ModulesGarden\OpenXchangeAddon\Core\Traits;

use \ModulesGarden\OpenXchangeAddon\Core\HandlerError\ErrorCodes\ErrorCodesLib;
use \ModulesGarden\OpenXchangeAddon\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 \ModulesGarden\OpenXchangeAddon\App\Helpers\ErrorCodesLib();
        // }        
    }

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

        if (is_array($code))
        {
            $code = isset($code[ErrorCodes::CODE]) ? $code[ErrorCodes::CODE] : null;
        }

        return $this->errorCodesCoreHandler->getErrorMessageByCode($code);
    }
}
