#pragma once

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

namespace crow
{
// ----------------------------------------------------------------------------
// qs_parse (modified)
// https://github.com/bartgrantham/qs_parse
// ----------------------------------------------------------------------------
/*  Similar to strncmp, but handles URL-encoding for either string  */
int qsStrncmp(const char* s, const char* qs, size_t n);

/*  Finds the beginning of each key/value pair and stores a pointer in qs_kv.
 *  Also decodes the value portion of the k/v pair *in-place*.  In a future
 *  enhancement it will also have a compile-time option of sorting qs_kv
 *  alphabetically by key.  */
size_t qsParse(char* qs, char* qs_kv[], size_t qs_kv_size);

/*  Used by qs_parse to decode the value portion of a k/v pair  */
int qsDecode(char* qs);

/*  Looks up the value according to the key on a pre-processed query string
 *  A future enhancement will be a compile-time option to look up the key
 *  in a pre-sorted qs_kv array via a binary search.  */
// char * qs_k2v(const char * key, char * qs_kv[], int qs_kv_size);
char* qsK2v(const char* key, char* const* qs_kv, int qs_kv_size, int nth);

/*  Non-destructive lookup of value, based on key.  User provides the
 *  destinaton string and length.  */
char* qsScanvalue(const char* key, const char* qs, char* val, size_t val_len);

// TODO: implement sorting of the qs_kv array; for now ensure it's not compiled
#undef _qsSORTING

// isxdigit _is_ available in <ctype.h>, but let's avoid another header instead
#define BMCWEB_QS_ISHEX(x)                                                     \
    ((((x) >= '0' && (x) <= '9') || ((x) >= 'A' && (x) <= 'F') ||              \
      ((x) >= 'a' && (x) <= 'f'))                                              \
         ? 1                                                                   \
         : 0)
#define BMCWEB_QS_HEX2DEC(x)                                                   \
    (((x) >= '0' && (x) <= '9')                                                \
         ? (x)-48                                                              \
         : ((x) >= 'A' && (x) <= 'F')                                          \
               ? (x)-55                                                        \
               : ((x) >= 'a' && (x) <= 'f') ? (x)-87 : 0)
#define BMCWEB_QS_ISQSCHR(x)                                                   \
    ((((x) == '=') || ((x) == '#') || ((x) == '&') || ((x) == '\0')) ? 0 : 1)

inline int qsStrncmp(const char* s, const char* qs, size_t n)
{
    int i = 0;
    char u1, u2;
    char unyb, lnyb;

    while (n-- > 0)
    {
        u1 = *s++;
        u2 = *qs++;

        if (!BMCWEB_QS_ISQSCHR(u1))
        {
            u1 = '\0';
        }
        if (!BMCWEB_QS_ISQSCHR(u2))
        {
            u2 = '\0';
        }

        if (u1 == '+')
        {
            u1 = ' ';
        }
        if (u1 == '%') // easier/safer than scanf
        {
            unyb = static_cast<char>(*s++);
            lnyb = static_cast<char>(*s++);
            if (BMCWEB_QS_ISHEX(unyb) && BMCWEB_QS_ISHEX(lnyb))
            {
                u1 = static_cast<char>((BMCWEB_QS_HEX2DEC(unyb) * 16) +
                                       BMCWEB_QS_HEX2DEC(lnyb));
            }
            else
            {
                u1 = '\0';
            }
        }

        if (u2 == '+')
        {
            u2 = ' ';
        }
        if (u2 == '%') // easier/safer than scanf
        {
            unyb = static_cast<char>(*qs++);
            lnyb = static_cast<char>(*qs++);
            if (BMCWEB_QS_ISHEX(unyb) && BMCWEB_QS_ISHEX(lnyb))
            {
                u2 = static_cast<char>((BMCWEB_QS_HEX2DEC(unyb) * 16) +
                                       BMCWEB_QS_HEX2DEC(lnyb));
            }
            else
            {
                u2 = '\0';
            }
        }

        if (u1 != u2)
        {
            return u1 - u2;
        }
        if (u1 == '\0')
        {
            return 0;
        }
        i++;
    }
    if (BMCWEB_QS_ISQSCHR(*qs))
    {
        return -1;
    }
    else
    {
        return 0;
    }
}

inline size_t qsParse(char* qs, char* qs_kv[], size_t qs_kv_size)
{
    size_t i;
    size_t j;
    char* substrPtr;

    for (i = 0; i < qs_kv_size; i++)
    {
        qs_kv[i] = nullptr;
    }

    // find the beginning of the k/v substrings or the fragment
    substrPtr = qs + strcspn(qs, "?#");
    if (substrPtr[0] != '\0')
    {
        substrPtr++;
    }
    else
    {
        return 0; // no query or fragment
    }

    i = 0;
    while (i < qs_kv_size)
    {
        qs_kv[i++] = substrPtr;
        j = strcspn(substrPtr, "&");
        if (substrPtr[j] == '\0')
        {
            break;
        }
        substrPtr += j + 1;
    }

    // we only decode the values in place, the keys could have '='s in them
    // which will hose our ability to distinguish keys from values later
    for (j = 0; j < i; j++)
    {
        substrPtr = qs_kv[j] + strcspn(qs_kv[j], "=&#");
        if (substrPtr[0] == '&' || substrPtr[0] == '\0')
        { // blank value: skip decoding
            substrPtr[0] = '\0';
        }
        else
        {
            qsDecode(++substrPtr);
        }
    }

#ifdef _qsSORTING
// TODO: qsort qs_kv, using qs_strncmp() for the comparison
#endif

    return i;
}

inline int qsDecode(char* qs)
{
    int i = 0, j = 0;

    while (BMCWEB_QS_ISQSCHR(qs[j]))
    {
        if (qs[j] == '+')
        {
            qs[i] = ' ';
        }
        else if (qs[j] == '%') // easier/safer than scanf
        {
            if (!BMCWEB_QS_ISHEX(qs[j + 1]) || !BMCWEB_QS_ISHEX(qs[j + 2]))
            {
                qs[i] = '\0';
                return i;
            }
            qs[i] = static_cast<char>(BMCWEB_QS_HEX2DEC(qs[j + 1] * 16) +
                                      BMCWEB_QS_HEX2DEC(qs[j + 2]));
            j += 2;
        }
        else
        {
            qs[i] = qs[j];
        }
        i++;
        j++;
    }
    qs[i] = '\0';

    return i;
}

inline char* qsK2v(const char* key, char* const* qs_kv, int qs_kv_size,
                   int nth = 0)
{
    int i;
    size_t keyLen, skip;

    keyLen = strlen(key);

#ifdef _qsSORTING
// TODO: binary search for key in the sorted qs_kv
#else  // _qsSORTING
    for (i = 0; i < qs_kv_size; i++)
    {
        // we rely on the unambiguous '=' to find the value in our k/v pair
        if (qsStrncmp(key, qs_kv[i], keyLen) == 0)
        {
            skip = strcspn(qs_kv[i], "=");
            if (qs_kv[i][skip] == '=')
            {
                skip++;
            }
            // return (zero-char value) ? ptr to trailing '\0' : ptr to value
            if (nth == 0)
            {
                return qs_kv[i] + skip;
            }
            else
            {
                --nth;
            }
        }
    }
#endif // _qsSORTING

    return nullptr;
}

inline char* qsScanvalue(const char* key, const char* qs, char* val,
                         size_t val_len)
{
    size_t i, keyLen;
    const char* tmp;

    // find the beginning of the k/v substrings
    if ((tmp = strchr(qs, '?')) != nullptr)
    {
        qs = tmp + 1;
    }

    keyLen = strlen(key);
    while (qs[0] != '#' && qs[0] != '\0')
    {
        if (qsStrncmp(key, qs, keyLen) == 0)
        {
            break;
        }
        qs += strcspn(qs, "&") + 1;
    }

    if (qs[0] == '\0')
    {
        return nullptr;
    }

    qs += strcspn(qs, "=&#");
    if (qs[0] == '=')
    {
        qs++;
        i = strcspn(qs, "&=#");
        strncpy(val, qs, (val_len - 1) < (i + 1) ? (val_len - 1) : (i + 1));
        qsDecode(val);
    }
    else
    {
        if (val_len > 0)
        {
            val[0] = '\0';
        }
    }

    return val;
}
} // namespace crow
// ----------------------------------------------------------------------------

