<?php
/**
* 2024 Anvanto
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
*
*  @author    Anvanto <anvantoco@gmail.com>
*  @copyright 2024 Anvanto
*  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*/

if (!defined('_PS_VERSION_')) {
    exit;
}

class AnblogConfig
{
    public $params;
    public $cat_image_dir = '';
    /**
     * @var int id_lang current language in for, while
     */
    public $cur_id_lang = '';
    /**
     * @var int id_lang current language in for, while
     */
    public $cur_prefix_rewrite = '';
    const CACHE_HOOK_ID = 'anblogHooks';

    public static function getInstance()
    {
        static $instance;
        if (!$instance) {
            // validate module
            $instance = new AnblogConfig();
        }
        return $instance;
    }

    public function __construct()
    {
       

        $defaultSettings = include _PS_MODULE_DIR_ . 'anblog/config.php';

        $valuesWithLang = ['blog_link_title', 'meta_title', 'meta_description', 'meta_keywords', 'category_rewrite', 'detail_rewrite'];

        $allConfig = [];

        foreach ($defaultSettings as $key => $value) {
            if (in_array($key, $valuesWithLang)) {
                $allConfig[$key] = Configuration::get('an_bl_' . $key , Context::getContext()->language->id, $value);
            } else {
                $allConfig[$key] = Configuration::get('an_bl_' . $key , null, $value);
            }
        } 

        $this->params = $allConfig;
    }

    public function mergeParams($params)
    {
        // validate module
        unset($params);
    }

    public function setVar($key, $value)
    {
        $this->params[$key] = $value;
    }

    public function get($name, $value = '')
    {
        if (isset($this->params[$name])) {
            // validate module
            return $this->params[$name];
        }
        return $value;
    }

    public static function getConfigName($name)
    {
        return Tools::strtoupper(_AN_BLOG_PREFIX_.$name);
    }

    public static function updateConfigValue($name, $value = '')
    {
        Configuration::updateValue(self::getConfigName($name), $value, true);
    }

    public static function getConfigValue($name)
    {
        return Configuration::get(self::getConfigName($name));
    }
    
}
