//
// Created by WJG on 2020-11-16.
//

#ifndef NEWCOSS_SCENEWORKER_H
#define NEWCOSS_SCENEWORKER_H

#include "json/json.h"
#include "../MsgSchedule/CThread.h"
#include "SceneManager.h"

class SceneWorker : public CThread, SceneWorkHandler {
public:
    void Run() override;

private:
    SceneThreadPool *pool;
    int exe_mod;   //0-条件触发执行，1-app触发执行，2-场景触发执行
    std::string scene_id;
    std::string scene_name;
    std::vector<Json::Value> rules_vector;
    Json::Value scene_rule;
    Json::Value input_param;

    std::mutex   m_;
    bool         cancel_ = false;
    std::condition_variable sleep_wait_;

    void processScnenRule();
    bool dealRule(Json::Value &rule);
    bool dealAction(Json::Value &action);
    bool dealSpecialAction(Json::Value &actions);
    bool dealCondition(Json::Value &condition, int mul_cmp);
    bool dealOperator(const std::string &op, const std::string &cmp_value, const std::string &value1, const std::string& value2);
    bool dealOperator(const std::string &op, int cmp_value, int value1, int value2);
//    bool dealOperator(std::string &op, std::string &rule_value, Json::Value &in_param);
//    void deviceAction(std::string &action_str, Json::Value &action_param);
//    void systemAction(std::string &action_str, Json::Value &action_param);
//    void appAction(std::string &action_str, Json::Value &action_param);

    void cancelSceneRunning() override;

    bool checkCondition(const std::string& condition_key, const std::string& condition_cmp,
                        const std::string& dev_value, const std::string& condition_v1, const std::string& condition_v2);
};

#endif //NEWCOSS_SCENEWORKER_H