namespace crow
{
class QueryString
{
  public:
    static const size_t maxKeyValuePairsCount = 256;

    QueryString() = default;

    QueryString(const QueryString& qs) : url(qs.url)
    {
        for (auto p : qs.keyValuePairs)
        {
            keyValuePairs.push_back(
                const_cast<char*>(p - qs.url.c_str() + url.c_str()));
        }
    }

    QueryString& operator=(const QueryString& qs)
    {
        if (this == &qs)
        {
            return *this;
        }

        url = qs.url;
        keyValuePairs.clear();
        for (auto p : qs.keyValuePairs)
        {
            keyValuePairs.push_back(
                const_cast<char*>(p - qs.url.c_str() + url.c_str()));
        }
        return *this;
    }

    QueryString& operator=(QueryString&& qs)
    {
        keyValuePairs = std::move(qs.keyValuePairs);
        auto* oldData = const_cast<char*>(qs.url.c_str());
        url = std::move(qs.url);
        for (auto& p : keyValuePairs)
        {
            p += const_cast<char*>(url.c_str()) - oldData;
        }
        return *this;
    }

    explicit QueryString(std::string newUrl) : url(std::move(newUrl))
    {
        if (url.empty())
        {
            return;
        }

        keyValuePairs.resize(maxKeyValuePairsCount);

        size_t count =
            qsParse(&url[0], &keyValuePairs[0], maxKeyValuePairsCount);
        keyValuePairs.resize(count);
    }

    void clear()
    {
        keyValuePairs.clear();
        url.clear();
    }

    friend std::ostream& operator<<(std::ostream& os, const QueryString& qs)
    {
        os << "[ ";
        for (size_t i = 0; i < qs.keyValuePairs.size(); ++i)
        {
            if (i != 0u)
            {
                os << ", ";
            }
            os << qs.keyValuePairs[i];
        }
        os << " ]";
        return os;
    }

    char* get(const std::string& name) const
    {
        char* ret = qsK2v(name.c_str(), keyValuePairs.data(),
                          static_cast<int>(keyValuePairs.size()));
        return ret;
    }

    std::vector<char*> getList(const std::string& name) const
    {
        std::vector<char*> ret;
        std::string plus = name + "[]";
        char* element = nullptr;

        int count = 0;
        while (true)
        {
            element = qsK2v(plus.c_str(), keyValuePairs.data(),
                            static_cast<int>(keyValuePairs.size()), count++);
            if (element == nullptr)
            {
                break;
            }
            ret.push_back(element);
        }
        return ret;
    }

  private:
    std::string url;
    std::vector<char*> keyValuePairs;
};

} // namespace crow
