/*
    SPDX-License-Identifier: GPL-3.0-or-later
    Copyright © 2015-2022 Amebis
*/

#pragma once

#include "common.h"

#include <assert.h>
#include <algorithm>
#include <istream>
#include <locale>
#include <map>
#include <ostream>
#include <set>
#include <string>
#include <vector>

#pragma warning(push)
#pragma warning(disable: 4200)
#pragma warning(disable: 4251)
#pragma warning(disable: 4512)


namespace ZRCola {
    ///
    /// Character rank type
    ///
    typedef double charrank_t;

    inline bool ispua(_In_ char_t c)
    {
        return u'\ue000' <= c && c <= u'\uf8ff';
    }

#ifndef _WIN32
    size_t wcslen(_In_z_ const char_t* str);
    size_t wcsnlen(_In_z_count_(count) const char_t* str, _In_ size_t count);
#endif


    ///
    /// Character block ID
    ///
    typedef uint16_t chrblkid_t;


    ///
    /// Character Database
    ///
    class character_db {
    public:
#pragma pack(push)
#pragma pack(2)
        ///
        /// Character data
        ///
        struct character {
        public:
            chrblkid_t blk;     ///> Character block ID

        protected:
            uint16_t chr_to;    ///< Character end in \c data
            uint16_t desc_to;   ///< Character description end in \c data
            uint16_t rel_to;    ///< Related characters end in \c data
            char_t data[];      ///< Character, character description

        private:
            character(_In_ const character &other);
            character& operator=(_In_ const character &other);

        public:
            ///
            /// Constructs the character
            ///
            /// \param[in] chr       Character
            /// \param[in] chr_len   Number of UTF-16 characters in \p chr
            /// \param[in] blk       Unicode block
            /// \param[in] desc      Description
            /// \param[in] desc_len  Number of UTF-16 characters in \p desc
            /// \param[in] rel       Related characters list (zero delimited)
            /// \param[in] rel_len   Number of UTF-16 characters in \p rel (including zero delimiters)
            ///
            character(
                _In_opt_z_count_(chr_len)  const char_t     *chr      = NULL,
                _In_opt_                         size_t      chr_len  = 0,
                _In_opt_                         chrblkid_t  blk      = 0,
                _In_opt_z_count_(desc_len) const char_t     *desc     = NULL,
                _In_opt_                         size_t      desc_len = 0,
                _In_opt_z_count_(rel_len)  const char_t     *rel      = NULL,
                _In_opt_                         size_t      rel_len  = 0)
            {
                this->blk = blk;
                this->chr_to = static_cast<uint16_t>(chr_len);
                if (chr && chr_len) memcpy(this->data, chr, sizeof(char_t)*chr_len);
                this->desc_to = static_cast<uint16_t>(this->chr_to + desc_len);
                if (desc && desc_len) memcpy(this->data + this->chr_to, desc, sizeof(char_t)*desc_len);
                this->rel_to = static_cast<uint16_t>(this->desc_to + rel_len);
                if (rel && rel_len) memcpy(this->data + this->desc_to, rel, sizeof(char_t)*rel_len);
            }

            const char_t*  chr    () const { return data;          };
                  char_t*  chr    ()       { return data;          };
            const char_t*  chr_end() const { return data + chr_to; };
                  char_t*  chr_end()       { return data + chr_to; };
                  uint16_t chr_len() const { return chr_to;        };

            const char_t*  desc    () const { return data + chr_to;    };
                  char_t*  desc    ()       { return data + chr_to;    };
            const char_t*  desc_end() const { return data + desc_to;   };
                  char_t*  desc_end()       { return data + desc_to;   };
                  uint16_t desc_len() const { return desc_to - chr_to; };

            const char_t*  rel    () const { return data + desc_to;   };
                  char_t*  rel    ()       { return data + desc_to;   };
            const char_t*  rel_end() const { return data + rel_to;    };
                  char_t*  rel_end()       { return data + rel_to;    };
                  uint16_t rel_len() const { return rel_to - desc_to; };
        };
#pragma pack(pop)

