#include "toolsHtml.h"
#include "CApp.h"
#include <QHash>
#include <QFile>
#include <QTextStream>
#include <QDebug>

ToolsHtml::ToolsHtml(QObject * parent){}

ToolsHtml::~ToolsHtml(){}

QString ToolsHtml::replaceInHtml(const QString & text)
{
    enum HtmlCodes{NUMBERZERO = 0,NUMBER_ONE,CODE,HTML_NUMBER,CHAR,HTML_NUMERIC_CODE,HTML_CHAR_CODE,EXPLANATION};
    QHash<QString,QString> hashHtml;
    QFile htmlFile(CApp::pCApp()->pathAppli()+"Ressources/codecs/html");
    if (!htmlFile.open(QIODevice::ReadOnly))
    {
  	    qWarning() << __FILE__ << QString::number(__LINE__) << "unable to open htmlFile" ;
        }
    QTextStream streamHtml(&htmlFile);
    while (!streamHtml.atEnd())
    {
  	    QString line = streamHtml.readLine();
  	    if (line.contains("&#"))
  	{
  		QStringList listLine = line.split("_")  ;
  		hashHtml.insert(listLine[CHAR],listLine[HTML_NUMERIC_CODE]);  		
  	    }  	
      }
    QString htmlString;
    htmlString = text;
    htmlString.replace("\t"," ");
    htmlString.replace("\'","\\'");
    for (int i = 0; i < hashHtml.count(); ++i)
    {
    	  QString key = hashHtml.keys()[i];
    	  QString htmlValue = hashHtml.value(key);
    	  htmlString.replace(key,htmlValue);
        }
    htmlString.replace("\r\n","\n");
    htmlString.replace("\n","<br/>");
    return htmlString;
}

