#include<iostream>
#include<string>
#include<vector>
#include<rapidjson/document.h>
#include<rapidjson/filereadstream.h>
#include<fstream>

using namespace std;
using namespace rapidjson;


class FSMBase
{
public:
      string StateName;
      string EventName;
      std::vector<FSMBase*> subitem;

public:

      FSMBase()
      {
            std::cout << "default constructor called" << std::endl;
      }

      void getdata()
      {
            std::cout << "FSMBase called" << std::endl;
      }

      virtual void Add(FSMBase* b1)
      {
            subitem.push_back(b1);
            b1->getdata();
      }

      ~FSMBase()
      {
            std::cout << " destructor called" << std::endl;
      }
};


class premitivetool :public FSMBase
{
public:

      void Add(FSMBase* b1)
      {
            subitem.push_back(b1);
            b1->getdata();
      }
};

class Event : public FSMBase
{
      string ActionType;
      string ActionParam;
public:

      void getdata(string Name, string ActType, string ActPar)
      {
            EventName = Name;
            ActionType = ActType;
            ActionParam = ActPar;
            std::cout << " " << EventName << " " << ActionType << " " << ActionParam << std::endl;
      }
};

class state :public FSMBase
{
      string EntreyMethodName;
      std::vector<Event*> eventitem;
public:

      void getdata(string Name, string Methodname)
      {
            StateName = Name;
            EntreyMethodName = Methodname;
            std::cout << "  " << StateName << "  " << EntreyMethodName << std::endl;
      }

      void Add(FSMBase* fsmbase1)
      {
            subitem.push_back(fsmbase1);
            fsmbase1->getdata();
      }
};

void createStatesFromJson(FSMBase* primitiveTool, Document& doc) {
      if (doc.HasMember("PrimitiveTool") && doc["PrimitiveTool"].HasMember("State") && doc["PrimitiveTool"]["State"].IsArray()) {
            const Value& states = doc["PrimitiveTool"]["State"];
            for (SizeType i = 0; i < states.Size(); ++i) {
                  const Value& stateVal = states[i];
                  state* StateObj = new state();

                  if (stateVal.HasMember("Name") && stateVal["Name"].IsString()) {
                        StateObj->getdata(stateVal["Name"].GetString(), stateVal["EntryMethodName"].GetString());
                  }

                  if (stateVal.HasMember("Event")) {
                        if (stateVal["Event"].IsArray()) {
                              const Value& events = stateVal["Event"];
                              for (SizeType j = 0; j < events.Size(); ++j) {
                                    const Value& eventVal = events[j];
                                    Event* EventObj = new Event();

                                    if (eventVal.HasMember("Name") && eventVal["Name"].IsString()) {
                                          string eventName = eventVal["Name"].GetString();
                                          string actionType = eventVal.HasMember("ActionType") ? eventVal["ActionType"].GetString() : "";
                                          string actionParam = eventVal.HasMember("ActionParam") ? eventVal["ActionParam"].GetString() : "";
                                          EventObj->getdata(eventName, actionType, actionParam);
                                          StateObj->Add(EventObj);
                                    }
                              }
                        }
                  }
                  primitiveTool->Add(StateObj);
            }
      }
}

int main()
{

      Document doc;
      // Open the file
      ifstream file("FSMImplementationJSON.json");
      if (!file.is_open()) {
            cerr << "Could not open file: FSMImplementationJSON.json" << endl;
            return 1;
      }

      // Read the entire file into a string
      string json((istreambuf_iterator<char>(file)),
            istreambuf_iterator<char>());

      

      // Parse the JSON data
      doc.Parse(json.c_str());
      //std::cout << doc["Name"].GetString() << std::endl;

      // Check for parse errors
      if (doc.HasParseError()) {
            cerr << "Error parsing JSON: "
                  << doc.GetParseError() << endl;
            return 1;
      }

in response to above code,if i want to check particular state and event how will i check that
      
      // Create primitive tool and load states
      premitivetool* prtool1 = new premitivetool();
      createStatesFromJson(prtool1, doc);

      // Clean up
      delete prtool1;
}