        ///
        /// Character index
        ///
        class indexChr : public index<uint16_t, uint32_t, character>
        {
        public:
            ///
            /// Constructs the index
            ///
            /// \param[in] h  Reference to vector holding the data
            ///
            indexChr(_In_ std::vector<uint16_t> &h) : index<uint16_t, uint32_t, character>(h) {}

            ///
            /// Compares two characters by ID (for searching)
            ///
            /// \param[in] a  Pointer to first element
            /// \param[in] b  Pointer to second element
            ///
            /// \returns
            /// - <0 when a <  b
            /// - =0 when a == b
            /// - >0 when a >  b
            ///
            virtual int compare(_In_ const character &a, _In_ const character &b) const
            {
                int r = ZRCola::CompareString(a.chr(), a.chr_len(), b.chr(), b.chr_len());
                if (r != 0) return r;

                return 0;
            }
        } idxChr;      ///< Character index

        textindex<char_t, char_t, uint32_t> idxDsc;     ///< Description index
        textindex<char_t, char_t, uint32_t> idxDscSub;  ///< Description index (sub-terms)
        std::vector<uint16_t> data;                     ///< Character data

    public:
        ///
        /// Constructs the database
        ///
        character_db() : idxChr(data) {}

        ///
        /// Clears the database
        ///
        void clear()
        {
            idxChr   .clear();
            idxDsc   .clear();
            idxDscSub.clear();
            data     .clear();
        }

        ///
        /// Search for characters by description in given blocks
        ///
        /// \param[in   ] str       Search string
        /// \param[in   ] blks      Set of blocks, character must be a part of
        /// \param[inout] hits      (character, count) map to append full-word hits to
        /// \param[inout] hits_sub  (character, count) map to append partial-word hits to
        /// \param[in   ] fn_abort  Pointer to function to periodically test for search cancellation
        /// \param[in   ] cookie    Cookie for \p fn_abort call
        ///
        bool Search(_In_z_ const char_t *str, _In_ const std::set<chrblkid_t> &blks, _Inout_ std::map<string_t, charrank_t> &hits, _Inout_ std::map<string_t, charrank_t> &hits_sub, _In_opt_ bool (__cdecl *fn_abort)(void *cookie) = NULL, _In_opt_ void *cookie = NULL) const;

        ///
        /// Get character block
        ///
        /// \param[in] chr  Character
        /// \param[in] len  Number of UTF-16 characters in \p chr
        ///
        /// \returns
        /// - Character block if character found
        /// - 0 otherwise
        ///
        chrblkid_t GetCharBlk(_In_z_count_(len) const char_t *chr, _In_ const size_t len) const
        {
            assert(len <= 0xffff);
            std::unique_ptr<character> c((character*)new char[sizeof(character) + sizeof(char_t)*len]);
            new (c.get()) character(chr, len);
            indexChr::size_type start;
            return idxChr.find(*c, start) ? idxChr[start].blk : 0;
        }

        ///
        /// Writes character database to a stream
        ///
        /// \param[in] stream  Output stream
        /// \param[in] db      Character database
        ///
        /// \returns The stream \p stream
        ///
        friend std::ostream& operator <<(_In_ std::ostream& stream, _In_ const character_db &db)
        {
            // Write character index.
            if (stream.fail()) return stream;
            stream << db.idxChr;

            // Write description index.
            if (!stream.good()) return stream;
            stream << db.idxDsc;

            // Write sub-term description index.
            if (!stream.good()) return stream;
            stream << db.idxDscSub;

            // Write data count.
            auto data_count = db.data.size();
#if defined(_WIN64) || defined(__x86_64__) || defined(__ppc64__)
            // 4G check
            if (data_count > 0xffffffff) {
                stream.setstate(std::ios_base::failbit);
                return stream;
            }
#endif
            if (stream.fail()) return stream;
            uint32_t count = (uint32_t)data_count;
            stream.write((const char*)&count, sizeof(count));

            // Write data.
            if (stream.fail()) return stream;
            stream.write((const char*)db.data.data(), sizeof(uint16_t)*static_cast<std::streamsize>(count));

            return stream;
        }


