//
// Created by Ht on 2022/10/21.
//

#ifndef IOT_EDGE_VDALNEW_QGAUTILS_H
#define IOT_EDGE_VDALNEW_QGAUTILS_H

#include <qlibc/qlibc.h>

namespace jedge {
    const static char* sval_os_cygwin_ = "cygwin";
    const static char* sval_os_windows_ = "windows";
    const static char* sval_os_macos_ = "macos";
    const static char* sval_os_android_ = "android";
    const static char* sval_os_prod_ = "prod";
    const static char* sval_os_respi_ = "respi";
    const static char* sval_os_linux_ = "linux";
    const static char* sval_os_default_ = "default";

    class JE_API QGAOUtils {

    public:
        static const std::string getPlatformOsName() {
#ifdef CYGWIN
            return sval_os_cygwin_;
#elif defined(ANDROID)
            return sval_os_android_;
#elif defined(MACOS)
            return sval_os_macos_;
#elif defined(WIN32)
            return sval_os_windows_;
#elif defined(RESPI)
            return sval_os_respi_;
#elif defined(PRODUCT)
            return sval_os_prod_;
#elif defined(LINUX)
            return sval_os_linux_;
#else
            return sval_os_default_;
#endif
        }

        using QValueAccessor = std::function<void(Json::Value&)>;
        static void  iterateDataValues(Json::Value& data, const QValueAccessor& accessor) {
            for(Json::Value& v : data) {
                if(v.isObject()) {
                    iterateDataValues(v, accessor);
                } else if(v.isArray()) {
                    for(auto& av : v)
                        iterateDataValues(av, accessor);
                } else {
                    accessor(v);
                }
            }
        }
        static qlibc::QData transUintToHexString(const qlibc::QData &src) {
            qlibc::QData ret(src);
            iterateDataValues(ret.asValue(), [](Json::Value& v) {
//                QDLog("Trying value : %s", StringUtils::valueToJsonString(v).c_str());
               if(v.isNumeric()) {
                   auto vv = v.asUInt();
                   if(vv>0xFFFF)
                       v = ByteUtils::dwordToHexStr(vv);
                   else if(vv>0xFF)
                       v = ByteUtils::wordToHexStr(vv);
                   else
                       v = ByteUtils::byteToHexStr(vv);
               } else if(v.isUInt64()) {
                   v = ByteUtils::qwordToHexStr(v.asUInt64());
               }
            });
            return ret;
        }
    };


}


#endif //IOT_EDGE_VDALNEW_QGAUTILS_H
