<?php

namespace app\common\arw\adjfut\src\WeChat;

use think\facade\Config as FacadeConfig;
use think\helper\Arr;

class Config
{
    /**
     * 实例
     *
     * @var $this
     * @date 2022-05-19
     * @example
     * @author arw
     * @since 1.0.0
     */
    private static $instance = null;
    /**
     * 配置项
     *
     * @var array
     * @date 2022-05-19
     * @example
     * @author arw
     * @since 1.0.0
     */
    private $config = [
        // 中控层配置
        'access_key' => '',
        'server_base' => '',
        // 小程序配置
        'xcx' => [
            'appid' => '',
            'secret' => '',

            'cache_key' => '',
            'log_channel' => '',
        ],
        // 公众号配置
        'gzh' => [
            'appid' => '',
            'secret' => '',
            // 推送模板
            'template' => [
                '模板名称' => '模板id'
            ],

            'cache_key' => '',
            'log_channel' => '',
        ],
    ];

    public function __construct()
    {
        $setConfig = function (string $key) {
            $this->setConfig($key, FacadeConfig::get("wechat.$key"));
        };
        foreach ([
            'access_key', 'server_base',
            'xcx.cache_key', 'xcx.log_channel',
            'xcx.appid', 'xcx.secret',
            'gzh.cache_key', 'gzh.log_channel',
            'gzh.appid', 'gzh.secret', 'gzh.template'
        ] as $key) {
            $setConfig($key);
        }
    }

    /**
     * 实例
     *
     * @return self
     * @date 2022-05-19
     * @example
     * @author arw
     * @since 1.0.0
     */
    public static function instance(): self
    {
        if (!self::$instance) {
            self::$instance = new self;
        }
        return self::$instance;
    }

    /**
     * 设置配置项
     *
     * @param string $key
     * @param mixed $value
     * @return void
     * @date 2022-05-19
     * @example
     * @author arw
     * @since 1.0.0
     */
    public function setConfig(string $key, $value)
    {
        Arr::set($this->config, $key, $value);
    }

    /**
     * 获取配置项
     *
     * @param string $key
     * @param mixed $default
     * @return mixed
     * @date 2022-05-19
     * @example
     * @author arw
     * @since 1.0.0
     */
    public static function get(string $key, $default = null)
    {
        return Arr::get(
            self::instance()->config,
            $key,
            $default
        );
    }
}
