<?php

namespace CMSMSExt;


use ArrayAccess;

/**
 * Class ModuleConfig
 *
 * @package CMSMSExt
 */
class ModuleConfig implements ArrayAccess
{
  #---------------------
  # Attributes
  #---------------------
  
  /**
   * @var array
   */
  private array $_data = [];
  /**
   * @var \cms_config
   */
  private \cms_config $_CMSMS_CFG;
  
  #---------------------
  # Magic methods
  #---------------------
  
  /**
   * ModuleConfig constructor.
   *
   * @param \CMSModule $mod
   */
  public function __construct(\CMSModule $mod)
  {
    $this->_CMSMS_CFG                    = \cmsms()->GetConfig();
    $this->_data['module_path']          = $mod->GetModulePath();
    $this->_data['mod_name']             = $mod->GetName();
    
    $fn = \cms_join_path($this->_data['module_path'], 'data', 'cfg.inc');
    $config = [];
    
    if( \is_readable($fn) )
    {
      include($fn);
      
      if( isset($config) )
      {
        foreach($config as $key => $value)
        {
          $this->_data[$key] = $value;
        }
      }
    }
  }
  
  /**
   * Get a data by key
   *
   * @param string The key data to retrieve
   *
   * @access public
   * @return mixed|null
   */
  public function __get ($key)
  {
    if($this->_CMSMS_CFG->offsetExists($key)) { return $this->_CMSMS_CFG[$key]; }
    
    return $this->_data[$key];
  }
  
  /**
   * is triggered by calling isset() or empty() on inaccessible members.
   *
   * @param $key
   *
   * @return bool
   * @link https://php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members
   */
  public function __isset($key)
  {
    return $this->offsetExists($key);
  }
  
  
  /**
   * @param $key
   * @param $value
   */
  public function __set($key, $value)
  {
    throw new \RuntimeException('Read Only');
  }
  
  /**
   * @param mixed $key
   *
   * @return bool
   */
  public function offsetExists($key) : bool
  {
    return ( isset($this->_data[$key]) || $this->_CMSMS_CFG->offsetExists($key) );
  }
  
  /**
   * @param mixed $key
   *
   * @return bool|mixed|string|void|null
   */
  #[\ReturnTypeWillChange]
  public function offsetGet($key)
  {
    if( $this->_CMSMS_CFG->offsetExists($key) ) { return $this->_CMSMS_CFG[$key]; }
    
    if(isset($this->_data[$key]))
    {
      $ret = $this->_data[$key];
    }
    else
    {
      $ret = NULL;
    }
    
    return $ret;
  }
  
  /**
   * @param mixed $key
   * @param mixed $value
   */
  public function offsetSet($key, $value) : void
  {
    throw new \RuntimeException('Read Only');
  }
  
  /**
   * @param mixed $key
   */
  public function offsetUnset($key) : void
  {
    throw new \RuntimeException('Read Only');
  }
  
  /**
   * @param $key
   *
   * @return bool|mixed|string|void|null
   */
  public function get($key)
  {
    if($this->_CMSMS_CFG->offsetExists($key)) { return $this->_CMSMS_CFG[$key]; }
    if( $this->offsetExists($key) ) { return $this->_data[$key]; }
  }
  
  /**
   * @param $key
   * @param $value
   */
  public function set($key, $value)
  {
    throw new \RuntimeException('Read Only');
  }
  
  /**
   * @param $key
   */
  public function erase($key)
  {
    throw new \RuntimeException('Read Only');
  }
}
?>