//    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 "web.h"

void genHTMLul(const QString &outputDir, const QString &title, shared_t *share)
{
    QStringList logNames;
    QStringList eveNames;
    QStringList dirNames;

    QString htmlText = "<!DOCTYPE html>\n";

    htmlText += "<html>\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 += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n";
    htmlText += "<link rel='stylesheet' href='/theme.css'>\n";
    htmlText += "</head>\n";
    htmlText += "<body>\n";
    htmlText += "<h3>" + title + "</h3>\n";

    if (QDir().exists(outputDir + "/live"))
    {
        eveNames = lsFilesInDir(outputDir + "/events", ".html");
        logNames = lsFilesInDir(outputDir + "/logs", "_log.html");

        htmlText += "<h4>Logs</h4>\n";
        htmlText += "<ul>\n";

        for (auto &&logName : logNames)
        {
            auto name = logName;

            name.remove("_log.html");

            htmlText += "  <li><a href='logs/" + logName + "'>" + name + "</a></li>\n";
        }

        htmlText += "</ul>\n";
        htmlText += "<h4>Live</h4>\n";
        htmlText += "<ul>\n";
        htmlText += "  <li><a href='stream.html'>" + share->camName + ":live" + "</a></li>\n";
        htmlText += "</ul>\n";
        htmlText += "<h4>Motion Events</h4>\n";

        genHTMLstream("stream");

        for (auto &&eveName : eveNames)
        {
            auto name = eveName;

            name.remove(".html");

            htmlText += "<a href='events/" + eveName + "'><img src='events/" + name + ".jpg" + "' style='width:25%;height:25%;'</a>\n";
        }
    }
    else
    {
        dirNames = lsDirsInDir(outputDir);

        htmlText += "<ul>\n";

        for (auto &&dirName : dirNames)
        {
            htmlText += "  <li><a href='" + dirName + "/index.html'>" + dirName + "</a></li>\n";
        }

        htmlText += "</ul>\n";
    }

    htmlText += "</body>\n";
    htmlText += "</html>";

    QFile outFile(QDir().cleanPath(outputDir) + "/index.html");

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

void genHTMLstream(const QString &name)
{
    QString htmlText = "<!DOCTYPE html>\n";

    htmlText += "<html>\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 += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n";
    htmlText += "<link rel='stylesheet' href='/theme.css'>\n";
    htmlText += "</head>\n";
    htmlText += "<body>\n";
    htmlText += "  <script src=\"https://cdn.jsdelivr.net/npm/hls.js@1\">\n";
    htmlText += "  </script>\n";
    htmlText += "  <video width=100% height=100% id=\"video\" controls>\n";
    htmlText += "  </video>\n";
    htmlText += "  <script>\n";
    htmlText += "    var video = document.getElementById('video');\n";
    htmlText += "    if (Hls.isSupported()) {\n";
    htmlText += "      var hls = new Hls({\n";
    htmlText += "        debug: true,\n";
    htmlText += "      });\n";
    htmlText += "      hls.loadSource('" + name + ".m3u8');\n";
    htmlText += "      hls.attachMedia(video);\n";
    htmlText += "      hls.on(Hls.Events.MEDIA_ATTACHED, function () {\n";
    htmlText += "        video.muted = true;\n";
    htmlText += "        video.play();\n";
    htmlText += "      });\n";
    htmlText += "    }\n";
    htmlText += "    else if (video.canPlayType('application/vnd.apple.mpegurl')) {\n";
    htmlText += "      video.src = '" + name + ".m3u8';\n";
    htmlText += "      video.addEventListener('canplay', function () {\n";
    htmlText += "        video.play();\n";
    htmlText += "      });\n";
    htmlText += "    }\n";
    htmlText += "  </script>\n";
    htmlText += "</body>\n";
    htmlText += "</html>";

    QFile outFile(name + ".html");

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

void genHTMLvod(const QString &name)
{
    QString htmlText = "<!DOCTYPE html>\n";

    htmlText += "<html>\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 += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n";
    htmlText += "<link rel='stylesheet' href='/theme.css'>\n";
    htmlText += "</head>\n";
    htmlText += "<body>\n";
    htmlText += "<video width=100% height=100% controls autoplay>\n";
    htmlText += "  <source src='" + name + ".mp4' type='video/mp4'>\n";
    htmlText += "</video>\n";
    htmlText += "</body>\n";
    htmlText += "</html>";

    QFile outFile("events/" + name + ".html");

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

void genCSS(shared_t *share)
{
    QString cssText = "body {\n";

    cssText += "  background-color: " + share->webBg   + ";\n";
    cssText += "  color: "            + share->webTxt  + ";\n";
    cssText += "  font-family: "      + share->webFont + ";\n";
    cssText += "}\n";
    cssText += "a {\n";
    cssText += "  color: "            + share->webTxt + ";\n";
    cssText += "}\n";

    QFile outFile(QDir().cleanPath(share->webRoot) + "/theme.css");

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