//    This file is part of Motion Watch.

//    Motion Watch is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.

//    Motion Watch is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.

#include "logger.h"

void recLog(const QString &line, shared_t *share)
{
    share->logMutex.lock();

    share->recLog += QDateTime::currentDateTime().toString("[yyyy-MM-dd-hh-mm-ss] ") + line + "<br>\n";

    share->logMutex.unlock();
}

void detLog(const QString &line, shared_t *share)
{
    share->logMutex.lock();

    share->detLog += QDateTime::currentDateTime().toString("[yyyy-MM-dd-hh-mm-ss] ") + line + "<br>\n";

    share->logMutex.unlock();
}

void enforceMaxLogSize(const QString &filePath, shared_t *share)
{
    QFile file(filePath);

    if (file.exists())
    {
        if (file.size() >= share->maxLogSize)
        {
            file.remove();
        }
    }
}

void dumpLogs(const QString &fileName, const QString &lines)
{
    if (!lines.isEmpty())
    {
        QFile outFile(fileName);

        if (outFile.exists())
        {
            outFile.open(QFile::Append);
        }
        else
        {
            outFile.open(QFile::WriteOnly);
        }

        outFile.write(lines.toUtf8());
        outFile.close();
    }
}

void initLogFrontPage(const QString &filePath, const QString &logLinesFile)
{
    if (!QFile::exists(filePath))
    {
        QString htmlText = "<!DOCTYPE html>\n";

        htmlText += "<html>\n";
        htmlText += "<script>\n";
        htmlText += "function includeHTML() {\n";
        htmlText += "  var z, i, elmnt, file, xhttp;\n";
        htmlText += "  z = document.getElementsByTagName(\"*\");\n";
        htmlText += "  for (i = 0; i < z.length; i++) {\n";
        htmlText += "    elmnt = z[i];\n";
        htmlText += "    file = elmnt.getAttribute(\"include-html\");\n";
        htmlText += "    if (file) {\n";
        htmlText += "      xhttp = new XMLHttpRequest();\n";
        htmlText += "      xhttp.onreadystatechange = function() {\n";
        htmlText += "        if (this.readyState == 4) {\n";
        htmlText += "          if (this.status == 200) {elmnt.innerHTML = this.responseText;}\n";
        htmlText += "          if (this.status == 404) {elmnt.innerHTML = \"Page not found.\";}\n";
        htmlText += "          elmnt.removeAttribute(\"include-html\");\n";
        htmlText += "          includeHTML();\n";
        htmlText += "        }\n";
        htmlText += "      }\n";
        htmlText += "      xhttp.open(\"GET\", file, true);\n";
        htmlText += "      xhttp.send();\n";
        htmlText += "      return;\n";
        htmlText += "    }\n";
        htmlText += "  }\n";
        htmlText += "};\n";
        htmlText += "</script>\n";
        htmlText += "<head>\n";
        htmlText += "<meta http-equiv=\"Cache-Control\" content=\"no-cache, no-store, must-revalidate\" />\n";
        htmlText += "<meta http-equiv=\"Pragma\" content=\"no-cache\" />\n";
        htmlText += "<meta http-equiv=\"Expires\" content=\"0\" />\n";
        htmlText += "<link rel='stylesheet' href='/theme.css'>\n";
        htmlText += "</head>\n";
        htmlText += "<body>\n";
        htmlText += "<p>\n";
        htmlText += "<div include-html='" + logLinesFile + "'></div>\n";
        htmlText += "<script>\n";
        htmlText += "includeHTML();\n";
        htmlText += "</script>\n";
        htmlText += "</p>\n";
        htmlText += "</body>\n";
        htmlText += "</html>\n";

        QFile outFile(filePath);

        outFile.open(QFile::WriteOnly);
        outFile.write(htmlText.toUtf8());
        outFile.close();
    }
}

void initLogFrontPages()
{
    initLogFrontPage("logs/recording_log.html", REC_LOG_NAME);
    initLogFrontPage("logs/detection_log.html", DET_LOG_NAME);
}
