//
// Created by ljx on 2021/12/27.
//

#ifndef SMARTPANEL_COSS_GASTRANSTABLE_H
#define SMARTPANEL_COSS_GASTRANSTABLE_H

#include <qlibc/QData.hpp>
#include <qlibc/QPObjectHolder.h>
#include "../DevTree/DevTree.h"

using CommandHandler = std::function<void(const qlibc::QData& msgSrc, qlibc::QData& msgDest)>;


class GasTransTable{

protected:
    qlibc::QPObjectHolder<CommandHandler> controlTables;
    DevTree& mDevTree_;

public:
    explicit GasTransTable(DevTree& mDevTree):mDevTree_(mDevTree){
        initControlTables();
    }
    void transControlToGas(const qlibc::QData &req, qlibc::QData &req2Gas){
        std::string device_id = req.getString("device_id");
        std::string command = req.getString("command");
        Json::Value v = mDevTree_.getDevAllInfo(device_id);
        qlibc::QData v_data(v);
        std::string device_address = v_data.getString("device_address");
        std::stringstream ss;
        QHLog("control message all info is %s ", v_data.toJSONString(true).c_str());
        if(v_data.getString("device_class") == "GroupDevice"){
            int addr = atoi(device_address.c_str());
            ss << std::hex << addr;
            device_address = ss.str();
        }

        req2Gas.putString("gw_sn", v_data.getString("sn"));
        req2Gas.putString("device_address", device_address);
        req2Gas.putString("command", command);
        req2Gas.putString("uri", "/lightGW/controlDevice");
        req2Gas.putString("cmd_source", "cpad");

        auto handler = controlTables.findObject(command);
        if(handler != nullptr)
            handler->operator () (req, req2Gas);

    }

private:


    void initControlTables(){
        controlTables.appendNew("turnOn", new CommandHandler([](const qlibc::QData &req, qlibc::QData &req2Gas) {
            qlibc::QData qd_param;
            req2Gas.putObjFmtData("param", qd_param);
        }));
        controlTables.appendNew("turnOff", new CommandHandler([](const qlibc::QData &req, qlibc::QData &req2Gas) {
            qlibc::QData qd_param;
            req2Gas.putObjFmtData("param", qd_param);
        }));
        controlTables.appendNew("setBrightness", new CommandHandler([](const qlibc::QData &req, qlibc::QData &req2Gas) {
            req2Gas.putObjFmtString("param.set_value", req.getObjFmtString("param.set_value"));
        }));
        controlTables.appendNew("adjustBrightness", new CommandHandler([](const qlibc::QData &req, qlibc::QData &req2Gas) {
            qlibc::QData qd_param;
            qd_param.putString("flag", req.getObjFmtString("param.flag"));
            qd_param.putString("adjust_value", req.getObjFmtString("param.adjust_value"));
            req2Gas.putData("param", qd_param);
        }));
        controlTables.appendNew("adjustColorTemperature", new CommandHandler([](const qlibc::QData &req, qlibc::QData &req2Gas) {
            qlibc::QData qd_param;
            qd_param.putString("flag", req.getObjFmtString("param.flag"));
            qd_param.putString("adjust_value", req.getObjFmtString("param.adjust_value"));
            req2Gas.putData("param", qd_param);
        }));
        controlTables.appendNew("setColorTemperature", new CommandHandler([](const qlibc::QData &req, qlibc::QData &req2Gas) {
            req2Gas.putObjFmtString("param.set_value", req.getObjFmtString("param.set_value"));
        }));
        controlTables.appendNew("setSaturation", new CommandHandler([](const qlibc::QData &req, qlibc::QData &req2Gas) {
            req2Gas.putObjFmtString("param.set_value", req.getObjFmtString("param.set_value"));
        }));
        controlTables.appendNew("setLighterMode", new CommandHandler([](const qlibc::QData &req, qlibc::QData &req2Gas) {
            req2Gas.putObjFmtString("param.set_value", req.getObjFmtString("param.set_value"));
        }));
        controlTables.appendNew("setColorMode", new CommandHandler([this](const qlibc::QData &req, qlibc::QData &req2Gas) {
            qlibc::QData qd_param;
            Json::Value v = mDevTree_.getDevAllInfo(req.getString("device_id"));
            qlibc::QData v_data(v);
//            qd_param.putString("lightness", v_data.getString("brightness_value"));
            qd_param.putString("lightness", req.getObjFmtString("param.lightness"));
            qd_param.putString("hue", req.getObjFmtString("param.hue"));
            req2Gas.putData("param", qd_param);
        }));
        controlTables.appendNew("setFlowMode", new CommandHandler([](const qlibc::QData &req, qlibc::QData &req2Gas) {
            req2Gas.putObjFmtString("param.flow_speed", req.getObjFmtString("param.flow_speed"));
        }));
        controlTables.appendNew("setCurtain", new CommandHandler([](const qlibc::QData &req, qlibc::QData &req2Gas) {
            req2Gas.putObjFmtString("param.set_value", req.getObjFmtString("param.set_value"));
        }));

    }

};



#endif //SMARTPANEL_COSS_GASTRANSTABLE_H

