/*
 * hashmap.c
 *
 *  Created on: 2019 ~4  27  
 *      Author: foluswen
 */
#include "Module_OcppBackend.h"
#undef FALSE
#undef TRUE
typedef enum boolean { FALSE, TRUE } BOOL;
static pthread_mutex_t mutexMap = PTHREAD_MUTEX_INITIALIZER;

int MessageSent_add(char *uuid, char *data)
{
	int result = PASS;
	FILE *outfile;
	char rmFileCmd[100]={0};
	char tempstring[100]={0};
	struct stat stats;

	stat("/Storage/OCPP_PH", &stats);
	if (S_ISDIR(stats.st_mode) == 1)
	{}
	else
	{
		DEBUG_INFO("/Storage/OCPP_PH directory not exist, create dir \n");
		sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP_PH");
		system(rmFileCmd);
	}

	memset(&rmFileCmd, 0, sizeof rmFileCmd);
	if((access("/Storage/OCPP_PH/MessageSent",F_OK)) != -1)
	{}
	else
	{
		DEBUG_INFO("/Storage/OCPP_PH/MessageSent not exist\n");
		FILE *log = fopen("/Storage/OCPP_PH/MessageSent", "w+");

		if(log == NULL)
		{
			DEBUG_ERROR("Can't Create File /Storage/OCPP_PH/MessageSent \n");
			result = FAIL;
		}
		else
		{
			fclose(log);
		}
	}

	// open file for writing
	if(result != FAIL)
	{
		outfile = fopen ("/Storage/OCPP_PH/MessageSent", "a");
		sprintf(tempstring,"%s,%s\n", uuid,data);
		fputs(tempstring, outfile);
		fclose (outfile);
	}

	return result;
}

int MessageSent_get(char *uuid, char *data)
{
	int result = PASS;
	FILE *fp;
	char str[1200]={0};
	char sstr[50]={ 0 }, datastr[30]={0};
	int c = 0;
	char *loc;
	char rmFileCmd[100]={0};
	struct stat stats;
	stat("/Storage/OCPP_PH", &stats);

	// Check for directory existence
	if (S_ISDIR(stats.st_mode) == 1)
	{}
	else
	{
		DEBUG_INFO("/Storage/OCPP_PH directory not exist, create dir \n");
		sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP_PH");
		system(rmFileCmd);
	}

	memset(&rmFileCmd, 0, sizeof rmFileCmd);

	if((access("/Storage/OCPP_PH/MessageSent",F_OK)) != -1)
	{}
	else
	{
		DEBUG_INFO("/Storage/OCPP_PH/MessageSent not exist\n");
		FILE *log = fopen("/Storage/OCPP_PH/MessageSent", "w+");

		if(log == NULL)
		{
			DEBUG_ERROR("Can't Create File /Storage/OCPP_PH/MessageSent \n");
			result = FAIL;
		}
		else
		{
			fclose(log);
		}
	}

	if(result != FAIL)
	{
		/* opening file for reading */
		fp = fopen("/Storage/OCPP_PH/MessageSent" , "r");
		if(fp == NULL)
		{
			DEBUG_ERROR("Error opening /Storage/OCPP_PH/MessageSent\n");
			result = FAIL;
		}

		c = fgetc(fp);
		rewind(fp);
		if(c == EOF)
		{
			DEBUG_INFO("/Storage/OCPP_PH/MessageSent is null\n");
			strcpy(data,"");
			result = FAIL;
		}
		else
		{
			result = FAIL;
			while (fgets (str, 1200, fp)!=NULL)
			{
				str[strlen(str) - 1] = '\0';

				/*********************uuid***************/
				int d = 0;
				while (str[d] != ',')
				{
					sstr[d] = str[d];
					d=d+ 1;
				}
				sstr[d] = '\0';

				if(strcmp(sstr, uuid) == 0)
				{
					loc = strstr(str, ",");
					memset(sstr ,0, sizeof(sstr) );
					int e = 0;
					while (loc[1+e] != '\0')
					{
						datastr[e] = loc[1+e];
						e++;
					}
					datastr[e] = '\0';
					strcpy(data,datastr);
					result = PASS;
					break;
				}
			}
		}
		fclose(fp);
	}

	return result;
}

int MessageSent_remove(char *uuid, char *data)
{
	int result = PASS;
	char tempfile[] = "/Storage/OCPP_PH/temp1.json";
	FILE *infile;
	FILE *outfile;
	int  resultRename=0;
	char filename[60]={0};
	char rmFileCmd[100]={0};
	char tempstring[100]={0};
	struct stat stats;
	stat("/Storage/OCPP_PH", &stats);

	// Check for directory existence
	if (S_ISDIR(stats.st_mode) == 1)
	{}
	else
	{
		DEBUG_INFO("/Storage/OCPP_PH directory not exist, create dir \n");
		sprintf(rmFileCmd,"mkdir -p %s","/Storage/OCPP_PH");
		system(rmFileCmd);
	}

	memset(&rmFileCmd, 0, sizeof rmFileCmd);

	if((access("/Storage/OCPP_PH/MessageSent",F_OK)) != -1)
	{}
	else
	{
		DEBUG_INFO("/Storage/OCPP_PH/MessageSent not exist\n");
		FILE *log = fopen("/Storage/OCPP_PH/MessageSent", "w+");

		if(log == NULL)
		{
			DEBUG_INFO("/Storage/OCPP_PH/MessageSent is NULL\n");
			result = FAIL;
		}
		else
		{
			fclose(log);
		}
	}

	if(result != FAIL)
	{
		sprintf(tempstring,"%s,%s", uuid,data);

		// open file for writing
		strcpy(filename, "/Storage/OCPP_PH/MessageSent");
		infile = fopen ("/Storage/OCPP_PH/MessageSent", "r");
		outfile = fopen (tempfile, "w");

		int c;
		c = fgetc(infile);
		rewind(infile);

		if(c == EOF)
		{
			DEBUG_INFO("/Storage/OCPP_PH/MessageSent is  NULL\n");

			fclose(infile);
			fclose(outfile);

			sprintf(rmFileCmd,"rm -f %s",tempfile);
			system(rmFileCmd);
		}
		else
		{
			char buf[1200]={0};

			while (fgets(buf, sizeof(buf), infile) != NULL)
			{
				buf[strlen(buf) - 1] = '\0';

				if(strcmp(tempstring, buf)== 0)
				{}
				else
				{
					fprintf(outfile,"%s\n", buf);
				}
			}

			fclose(infile);
			fclose(outfile);

			sprintf(rmFileCmd,"rm -f %s",filename);
			system(rmFileCmd);

			resultRename = rename(tempfile, filename);

			if(resultRename == 0)
			{}
			else
			{
				DEBUG_ERROR("Error: unable to rename the file");
				result = FAIL;
			}
		}
	}

	return result;
}

int hashmap_operation(int type, char *uuid, char *data)
{
	pthread_mutex_lock(&mutexMap);
	int result=0;
	if(type == HASH_OP_ADD)
		result = MessageSent_add(uuid, data);
	else if(type  == HASH_OP_GET)
		result = MessageSent_get(uuid,data);
	else if(type == HASH_OP_REMOVE)
		result = MessageSent_remove(uuid, data);
	pthread_mutex_unlock(&mutexMap);

	return result;
}