<?php

namespace Hura8\System;

use Hura8\Traits\ClassCacheTrait;

class Config
{

    use ClassCacheTrait;


    public static function getRequestLanguage() : string {
        return static::getCache("getRequestLanguage", function () {
            $lang = $_REQUEST[Constant::LANGUAGE_ID] ?? DEFAULT_LANGUAGE;

            return (array_key_exists($lang , Constant::languagePermitList())) ? $lang : DEFAULT_LANGUAGE;
        });
    }


    public static function getClientInfo() {
        return static::getCache("getClientInfo", function () {
            $config_file = CONFIG_DIR . '/client/client.info.php';
            if(file_exists($config_file)) {
                return include $config_file;
            }

            return [];
        });
    }


    public static function getProductHotTypeList(){

        return static::getCache('getProductHotTypeList', function (){
            $config_file = CONFIG_DIR . "/client/product.hottype.php";
            if(!file_exists($config_file)) {
                die("File: client/product.hottype.php not exist !");
            }

            return include $config_file;
        });
    }


    public static function getRoutes() {
        return static::getCache("getRoutes", function () {
            $config_file = CONFIG_DIR . '/client/routing_main.php';
            if(!file_exists($config_file)) {
                die("File not exist: config/client/routing_main.php");
            }

            return include $config_file;
        });
    }


    public static function getEntityLanguageFields() {
        return static::getCache("getEntityLanguageFields", function () {

            $config_file = CONFIG_DIR . '/client/language_fields.php';
            if(!file_exists($config_file)) {
                die("File not exist: config/client/language_fields.php");
            }

            return include $config_file;
        });
    }


    public static function getSettingsEmail() {
        return static::getCache("getSettingsEmail", function () {

            $system_config_file = CONFIG_DIR . '/system/settings.email.php';
            if(!file_exists($system_config_file)) {
                die("File not exist: config/system/settings.email.php");
            }

            $system_config = include $system_config_file;

            // client extension
            $client_config_file = CONFIG_DIR . '/client/settings.email-extend.php';
            $client_config = [];
            if(file_exists($client_config_file)) {
                $client_config = include $client_config_file;
            }

            $final_config = $system_config;

            if(isset($client_config['email'])) {
                $final_config['email'] = array_merge($final_config['email'], $client_config['email']);
            }

            if(isset($client_config['template'])) {
                $final_config['template'] = array_merge($final_config['template'], $client_config['template']);
            }

            return $final_config;

        });
    }


    public static function getSettingsPrint() {

        return static::getCache("getSettingsPrint", function () {

            $config_file = CONFIG_DIR . '/system/settings.print.php';
            if (!file_exists($config_file)) {
                // die("File not exist: config/system/settings.print.php");
                return [];
            }

            return include $config_file;

        });
    }


    public static function getRelationConfigForItem($item_type) {

        return static::getCache("getRelationConfigForItem-".$item_type, function () use ($item_type) {

            $config_relation_file = ROOT_DIR . '/config/client/config_relation.php';
            if(!file_exists($config_relation_file)) {
                die("config/client/config_relation.php does not exist!");
            }

            $system_relation_file = ROOT_DIR . '/config/system/relation_config.php';
            if(!file_exists($system_relation_file)) {
                die("config/system/relation_config.php does not exist!");
            }

            $config_relation    = include $config_relation_file;
            $available_content  = include $system_relation_file;

            if(isset($config_relation[$item_type])) {
                $config = array();
                foreach ($config_relation[$item_type] as $type) {
                    if(isset($available_content[$type])) $config[$type] = $available_content[$type];
                }
                return $config;
            }

            return [];

        });

    }


    public static function getProductImageTypes(){
        $config_image_type_file = ROOT_DIR . "/config/client/config_product_image_types.php";
        if(!file_exists($config_image_type_file)){
            return [
                'standard'  => 'Hình sản phẩm',
            ];
        }

        return include $config_image_type_file;
    }


    public static function getProductUnitList() {
        return static::getCache("getProductUnitList", function (){
            $config_file = CONFIG_DIR . "/client/product.unit.php";
            if(file_exists($config_file)) {
                return include $config_file;
            }

            return [];
        });
    }


    public static function getLanguageCount(){
        $language_list = static::getLanguageConfig();
        $count = sizeof($language_list);
        return ($count > 1) ? $count : 1; //always have at least 1 language
    }


    public static function getLanguageConfig(){
        return static::getCache('getLanguageConfig', function (){
            $config_file = CONFIG_DIR . "/client/language_enable.php";
            if(!file_exists($config_file)) {
                return [];
            }

            return include $config_file;
        });
    }


    public static function getClientBuildConfig() {
        return static::getCache("getClientBuildConfig", function (){
            $config_file = CONFIG_DIR . '/build/store.config.php';
            if(file_exists($config_file)) {
                return include $config_file;
            }

            return [];
        });
    }


}
