/*
 * console_handler.c
 *
 *  Created on: Mar 11, 2019
 *      Author: nxf47030
 */

#include "console_handler.h"
#include "datawork.h"


//variables
console_handler_t g_consoleHandler;

extern char g_holdk;
extern char sendFlag;
static char finalString[CONSOLE_LINE_LENGTH];
/*use this string to communicate with target devices*/
static char singleCommand[CONSOLE_LINE_LENGTH];

static char commandExecuted = 1;

static int singleCommandLenght = 0;

static int columnCount = 0;

static int collumnFinish = 0;

static char address = 0;
//functions

extern void callKeyboardAction(void);

static void loadFinalString()
{
	for(int i = 0; i<CONSOLE_LINE_LENGTH;i++)
	{
		finalString[i] = g_consoleHandler.consoleLine[i];
		g_consoleHandler.consoleLine[i] = '\0';
	}
}
static void appendChar(char *astr,char c)
{
	for(int i = 0;;i++)
	{
		if(astr[i] == '\0')
		{
			astr[i] = c;
		}
	}
}

/*add your callbacks by addresses to this function*/
static void commandCallbacks()
{
	int i = 0;
	switch (address) {
		case '0'://callback for address0
			if (!sendFlag)
			{
				g_holdk = 1;
				//user code point
				getSData(g_StrBuf);
				g_holdk = 0;
				sendFlag = 1;
				callKeyboardAction();
			}

			//appendChar(g_StrBuf,'a');
			break;
		case '1'://callback for address0
			if (!sendFlag)
			{
				g_holdk = 1;
				//user code point
				getSData(g_StrBuf);
				g_holdk = 0;
				sendFlag = 1;
				callKeyboardAction();
			}

			//appendChar(g_StrBuf,'b');
			break;
		default:
			break;
	}
}
/*Called when row of values is complete*/
static void rowComplete()
{

	if (!sendFlag)
	{
		g_holdk = 1;
		g_StrBuf[0] = '\n';
//		for(int i = 1;i<columnCount+2;i++)
//		{
//			if (i<columnCount)
//			{
//				g_StrBuf[i] = 150;//code for left key (implemented in key_codes.c)
//			}
//			else
//			{
//				g_StrBuf[i] = '\0';
//			}
//		}

		collumnFinish = 0;
		g_holdk = 0;


		sendFlag = 1;
	}


//	appendChar(g_StrBuf,'\n');
//	for(int i = 0;i < columnCount;i++)
//	{
//		appendChar(g_StrBuf, 150);
//	}
//	columnCount = 0;
}

void checkCommand()
{

	if (g_StrBuf[0] == '\0' && commandExecuted == 0)
	{
		executeCommand();
	}
	if(collumnFinish)
	{
		rowComplete();
	}
}


void callCommand()
{
	commandExecuted = 0;
	executeCommand();
}

void executeCommand()
{
	static int j = 0;
	//find first '$' sign
	int firstDollar;
	if (!j) {
		firstDollar = 0;
		columnCount = 0;
		for(int i = 0;i<CONSOLE_LINE_LENGTH;i++)
		{
			if(finalString[i] == '$')
			{
				firstDollar = i;
				address = finalString[i+1];
				//columnCount++;
				j = firstDollar+2;
				break;
			}
		}
		//count columns
		for(int i = 0;i<CONSOLE_LINE_LENGTH;i++)
				{
					if(finalString[i] == '$')
					{
						columnCount++;
					}
					if(finalString[i] == '\0')
					{
						break;
					}
				}
	}
	//start after first '$'
	for(;j<CONSOLE_LINE_LENGTH;j++)
	{

		if(j>0 && finalString[j-1] == '$')//get next command address
		{
			address = finalString[j];
			singleCommandLenght = 0;
			//columnCount++;
		}
		if(finalString[j] == '$' || finalString[j] == '\0')//single command is fully copied
		{
			commandCallbacks();
			j++;

			if(finalString[j] == '\0')//end of line
			{
				collumnFinish = 1;
				rowComplete();
				commandExecuted = 1;
				j = 0;
			}
			break;
		}
		else
		{
			//copy char
			singleCommand[singleCommandLenght++] = finalString[j];
		}


	}
}

int32_t resolveChar(int32_t i)
{
	switch(g_consoleHandler.inputStr[i])
	{
		case '$': //address sign

          break;
        case '\r': //enter key
          //s_recvSize++;
        	g_consoleHandler.inputStr[i+1] = '\n';
        	loadFinalString();
          break;
        default:
          break;
    }
	if(g_consoleHandler.inputStr[i] == '\e')
    {
		switch(g_consoleHandler.inputStr[i+2])
        {
        	case 68://left arrow code
//				if (g_consoleLineCurPos>0) {
//				g_consoleLineCurPos--;
//				}
                //break;
            case 67://right arrow code
//				if (g_consoleLineCurPos<g_consoleLineLength) {
//				g_consoleLineCurPos++;
//				}
//				else
//				{
//				s_currSendBuf[i] = '\0';
//				i+=2;
//				}
                //break;
            case 65:
            case 66://up and down arrows
                i+=3;
                break;
            default:
                break;
         }
         //escapedPos = i+3;
    }
    return i;
}

void deleteChar()
{
	int j = g_consoleHandler.CurPos - 1;
	for(;j<g_consoleHandler.Length;j++)
	{
		g_consoleHandler.consoleLine[j] = g_consoleHandler.consoleLine[j+1];
	    //rewrite characters behind the removed one
		//s_currSendBuf[s_sendSize++] = g_consoleLine[j];
	}
	j-=g_consoleHandler.CurPos-1;
	//clear remaining last character from console
	//s_currSendBuf[s_sendSize++] = ' ';
	g_consoleHandler.CurPos--;
	g_consoleHandler.Length--;
	//reset cursor position after rewriting
	/*
	for(;j>0;j--)
	{
		s_currSendBuf[s_sendSize++] = 27;
	    s_currSendBuf[s_sendSize++] = 91;
	    s_currSendBuf[s_sendSize++] = 68;
	}
	*/
}

void addChar(int32_t i)
{
	//shift caharters to make space at cursor position

	/*for(int j = g_consoleHandler.Length;j>=(int)g_consoleHandler.CurPos;j--)
	{
		_consoleHandler[j+1] = g_consoleLine[j];
	}
	*/
	//add character
	g_consoleHandler.consoleLine[g_consoleHandler.CurPos] = g_consoleHandler.inputStr[i];
	g_consoleHandler.CurPos++;
	g_consoleHandler.Length++;
	//update console output
	/*
	for(int j = g_consoleLineCurPos;j< g_consoleLineLength;j++)
	{
		s_currSendBuf[s_sendSize++] = g_consoleLine[j];
	}
	//reset cursor position on console window
	for(int j = g_consoleLineLength - g_consoleLineCurPos;j>0;j--)
	{
		s_currSendBuf[s_sendSize++] = 27;
		s_currSendBuf[s_sendSize++] = 91;
		s_currSendBuf[s_sendSize++] = 68;
	}
	*/
}

