class ForEachScriptPull : public IForEachScriptObjectFunctor
{
public:
	ForEachScriptPull() : result(false)
	{}

	virtual bool Visit(VMScriptInstance * script, void * arg2)
	{		
		VMClassInfo* classInfo = script->classInfo;
		VMClassInfo* _classInfo = classInfo;
		while (classInfo)
		{
			_MESSAGE("%s classInfo : %s", GetDayTime().c_str(), _classInfo->name.data);
			std::string s = _classInfo->name.data;
			std::transform(s.begin(), s.end(), s.begin(), tolower);
			if (s.compare("norlever01script") == 0)
			{
				VMClassRegistry* registry = (*g_skyrimVM)->GetClassRegistry();
				VMIdentifier* identifier = NULL;
				if (registry->Unk_13(&_classInfo->name, &identifier))
				{
					if (identifier)
					{
						std::string str = identifier->state.data;
						//_MESSAGE("%s state data : %s", GetDayTime().c_str(), str.c_str());
						std::transform(str.begin(), str.end(), str.begin(), tolower);
						if (str.compare("pulledposition") == 0)
						{
							_MESSAGE("%s Get pulledposition state!!", GetDayTime().c_str(), _classInfo->name.data);
							result = true;
							return false;
						}
					}
				}
			}
			_classInfo = _classInfo->parent;
			if (!_classInfo)
				classInfo = _classInfo;
		}

		return true;
	}

	bool result;
};

class ForEachScriptPush : public IForEachScriptObjectFunctor
{
public:
	ForEachScriptPush() : result(false)
	{}

	virtual bool Visit(VMScriptInstance * script, void * arg2)
	{
		VMClassInfo* classInfo = script->classInfo;
		VMClassInfo* _classInfo = classInfo;
		while (classInfo)
		{
			std::string s = _classInfo->name.data;
			std::transform(s.begin(), s.end(), s.begin(), tolower);
			if (s.compare("norlever01script") == 0)
			{
				VMClassRegistry* registry = (*g_skyrimVM)->GetClassRegistry();
				VMIdentifier* identifier = NULL;
				if (registry->Unk_13(&classInfo->name, &identifier))
				{
					if (identifier)
					{
						std::string str = identifier->state.data;
						std::transform(str.begin(), str.end(), str.begin(), tolower);
						if (str.compare("pushedposition") == 0)
						{
							result = true;
							return false;
						}
					}
				}
			}
			classInfo = classInfo->parent;
		}

		return true;
	}

	bool result;
};

class Papyrus
{	
public:
		static SInt32 GetStateLeverScript(TESObjectREFR* ref)
		{
			int value = 0;
			if (!ref)
				return value;
			
			VMClassRegistry* registry = (*g_skyrimVM)->GetClassRegistry();
			IObjectHandlePolicy* policy = registry->GetHandlePolicy();
			UInt64 handle = policy->Create(ref->kTypeID, ref);
			
			ForEachScriptPull pullfinder;
			registry->VisitScripts(handle, &pullfinder);

			ForEachScriptPush pushfinder;
			//registry->VisitScripts(handle, &pushfinder);

			//ForEachScriptBusy busyfinder;
			//registry->VisitScripts(handle, &busyfinder);

			if (pullfinder.result)
			{
				value = 1;
			}			
			else if (pullfinder.result)
			{
				value = 2;
			}

			return value;
		}
};