<?php

namespace Hura8\System\Controller;

use Hura8\Components\Template\AdminController\ATemplateSetController;
use Hura8\System\Model\SettingModel;

class SettingController
{

    static $file_folder = "media/settings";

    protected  $objSettingModel;

    // reserved special keys which should not be created by admin (but the system)
    protected $special_keys = [];

    public function __construct()
    {
        $this->objSettingModel = new SettingModel();
        $this->special_keys = include CONFIG_DIR . "/system/special_settings_keys.php";
    }


    public function getAll(){
        return $this->objSettingModel->getAll();
    }


    public function getSpecialKeys() {
        $group_keys = $this->special_keys;
        return array_merge($group_keys['design'], $group_keys['system']);
    }


    public function get($key, $default =null){
        return $this->objSettingModel->get($key, $default);
    }


    public function delete($key){
        return $this->objSettingModel->delete($key);
    }


    // update bulk key-values
    public function updateBulk(array $key_values){
        foreach ($key_values as $key => $value) {
            $this->objSettingModel->updateOrCreate($key, $value);
        }
    }


    public function updateOrCreate($key, $value, $comment = '') {
        return $this->objSettingModel->updateOrCreate($key, $value, $comment );
    }


    public function getList(array $keys) {
        return $this->objSettingModel->getList($keys);
    }


    public function populateSpecialKeys() {
        $keys = [];
        foreach ($this->special_keys as $group => $_list) {
            $keys = array_merge($keys, $_list);
        }

        return $this->objSettingModel->populateKeys($keys);
    }


    //tao config file tu database va upload vao thu muc website
    // Global config variables available to the whole website
    // includes:
    /*
     * - domain setting
     * - main domain
     * - template_set
     * - exchange rate
     * - password to unlock website
     * - google analytic verification
     * - google webmaster tool verification
     * - number of products to display
     * - default product display type: list|grid
     *
     * */
    public function create_config_file_n_upload(){

        $objDomainController     = new DomainController();
        $config_domains = $objDomainController->buildDomainConfig();

        $config = [];

        // * - domain setting
        $config['domains'] = $config_domains;

        // * - template_set
        $objATemplateSetController  = new ATemplateSetController();
        $config['template_set'] = $objATemplateSetController->getActivatedSet();

        // * - exchange rate
        // * - password to unlock website
        // * - google analytic verification
        // * - google webmaster tool verification
        // * - number of products to display
        // * - default product display type: list|grid
        $config['setup'] = $this->getList(array(
            "web_close_pass" ,
            "web_close_message" ,
            "exchange_rate"    ,
            "google_domain_verify" ,
            "product_per_page" ,
            "product_default_order",
            "site_manager" ,
            "site_manager_access_key",
        ));

        $config_file = CONFIG_DIR . "/build/store.config.php";

        $config_head = "<?php
/**
 * Author: HuraSoft
 * Generated time : ".date("d-m-Y, g:ia")."
 */
";
        $config_content = "return ".var_export($config, true) .";";

        @file_put_contents($config_file, $config_head. $this->_minifyCode($config_content));
    }


    //this is a greatly simplified version
    protected function _minifyCode($text) {
        //remove line break
        $text = str_replace(["\n", "\r", "\t"], " ", $text);
        //remove double spacings
        $text = preg_replace("/(\s+)/i", " ", $text);

        return trim($text);
    }

}
