#pragma once

/* <editor-fold desc="MIT License">

Copyright(c) 2018 Robert Osfield

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

</editor-fold> */

#include <vsg/app/CompileManager.h>
#include <vsg/core/Inherit.h>
#include <vsg/core/observer_ptr.h>
#include <vsg/io/FileSystem.h>
#include <vsg/io/Options.h>
#include <vsg/nodes/PagedLOD.h>
#include <vsg/threading/DeleteQueue.h>
#include <vsg/utils/Instrumentation.h>

#include <condition_variable>
#include <list>
#include <thread>

namespace vsg
{

    /// Class used by the DatabasePager to keep track of PagedLOD nodes
    class CulledPagedLODs : public Inherit<Object, CulledPagedLODs>
    {
    public:
        CulledPagedLODs()
        {
            highresCulled.reserve(512);
            newHighresRequired.reserve(8);
        }

        void clear()
        {
            highresCulled.clear();
            newHighresRequired.clear();
        }

        std::vector<const PagedLOD*> highresCulled;
        std::vector<const PagedLOD*> newHighresRequired;
    };

    struct PagedLODContainer : public Inherit<Object, PagedLODContainer>
    {
        explicit PagedLODContainer(uint32_t maxNumPagedLOD = 10000);

        struct List
        {
            uint32_t head = 0;
            uint32_t tail = 0;
            uint32_t count = 0;
            std::string name;
        };

        struct Element
        {
            uint32_t previous = 0;
            uint32_t next = 0;
            ref_ptr<PagedLOD> plod;
            List* list = nullptr;
        };

        using Elements = std::vector<Element>;

        Elements elements;
        List availableList;
        List inactiveList;
        List activeList;

        void resize(uint32_t new_size);
        void resize();
        void inactive(const PagedLOD* plod);
        void active(const PagedLOD* plod);
        void remove(PagedLOD* plod);

        void _move(const PagedLOD* plod, List* targetList);

        bool check();
        bool check(const List& list);

        void print(std::ostream& out);
    };

    /// Thread safe queue for tracking PagedLOD that needs to be loaded, compiled or merged by the DatabasePager
    class VSG_DECLSPEC DatabaseQueue : public Inherit<Object, DatabaseQueue>
    {
    public:
        explicit DatabaseQueue(ref_ptr<ActivityStatus> status);

        using Nodes = std::list<ref_ptr<PagedLOD>>;

        ActivityStatus* getStatus() { return _status; }
        const ActivityStatus* getStatus() const { return _status; }

        void add(ref_ptr<PagedLOD> plod);

        void add(ref_ptr<PagedLOD> plod, const CompileResult& cr);

        /// prune entries older than specified frameCount
        uint32_t prune(uint64_t frameCount);

        ref_ptr<PagedLOD> take_when_available(uint64_t frameCount);

        Nodes take_all(CompileResult& result);

    protected:
        ~DatabaseQueue() override;

        std::mutex _mutex;
        std::condition_variable _cv;
        Nodes _queue;
        CompileResult _compileResult;
        ref_ptr<ActivityStatus> _status;
    };
    VSG_type_name(vsg::DatabaseQueue);

    /// Multi-threaded database pager for reading, compiling loaded PagedLOD subgraphs and updating the scene graph
    /// with newly loaded subgraphs and pruning expired PagedLOD subgraphs
    class VSG_DECLSPEC DatabasePager : public Inherit<Object, DatabasePager>
    {
    public:
        DatabasePager();

        DatabasePager(const DatabasePager&) = delete;
        DatabasePager& operator=(const DatabasePager& rhs) = delete;

        virtual void start(uint32_t numReadThreads = 4);

        virtual void request(ref_ptr<PagedLOD> plod);

        virtual void updateSceneGraph(ref_ptr<FrameStamp> frameStamp, CompileResult& cr);

        ref_ptr<CompileManager> compileManager;

        std::atomic_uint numActiveRequests{0};
        std::atomic_uint64_t frameCount{0};

        ref_ptr<CulledPagedLODs> culledPagedLODs;

        /// for systems with smaller GPU memory limits you may need to reduce the targetMaxNumPagedLODWithHighResSubgraphs to keep memory usage within available limits.
        uint32_t targetMaxNumPagedLODWithHighResSubgraphs = 1500;

        /// number of frames before a PagedLOD with a failed load/compile is attempted to be loaded/compiled again.
        uint64_t delayBeforeNextLoadAttempt = 60;

        std::mutex pendingPagedLODMutex;

        ref_ptr<PagedLODContainer> pagedLODContainer;

        /// Hook for assigning Instrumentation to enable profiling
        ref_ptr<Instrumentation> instrumentation;

        /// assign Instrumentation to all CompileTraversal and their associated Context
        void assignInstrumentation(ref_ptr<Instrumentation> in_instrumentation);

        /// read and delete threads created by start()
        std::list<std::thread> threads;

        ref_ptr<ActivityStatus> status;
        ref_ptr<DeleteQueue> deleteQueue;

    protected:
        ~DatabasePager() override;

        void requestDiscarded(PagedLOD* plod);

        ref_ptr<DatabaseQueue> _requestQueue;
        ref_ptr<DatabaseQueue> _toMergeQueue;
    };
    VSG_type_name(vsg::DatabasePager);

} // namespace vsg
