/* MIT License (From https://choosealicense.com/ )

Copyright (c) 2017 Jonathan Burget support@solarfusionsoftware.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.*/

#include "TigerEngine.h"

// Global variable so the objects are not lost
static TIGValue *theNumberStack;
static TIGValue *theStringStack;
static int stackNumber;
static char *stackString;

// ************* For debugging Purposes *************
TIGValue *TIGStringNumberStack(void)
{
	return theNumberStack;
}

TIGValue *TIGStringStringStack(void)
{
	return theStringStack;
}
// ************* For debugging Purposes *************

void TIGStringStartStack(const char *startStackString)
{
	// If there is another start stack called before the end stack free it
	if (stackString != NULL)
	{
		free(stackString);
		stackString = NULL;
	}

	if (startStackString == NULL)
	{
		stackNumber++;
	}
	else
	{
		stackString = (char *)malloc((strlen(startStackString) + 1) * sizeof(char));
		
		if (startStackString != NULL)
		{
			strcpy(stackString, startStackString);
		}
	}
}

void TIGStringEndStack(const char *endStackString)
{
	if (endStackString != NULL)
	{
		while (theStringStack != NULL)
		{
			TIGValue *theNextStack = theStringStack->nextStack;
			
			// 0 means both strings are the same
			if (strcmp(theStringStack->stackString, endStackString) == 0)
			{
				theStringStack = TIGStringDestroy(theStringStack);
			}
			
			theStringStack = theNextStack;
		}
	}
	else
	{
		while (theNumberStack != NULL)
		{
			TIGValue *theNextStack = theNumberStack->nextStack;
			
			if (theNumberStack->stackNumber == stackNumber)
			{
				theNumberStack = TIGStringDestroy(theNumberStack);
			}
			
			theNumberStack = theNextStack;
		}
	}
	
	// If there is another end or start stack string called before this end stack free it
	if (stackString != NULL)
	{
		free(stackString);
		stackString = NULL;
	}

	if (endStackString == NULL)
	{
		stackNumber--;
	}
}

TIGValue *TIGStringCreate(TIGValue *tigString, TIGBool useStack)
{
	tigString = (TIGValue *)malloc(1 * sizeof(TIGValue));
	
	if (tigString == NULL)
	{
#ifdef TIG_DEBUG
		printf("ERROR Function:TIGStringCreate() Variable:tigString Equals:NULL\n");

	#ifdef TIG_DEBUG_ASSERT
		assert(0);
	#endif
#endif
		return NULL;
	}
	
	if (useStack)
	{
		if (stackString != NULL)
		{
			if (theStringStack == NULL)
			{
				tigString->nextStack = NULL;
			}
			// Add the last added TIGString to the new tigString's ->nextStack
			else
			{
				tigString->nextStack = theStringStack;
			}
			
			tigString->stackNumber = -1;
			tigString->stackString = (char *)malloc((strlen(stackString) + 1) * sizeof(char));
			
			if (tigString->stackString != NULL)
			{
				strcpy(tigString->stackString, stackString);
			}
			else
			{
#ifdef TIG_DEBUG
				printf("ERROR Function:TIGStringCreate() Variable:tigString->stackString Equals:NULL\n");

	#ifdef TIG_DEBUG_ASSERT
		assert(0);
	#endif
#endif
			}
			
			// This adds the new tigString to the global TIGString stack
			theStringStack = tigString;
		}
		else
		{
			if (theNumberStack == NULL)
			{
				tigString->nextStack = NULL;
			}
			// Add the last added TIGString to the new tigString's ->nextStack
			else
			{
				tigString->nextStack = theNumberStack;
			}
			
			tigString->stackNumber = stackNumber;
			tigString->stackString = NULL;
			
			// This adds the tigString to the global TIGString stack
			theNumberStack = tigString;
		}
	}
	else
	{
		tigString->nextStack = NULL;
		
		tigString->stackString = NULL;
		tigString->stackNumber = -2;
	}
	
	tigString->nextLevel = NULL;
	tigString->thisLevel = NULL;
	
	tigString->number = 0.0;
	
	// Sets the TIGObject's string to an empty string
	tigString->string = NULL;
	
	// object type
	tigString->type = "String";
	
	return tigString;
}

TIGValue *TIGStringDestroy(TIGValue *tigString)
{
	// If the "tigString" pointer has already been used free it
	if (tigString != NULL)
	{		
		if (strcmp(tigString->type, "String") != 0)
		{
#ifdef TIG_DEBUG
			printf("ERROR Function:TIGStringDestroy() Variable:tigString->type Equals:%s Valid:\"String\"\n", tigString->type);
	#ifdef TIG_DEBUG_ASSERT
		assert(0);
	#endif
#endif
			return tigString;
		}

		if (tigString->string != NULL)
		{
			free(tigString->string);
			tigString->string = NULL;
		}
		
		if (tigString->stackString != NULL)
		{
			free(tigString->stackString);
			tigString->stackString = NULL;
		}

		tigString->number = 0.0;
		tigString->stackNumber = 0;
		
		tigString->type = NULL;
		
		tigString->nextStack = NULL;
		tigString->nextLevel = NULL;
		tigString->thisLevel = NULL;
		
		free(tigString);
		tigString = NULL;
	}
	
	return tigString;
}

TIGValue *TIGStr(const char *string)
{
	return TIGStringInput(NULL, string);
}

TIGValue *TIGStringInput(TIGValue *tigString, const char *string)
{
	return TIGStringStackInput(tigString, string, TIGYes);
}

TIGValue *TIGStringStackInput(TIGValue *tigString, const char *string, TIGBool useStack)
{
	if (tigString == NULL)
	{
		tigString = TIGStringCreate(tigString, useStack);
		
		if (tigString == NULL)
		{
#ifdef TIG_DEBUG
			printf("ERROR Function:TIGStringStackInput() Variable:tigString Equals:NULL\n");

	#ifdef TIG_DEBUG_ASSERT
		assert(0);
	#endif
#endif
			return NULL;
		}
	}
	else if (strcmp(tigString->type, "String") != 0)
	{
#ifdef TIG_DEBUG
		printf("ERROR Function:TIGStringStackInput() Variable:tigString->type Equals:%s Valid:\"String\"\n", tigString->type);

	#ifdef TIG_DEBUG_ASSERT
		assert(0);
	#endif
#endif
		return NULL;
	}
	
	// If there is already a string free it
	if (tigString->string != NULL)
	{
		free(tigString->string);
		tigString->string = NULL;
	}
	
	tigString->string = (char *)malloc((strlen(string) + 1) * sizeof(char));

	if (tigString->string == NULL || string == NULL)
	{
#ifdef TIG_DEBUG
		if (string == NULL)
		{
			printf("ERROR Function:TIGStringStackInput() Variable:string Equals:NULL\n");
		}
		
		if (tigString->string == NULL)
		{
			printf("ERROR Function:TIGStringStackInput() Variable:tigString->string Equals:NULL\n");
		}

	#ifdef TIG_DEBUG_ASSERT
		assert(0);
	#endif
#endif
		return NULL;
	}
	else
	{
		strcpy(tigString->string, string);
	}
	
	return tigString;
}

char *TIG