        ///
        /// Reads character database from a stream
        ///
        /// \param[in ] stream  Input stream
        /// \param[out] db      Character database
        ///
        /// \returns The stream \p stream
        ///
        friend std::istream& operator >>(_In_ std::istream& stream, _Out_ character_db& db)
        {
            // Read character index.
            stream >> db.idxChr;
            if (!stream.good()) return stream;

            // Read description index.
            stream >> db.idxDsc;
            if (!stream.good()) return stream;

            // Read sub-term description index.
            stream >> db.idxDscSub;
            if (!stream.good()) return stream;

            // Read data count.
            uint32_t count;
            stream.read((char*)&count, sizeof(count));
            if (!stream.good()) return stream;

            if (count) {
                // Read data.
                db.data.resize(count);
                stream.read((char*)db.data.data(), sizeof(uint16_t) * static_cast<std::streamsize>(count));
            }
            else
                db.data.clear();

            return stream;
        }
    };


    ///
    /// Character classification database template
    ///
    template <typename T_id>
    class chrclass_db {
    public:
#pragma pack(push)
#pragma pack(2)
        ///
        /// Character classification data
        ///
        struct chrcls {
        public:
            T_id id;            ///< Character classification ID
            uint16_t rank;      ///< Character classification rank

        protected:
            uint16_t name_to;   ///< Character classification name end in \c data
            char_t data[];      ///< Character classification name

        private:
            chrcls(_In_ const chrcls &other);
            chrcls& operator=(_In_ const chrcls &other);

        public:
            ///
            /// Constructs the character classification
            ///
            /// \param[in] id        Character classification ID
            /// \param[in] rank      Character classification rank
            /// \param[in] name      Character classification name
            /// \param[in] name_len  Number of UTF-16 characters in \p name
            ///
            chrcls(
                _In_opt_                         T_id      id       = default,
                _In_opt_                         uint16_t  rank     = 0,
                _In_opt_z_count_(name_len) const char_t   *name     = NULL,
                _In_opt_                         size_t    name_len = 0)
            {
                this->id   = id;
                this->rank = rank;
                this->name_to = static_cast<uint16_t>(name_len);
                if (name && name_len) memcpy(this->data, name, sizeof(char_t)*name_len);
            }

            const char_t*  name    () const { return data;           };
                  char_t*  name    ()       { return data;           };
            const char_t*  name_end() const { return data + name_to; };
                  char_t*  name_end()       { return data + name_to; };
                  uint16_t name_len() const { return name_to;        };
        };
#pragma pack(pop)

        ///
        /// Character classification index
        ///
        class indexChrId : public index<uint16_t, uint32_t, chrcls>
        {
        public:
            ///
            /// Constructs the index
            ///
            /// \param[in] h  Reference to vector holding the data
            ///
            indexChrId(_In_ std::vector<uint16_t> &h) : index<uint16_t, uint32_t, chrcls>(h) {}

            ///
            /// Compares two character categories by ID (for searching)
            ///
            /// \param[in] a  Pointer to first element
            /// \param[in] b  Pointer to second element
            ///
            /// \returns
            /// - <0 when a <  b
            /// - =0 when a == b
            /// - >0 when a >  b
            ///
            virtual int compare(_In_ const chrcls &a, _In_ const chrcls &b) const
            {
                if (a.id < b.id) return -1;
                if (a.id > b.id) return  1;

                return  0;
            }
        } idxChrId;  ///< Character classification index

