#include "tools.h"

#include <QDir>

void Tools::printTime()
{
    qDebug() << QTime::currentTime().toString("hh:mm:ss.zzz");
}

QString Tools::createLocalPath(QString path)
{
    qint8 pos = path.indexOf(applicationFolderName);

    QString localPath = path.remove(0,--pos);

    //qDebug() << "Local path: " << localPath;
    return localPath;
}

QString Tools::createRootPath(QString path)
{
    return QDir::currentPath() + path;
}

QString Tools::createSharedPath(QString path){
    return QDir::currentPath()+ "/" + sharedDataFolderName + path;
}

QString Tools::createRealPath(QString path,StreamingVersionData* currentVersionData)
{
    QString resultPath;
    int index = path.indexOf(currentVersionData->getViewName());

    if(index != -1)
    {
        resultPath = path.remove(0,index + currentVersionData->getViewName().length());
        resultPath.prepend(buildDataPath + streamingAssetsFolderName);
    }
    else
    {
        resultPath = Tools::createRootPath(path);
    }

    return resultPath;
}

QString Tools::createStreamingToRealPath(QString path,StreamingVersionData* streamingVersionData)
{
    QString resultPath = path;
    int index = path.indexOf(streamingAssetsFolderName);

    if(index != -1)
    {
        resultPath = path.remove(0,index + streamingAssetsFolderName.length());
        resultPath.prepend(QDir::currentPath() + "/" + sharedDataFolderName + "/" + streamingVersionData->getViewName());
    }

    return resultPath;


}

QString Tools::createVersionHashFilepath(QString fileName)
{
    return staticDataFolderName + "/" + fileName + "Hash.xml";
}

QString Tools::createUpdateFilePath(QString path)
{
    //qDebug() << "Full path: " << path;
    qint8 pos = path.indexOf(streamingAssetsFolderName);

    QString localPath = path.remove(0,--pos);

    //qDebug() << "Local path: " << localPath;
    return localPath;
}

QString Tools::createFolderPath(QString path)
{
    quint8 pos = path.lastIndexOf("\\");

    QString folderPath = path.remove(pos,path.length() - pos);

    return folderPath;
}

QString Tools::createFullPath(QString path)
{
    QString fullPath;
    qint8 pos = path.indexOf("Application");

    QString localPath = path.remove(0,--pos);

    //qDebug() << "CLIENT: localPath" << localPath;
    fullPath = QDir::currentPath() + localPath;

    return fullPath;
}

bool Tools::checkNonStaticData(QString path)
{
    if(path.contains(sharedDataFolderName)) return true;

    return false;
}



bool Tools::loadXMLtoDOM(QString pathNameFile, QDomDocument *commonDOM)
{
    QFile xmlInFile(pathNameFile);

    if (! xmlInFile.open(QFile::ReadOnly | QFile::Text)) {
        qDebug() << "loadXMLtoDOM: Couldn't read the file: " + pathNameFile;
        return false;
    }

    commonDOM->setContent(xmlInFile.readAll());
    xmlInFile.close();

    return true;
}

bool Tools::saveDOMtoXML(QString pathNameFile, QDomDocument *commonDOM)
{
    QFile xmlOutFile(pathNameFile);
    if (!xmlOutFile.open(QFile::WriteOnly | QFile::Text))
    {
        qDebug() << "saveDOMtoXML: Failed to write a file: " + pathNameFile;
        return false;
    }
    else
    {
        QTextStream outFile(&xmlOutFile);
        commonDOM->save(outFile, 4);
        xmlOutFile.close();
    }
    return true;
}
