//
// Created by WJG on 2022-3-18.
//

#ifndef SMARTPANEL_COSS_LIGHTJIASCENEPROCESS_H
#define SMARTPANEL_COSS_LIGHTJIASCENEPROCESS_H

#include "json/json.h"
#include "scene_comm.h"


#define SCENE_GROUP_FILE "/scene_cfg.json"
#define MAX_GROUP_OF_SINGLE_DEVICE 6
class LightjiaSceneProcess {
public:
    explicit LightjiaSceneProcess(const std::string& base_path);
    explicit LightjiaSceneProcess(const std::string& base_path, const std::string& sid);
    ~LightjiaSceneProcess();

    // forceNoGroup: 不是否进行智能分组尝试，清理分组时不尝试
    // doGroupActionNow: 如需要、是否立即向vdal进行分组指令发送
    int parseLightjiaSceneFile(bool &groupped, const std::string &lightjia_file, const std::string &scene_file_dir,
                               bool doGroupActionNow, bool forceNoGroup = false );
    int refreshSceneConfig();

private:
    std::string basePath;

    Json::Value result;
    Json::Value actions;
    Json::Value conditions;
    Json::Value rules;
    Json::Value new_actions;

    std::string scene_id;
    Json::Value scene_cfg_root;

    bool parseLightjiaAction(const Json::Value &_lac);
    bool parseLightjiaCondition(const Json::Value &_lcons);
    int regroupLightjiaAction(bool &groupped, bool doRegroupAction);
//    void defaultSubscribeLight();
    std::string getDevidHash(const std::unordered_set<std::string>& did_set);
    std::string divideIntoGroup(int &cntOfGroup, string &did_hash, unordered_set<std::string> &did_set, bool doRegroupActionNow);
    int regroupProcess(int start_pos, int end_pos, bool doRegroupActionNow);
    bool breakFromGroup(const std::string& gp_id, Json::Value dev_array);

    //清空指定场景的分组信息
    void clearSceneGroupPairConfigIfExists();

    int secondCheckSceneGroupPair();
    void saveSceneConfig();
    void loadSceneConfig();

public:
    int clearScenelessGroups();
    std::string getSceneGroupAddr();
};


class SceneRegroupWorker : public CThread {
    void Run() override ;
};

class SceneClearGroupWorker : public CThread {
    void Run() override ;
};

#endif //SMARTPANEL_COSS_LIGHTJIASCENEPROCESS_H