        ///
        /// Rank index
        ///
        class indexRank : public index<uint16_t, uint32_t, chrcls>
        {
        public:
            ///
            /// Constructs the index
            ///
            /// \param[in] h  Reference to vector holding the data
            ///
            indexRank(_In_ std::vector<uint16_t> &h) : index<uint16_t, uint32_t, chrcls>(h) {}

            ///
            /// Compares two character categories by ID (for searching)
            ///
            /// \param[in] a  Pointer to first element
            /// \param[in] b  Pointer to second element
            ///
            /// \returns
            /// - <0 when a <  b
            /// - =0 when a == b
            /// - >0 when a >  b
            ///
            virtual int compare(_In_ const chrcls &a, _In_ const chrcls &b) const
            {
                if (a.rank < b.rank) return -1;
                if (a.rank > b.rank) return +1;

                return 0;
            }

            ///
            /// Compares two character categories by rank (for sorting)
            ///
            /// \param[in] a  Pointer to character classification
            /// \param[in] b  Pointer to second character classification
            ///
            /// \returns
            /// - <0 when a <  b
            /// - =0 when a == b
            /// - >0 when a >  b
            ///
            virtual int compare_sort(_In_ const chrcls &a, _In_ const chrcls &b) const
            {
                     if (a.rank < b.rank) return -1;
                else if (a.rank > b.rank) return +1;

                auto &coll = std::use_facet<std::collate<char_t>>(std::locale());
                return coll.compare(a.name(), a.name_end(), b.name(), b.name_end());
            }
        } idxRank;  ///< Rank index

        std::vector<uint16_t> data;     ///< Character classification data

    public:
        ///
        /// Constructs the database
        ///
        chrclass_db() : idxChrId(data), idxRank(data) {}

        ///
        /// Clears the database
        ///
        void clear()
        {
            idxChrId.clear();
            idxRank .clear();
            data    .clear();
        }


        ///
        /// Writes character classification database to a stream
        ///
        /// \param[in] stream  Output stream
        /// \param[in] db      Character classification database
        ///
        /// \returns The stream \p stream
        ///
        friend std::ostream& operator <<(_In_ std::ostream& stream, _In_ const chrclass_db<T_id>& db)
        {
            // Write character classification index.
            if (stream.fail()) return stream;
            stream << db.idxChrId;

            // Write rank index.
            if (stream.fail()) return stream;
            stream << db.idxRank;

            // Write data count.
            auto data_count = db.data.size();
#if defined(_WIN64) || defined(__x86_64__) || defined(__ppc64__)
            // 4G check
            if (data_count > 0xffffffff) {
                stream.setstate(std::ios_base::failbit);
                return stream;
            }
#endif
            if (stream.fail()) return stream;
            uint32_t count = (uint32_t)data_count;
            stream.write((const char*)&count, sizeof(count));

            // Write data.
            if (stream.fail()) return stream;
            stream.write((const char*)db.data.data(), sizeof(uint16_t) * static_cast<std::streamsize>(count));

            return stream;
        }


        ///
        /// Reads character classification database from a stream
        ///
        /// \param[in ] stream  Input stream
        /// \param[out] db      Character classification database
        ///
        /// \returns The stream \p stream
        ///
        friend std::istream& operator >>(_In_ std::istream& stream, _Out_ chrclass_db<T_id>& db)
        {
            // Read character classification index.
            stream >> db.idxChrId;
            if (!stream.good()) return stream;

            // Read rank index.
            stream >> db.idxRank;
            if (!stream.good()) return stream;

            // Read data count.
            uint32_t count;
            stream.read((char*)&count, sizeof(count));
            if (!stream.good()) return stream;

            if (count) {
                // Read data.
                db.data.resize(count);
                stream.read((char*)db.data.data(), sizeof(uint16_t) * static_cast<std::streamsize>(count));
            }
            else
                db.data.clear();

            return stream;
        }
    };

    ///
    /// Character block database
    ///
    using chrblk_db = chrclass_db<chrblkid_t>;
};

#pragma warning(pop)
