//
// Created by Ht on 2021/1/17.
//

#ifndef LIBDEVCPP_MGBUSPATHSETTER_H
#define LIBDEVCPP_MGBUSPATHSETTER_H

namespace jedge {

    static const char* sval_jedge_data_cache_dir = "./jedge";
    static const char* vkey_service_data_base_dir = "dir";
    static const char* vkey_service_data_base_cache_ = "dir-cache";

    class JE_API MgbusPathSetter {
    protected:
        std::string             base_dir_;      //根目录、默认为当前目录
        std::string             cache_dir_ = sval_jedge_data_cache_dir;     //可写数据缓存目录、默认为.jedge目录

        //准备初始化目录
        void prepareDataDir(const qlibc::QData& config) {
            //initRootDir
            std::string root = config.getString(vkey_service_data_base_dir);
            if(root.empty()) {
                root = SystemUtil::getCurrentPath();
                QLog("Init dir set to : %s", root.c_str());
            }
            setDataInitRootDir(root);
            std::string cache = config.getString(vkey_service_data_base_cache_, root);
            if(cache.empty()) cache = root;
            setDataCacheRootDir(cache);
        };

    public:
        //获取数据根路径
        const std::string& getDataCacheRootDir() const {
            return cache_dir_;
        }

        const std::string& getDataInitRootDir() const {
            return base_dir_;
        }

        std::string getAppInitFileName(const std::string& relaFn) {
            std::string fn;
            FileUtils::contactFileNames(fn, getDataInitRootDir().c_str(), relaFn.c_str(), nullptr);
            return fn;
        }

        std::string getAppCacheFileName(const std::string& relaFn) {
            std::string fn;
            FileUtils::contactFileNames(fn, getDataCacheRootDir().c_str(), relaFn.c_str(), nullptr);
            return fn;
        }

        void setDataInitRootDir(const std::string& dir) {
            base_dir_ = dir;
        }

        void setDataCacheRootDir(const std::string& dir){
            cache_dir_ = dir;
        }

        void createSubCacheDir(const char* sub, ...){
            std::string path(cache_dir_);
            FileUtils::appendFilename(path, sub);
            //创建各子文件夹
            va_list ap;
            va_start(ap, sub);
            const char* ak;
            while(true){
                ak = va_arg(ap, const char*);
                if(ak== nullptr) break;
                FileUtils::appendFilename(path, ak);
            }
            va_end(ap);
            SystemUtil::createDirs(path);
        }

        std::string getSubCacheDirFullPath(const char* sub, ...){
            std::string path(cache_dir_);
            FileUtils::appendFilename(path, sub);
            va_list ap;
            va_start(ap, sub);
            const char* ak;
            while(true){
                ak = va_arg(ap, const char*);
                if(ak== nullptr) break;
                FileUtils::appendFilename(path, ak);
            }
            va_end(ap);
            return path;
        }

        MgbusPathSetter& loadCacheDataConfig(const std::string& file, qlibc::QData& target){
            target.loadFromFile(FileUtils::contactFilename(getDataCacheRootDir(), file));
            return *this;
        }

        MgbusPathSetter& loadInitDataConfig(const std::string& file, qlibc::QData& target){
            target.loadFromFile(FileUtils::contactFilename(getDataInitRootDir(), file));
            return *this;
        }
    };
}


#endif //LIBDEVCPP_MGBUSPATHSETTER_H
