#include "inc/motionGloveSdkHelper.h"

/*
ret: index, -1 not find
*/
int GloveSuitManager_SearchByName(GloveSuitManager_TypeDef* mssm, int len, uint8_t* actorName, int nameLen)
{
	int ret = -1;
	if (nameLen > ACTOR_NAME_LEN_MAX)
	{
		nameLen = ACTOR_NAME_LEN_MAX;
	}

	if (nameLen <= 0)
	{
		return ret;
	}
	if (mssm == 0)
	{
		return ret;
	}
	if (len <= 0)
	{
		return ret;
	}


	for (int i = 0; i < len; i++)
	{
		if (mssm[i].actorName[0] == 0x00)
		{
			/*跳过为0的*/
			continue;
		}
		else
		{
			if (0 == memcmp(actorName, mssm[i].actorName, nameLen))
			{
				ret = i;
				break;
			}
		}
	}

	return ret;
}


int GloveSuitManager_getFirstUnOccupy(GloveSuitManager_TypeDef* mssm, int len)
{
	int ret = -1;


	for (int i = 0; i < len; i++)
	{
		if (mssm[i].actorName[0] == 0x00)
		{
			ret = i;
			/*跳过为0的*/
			break;
		}

	}

	return ret;
}


/*
ret: suit index
*/
int GloveSuitManager_AddName(GloveSuitManager_TypeDef* mssm, int len, uint8_t* actorName, int nameLen)
{
	int ret = -1;
	if (nameLen > ACTOR_NAME_LEN_MAX)
	{
		nameLen = ACTOR_NAME_LEN_MAX;
	}

	if (nameLen <= 0)
	{
		return ret;
	}

	int unOccupyIdx = GloveSuitManager_getFirstUnOccupy(mssm, len);
	if (unOccupyIdx < 0)
		return ret;
	for (int i = 0; i < nameLen; i++)
	{
		mssm[unOccupyIdx].actorName[i] = actorName[i];
	}
	mssm[unOccupyIdx].actorNameLen = nameLen;
	return unOccupyIdx;
}

int isFrameCSV_Glove(const char* buff)
{
	int ret = -1;
	if (buff == 0)
		return ret;

	std::string buffStr(buff);
	size_t foundGlove = buffStr.find("Glove");
	if (foundGlove != std::string::npos)
	{
		if (foundGlove == 0)
		{
			ret = 0;

			return ret;
		}
		//found
	}
	else
	{
		//not found
	}
	return ret;
}
int isFrameCSV_FullBodyMocap(const char* buff)
{
	int ret = -1;
	if (buff == 0)
		return ret;

	//----
	std::string buffStr(buff);
	size_t foundFullBodyMocap = buffStr.find("Actor");
	if (foundFullBodyMocap != std::string::npos)
	{
		if (foundFullBodyMocap == 0)
		{
			ret = 0;

			return ret;
		}
		//found
	}
	else
	{
		//not found
	}
	return ret;
}
/*
检查字符串是否是csv格式的
特征是字符串以Glove或者Actor开头
*/
int isFrameCSVFormat(const char* buff)
{
	int ret = -1;
	if (buff == 0)
		return ret;

	if ((0 == isFrameCSV_Glove(buff))||
		(0 == isFrameCSV_FullBodyMocap(buff))
		)
	{
		ret = 0;
		return ret;
	}

	return ret;
	
}