#include "StdAfx.h"
#include "Status.h"
#include "SysTime.h"
#include <cstring>

Status::Status() :
	  m_MsgCnt				( 0, MAX_MSG_QUEUE )
	, m_MsgCntBuffer		( 0, MAX_MSG_QUEUE )
{
	memset( m_Messages, 0, sizeof( char  ) * MAX_MSG_QUEUE * MSG_SIZE );
	memset( m_bFree,    1, sizeof( bool  ) * MAX_MSG_QUEUE );
	memset( m_msgBuf,   0, sizeof( char  ) * MSG_SIZE );
	memset( m_MsgColor, 0, sizeof( UINT8 ) * MSG_SIZE );
}

Status::~Status(void)
{
}

void Status::PrintStatus( char *msg, ... )
{
	m_semData.Lock();
      
   va_list argptr;
	va_start( argptr, msg );
	if( strlen(msg) >= MSG_SIZE ) { msg[MSG_SIZE-1] = 0; }
	if(m_msgBuf) vsprintf_s( m_msgBuf, MSG_SIZE, msg, argptr );
	va_end( argptr );	
	
	PrintStatusMsg( COLOR_MSG );
	m_semData.Unlock();
}

void Status::PrintStatus( int argColor, char *msg, ... )
{
	m_semData.Lock();
   va_list argptr;
   va_start( argptr, msg );
	if( strlen(msg) >= MSG_SIZE ) { msg[MSG_SIZE-1] = 0; }

	// Format the message
   vsprintf_s( m_msgBuf, MSG_SIZE, msg, argptr );
   va_end( argptr );	

	PrintStatusMsg( argColor );
	m_semData.Unlock();
}

void Status::PrintStatusMsg( int argColor  )
{
	if( !m_bFree[m_MsgCntBuffer] ) return;  // discard - previous hasn't been printed yet

	//m_msgBuf[ MSG_SIZE-12 ] = 0; // leave room for CGUI header

	// go through buffer and look through any new lines
	//int newLineCnt = 0;
	//for( int i=0; i<(int)strlen( m_msgBuf ); i++ )
	//{
	//	if(m_msgBuf[i] == '\n' )
	//	{
	//		newLineCnt++;
	//		m_msgBuf[i] = ' ';
	//	}
	//}

	strcpy_s( m_Messages[ m_MsgCntBuffer ], MSG_SIZE, m_msgBuf );
   m_MsgColor[m_MsgCntBuffer] = (UINT8)argColor;
	m_bFree[m_MsgCntBuffer] = false;
	WriteLog( m_Messages[ m_MsgCntBuffer ] );
	
   m_MsgCntBuffer++;

   // add multiple lines, if necessary
   if( strlen(m_msgBuf) > MSG_SIZE )
   {
	   strcpy_s( m_Messages[ m_MsgCntBuffer ], MSG_SIZE, &m_msgBuf[MSG_SIZE] );
      m_MsgColor[m_MsgCntBuffer] = (UINT8)argColor;
		m_bFree[m_MsgCntBuffer] = false;
		WriteLog( m_Messages[ m_MsgCntBuffer ] );
	   m_MsgCntBuffer++;
   }


   // add new line, if necessary
	//if( newLineCnt > 0 )
	//{
	//	strcpy_s( m_Messages[ m_MsgCntBuffer ], "" );
   //   m_MsgColor[m_MsgCntBuffer] = (UINT8)argColor;
	//	m_bFree[m_MsgCntBuffer] = false;
	//	m_MsgCntBuffer++;
	//}
}

bool Status::MessageAvailable()
{
	m_semData.Lock();
	bool bRtn = m_MsgCntBuffer != m_MsgCnt;
	m_semData.Unlock();
	return bRtn;
}

bool Status::GetNextMessage( char *msg, int *pColor )
{
	m_semData.Lock();
	strcpy_s( msg, MSG_SIZE, m_Messages[ m_MsgCnt ] );
	m_bFree[m_MsgCnt] = true;
	*pColor = m_MsgColor[m_MsgCnt];
	m_MsgCnt++;
	m_semData.Unlock();
	return true;
}

COLORREF Status::GetColorRef( int argColor )
{
   switch( argColor )
   {
       case COLOR_MSG:       
         return RGB( 0, 0, 0 );
      case COLOR_WARNING:   
         return RGB( 235, 235, 0 );
      case COLOR_ERROR:     
         return RGB( 255, 0, 0 );

		default:
         return RGB( 0, 0, 0 );
	 }
}
 
void Status::WriteLog( char *entry )
{
	char logName[128];
	FILE			*f1 = NULL;
	m_SysTm.Update();

	if( strlen( entry ) > 256 ) { entry[255] = 0; }
	sprintf_s( m_strLog, 1024, "%02d%02d%04d::%02d%02d%02d: %s", (int)m_SysTm.Month(), (int)m_SysTm.Day(),    (int)m_SysTm.Year(),
		                                                          (int)m_SysTm.Hour(),  (int)m_SysTm.Minute(), (int)m_SysTm.Second(), entry );
	bool bNewLine = true;
	int len = strlen( m_strLog );
	if( len > 0 )
	{
		if( m_strLog[ len-1 ] == '\n' )
		{ bNewLine = false; }
		if( m_strLog[ len-1 ] == '\r' )
		{ bNewLine = false; }
	}

	sprintf_s( logName, 128, "C:\\3D\\LOG\\%02d-%02d-%04d_LDV_LOG.txt", (int)m_SysTm.Month(), (int)m_SysTm.Day(), (int)m_SysTm.Year() );

	fopen_s( &f1, logName, "a" );
	if( f1 )
	{
		fprintf( f1, "%s%s", m_strLog, bNewLine ? "\n" : "" );
		fclose( f1 );
	}
}
