/*
 * Copyright 2013-2016 Emmanuel Engelhart <kelson@kiwix.org>
 * Copyright 2016 Matthieu Gautier <mgautier@kymeria.fr>
 *
 * This program 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
 * any later version.
 *
 * This program 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

#include "tools.h"
#include "../tools.h"

#include <string.h>
#include <sstream>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <regex>
#include <map>

#include <zlib.h>
#include <magic.h>

static const std::map<std::string, std::string> extMimeTypes = {
    {"html",       "text/html"},
    {"htm",        "text/html"},
    {"png",        "image/png"},
    {"tiff",       "image/tiff"},
    {"tif",        "image/tiff"},
    {"jpeg",       "image/jpeg"},
    {"jpg",        "image/jpeg"},
    {"gif",        "image/gif"},
    {"svg",        "image/svg+xml"},
    {"txt",        "text/plain"},
    {"xml",        "text/xml"},
    {"epub",       "application/epub+zip"},
    {"pdf",        "application/pdf"},
    {"ogg",        "audio/ogg"},
    {"ogv",        "video/ogg"},
    {"js",         "application/javascript"},
    {"json",       "application/json"},
    {"css",        "text/css"},
    {"otf",        "font/otf"},
    {"sfnt",       "font/sfnt"},
    {"eot",        "application/vnd.ms-fontobject"},
    {"ttf",        "font/ttf"},
    {"collection", "font/collection"},
    {"woff",       "font/woff"},
    {"woff2",      "font/woff2"},
    {"vtt",        "text/vtt"},
    {"webm",       "video/webm"},
    {"webp",       "image/webp"},
    {"mp4",        "video/mp4"},
    {"doc",        "application/msword"},
    {"docx",       "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
    {"ppt",        "application/vnd.ms-powerpoint"},
    {"odt",        "application/vnd.oasis.opendocument.text"},
    {"odp",        "application/vnd.oasis.opendocument.text"},
    {"zip",        "application/zip"},
    {"wasm",       "application/wasm"}
};

static std::map<std::string, std::string> fileMimeTypes;

extern bool inflateHtmlFlag;

extern magic_t magic;

/* Decompress an STL string using zlib and return the original data. */
inline std::string inflateString(const std::string& str)
{
  z_stream zs;  // z_stream is zlib's control structure
  memset(&zs, 0, sizeof(zs));

  if (inflateInit(&zs) != Z_OK)
    throw(std::runtime_error("inflateInit failed while decompressing."));

  zs.next_in = (Bytef*)str.data();
  zs.avail_in = str.size();

  int ret;
  char outbuffer[32768];
  std::string outstring;

  // get the decompressed bytes blockwise using repeated calls to inflate
  do {
    zs.next_out = reinterpret_cast<Bytef*>(outbuffer);
    zs.avail_out = sizeof(outbuffer);

    ret = inflate(&zs, 0);

    if (outstring.size() < zs.total_out) {
      outstring.append(outbuffer, zs.total_out - outstring.size());
    }
  } while (ret == Z_OK);

  inflateEnd(&zs);

  if (ret != Z_STREAM_END) {  // an error occurred that was not EOF
    std::ostringstream oss;
    oss << "Exception during zlib decompression: (" << ret << ") " << zs.msg;
    throw(std::runtime_error(oss.str()));
  }

  return outstring;
}

std::string getMimeTypeFromExtension(const std::string& extension)
{
  const auto it = extMimeTypes.find(asciitolower(extension));
  return it != extMimeTypes.end() ?  it->second : "";
}

inline bool seemsToBeHtml(const std::string& path)
{
  return getMimeTypeFromExtension(getFileExtension(path)) == "text/html";
}

std::string getFileContent(const std::string& path)
{
  std::ifstream in(path, std::ios::binary| std::ios::ate);
  if (in) {
    std::string contents;
    contents.resize(in.tellg());
    in.seekg(0, std::ios::beg);
    in.read(&contents[0], contents.size());
    in.close();

    /* Inflate if necessary */
    if (inflateHtmlFlag && seemsToBeHtml(path)) {
      try {
        contents = inflateString(contents);
      } catch (...) {
        std::cerr << "Can not initialize inflate stream for: " << path
                  << std::endl;
      }
    }
    return (contents);
  }
  std::cerr << "zimwriterfs: unable to open file at path: " << path
            << std::endl;
  throw(errno);
}

std::string extractRedirectUrlFromHtmlHeadValue(const std::string& refresh_value)
{
  std::string url;
  static const std::regex refresh_regex(R"(^\s*([0-9.]+)\s*(?:[;,]\s*(?:url *=)?\s*(["']?)(.*?)\2)?\s*$)", std::regex::icase);

  std::smatch match;
  if (std::regex_match(refresh_value, match, refresh_regex)) {
    if (match[3].matched && !match[3].str().empty()) {
      url = match[3].str();
    }
  }

  return url;
}

std::string extractRedirectUrlFromHtml(const GumboVector* head_children)
{
  std::string url;

  for (unsigned int i = 0; i < head_children->length; ++i) {
    GumboNode* child = (GumboNode*)(head_children->data[i]);
    if (child->type == GUMBO_NODE_ELEMENT
        && child->v.element.tag == GUMBO_TAG_META) {
      GumboAttribute* attribute;
      if ((attribute
           = gumbo_get_attribute(&child->v.element.attributes, "http-equiv"))
          != NULL) {
        if (!strcmp(attribute->value, "refresh")) {
          if ((attribute
               = gumbo_get_attribute(&child->v.element.attributes, "content"))
              != NULL) {
            url = extractRedirectUrlFromHtmlHeadValue(attribute->value);
            if (url.empty()) {
              throw std::runtime_error(
                "Unable to find the redirect/refresh target URL in the HTML DOM");
            }
          }
        }
      }
    }
  }

  return url;
}

std::string generateDate()
{
  time_t t = time(0);
  struct tm* now = localtime(&t);
  std::stringstream stream;
  stream << (now->tm_year + 1900) << '-' << std::setw(2) << std::setfill('0')
         << (now->tm_mon + 1) << '-' << std::setw(2) << std::setfill('0')
         << now->tm_mday;
  return stream.str();
}


std::string getMimeTypeForFile(const std::string &directoryPath, const std::string& filename)
{
  /* Try to get the mimeType from the file extension */
  std::string mimeType = getMimeTypeFromExtension(getFileExtension(filename));
  if ( !mimeType.empty() ) {
    return mimeType;
  }

  /* Try to get the mimeType from the cache */
  try {
    return fileMimeTypes.at(filename);
  } catch (std::out_of_range&) {}

  /* Try to get the mimeType with libmagic */
  try {
    std::string path = directoryPath + "/" + filename;
    mimeType = std::string(magic_file(magic, path.c_str()));
    if (mimeType.find(";") != std::string::npos) {
      mimeType = mimeType.substr(0, mimeType.find(";"));
    }
    fileMimeTypes[filename] = mimeType;
  } catch (...) { }
  if (mimeType.empty()) {
    return "application/octet-stream";
  } else {
    return mimeType;
  }
}
