#ifndef SUPPORT_H
#define SUPPORT_H

//------------------------------------------------------
// Includes
//------------------------------------------------------
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <sstream>
#include "Constants.h"

using namespace std;

////------------------------------------------------------
//// Log Library
////------------------------------------------------------
void logToFile(string aLogType, string aClassName, string aLogMessage)
{
    FILE* mLogFile;
    string filePath;
    bool isLogToFileEnabled;

    isLogToFileEnabled = true;

    if(isLogToFileEnabled == true)
    {
        filePath = "./" + aClassName + "." + aLogType + ".log.txt";

        mLogFile = 0;

        // Open log file to append
        if(mLogFile == 0)
        {
            mLogFile = fopen(filePath.c_str(), "at");

            if(!mLogFile)
            {
                mLogFile = fopen(filePath.c_str(), "wt");
            }

            if(!mLogFile)
            {
                // Generate error
                string aErrorDescription = "Cannot open log file: " + filePath + "\n";

                cout << "Error: ";
                cout << setw(30) << left << "<Logger>";
                cout << "Error Description: " << aErrorDescription << endl;
                return;
            }
        }

        // Print log
        fprintf(mLogFile, aLogMessage.c_str());

        // Close log file
        fclose(mLogFile);
    }
}

////------------------------------------------------------
//// String Library
////------------------------------------------------------
string adjustStringLenght(string aStr, unsigned int aLength)
{
   string adjustedString;
   
   adjustedString = "";
   string fillChar = CONST_FILL_CHAR;
   
   if(aStr.length() == aLength)
   {
      adjustedString = aStr;
   }
   else if(aStr.length() > aLength)
   {
	   adjustedString = aStr.substr(0, aLength);
   }
   else if(aStr.length() < aLength)
   {
	   for(unsigned int i = 0; i < aLength - aStr.length(); i++)
	   {
		   adjustedString.append(fillChar);
	   }
	   
	   adjustedString.append(aStr);
   }
   
   return adjustedString;
}

#endif // SUPPORT_H
