//#####################################################################
// Copyright 2020, Craig Schroeder.
// This file is part of PhysBAM whose distribution is governed by the license contained in the accompanying file PHYSBAM_COPYRIGHT.txt.
//#####################################################################
#ifndef __QT_PICK__
#define __QT_PICK__

#include <Core/Log/LOG.h>
#include <Core/Matrices/MATRIX.h>
#include <Core/Meshes/MESHES_FORWARD.h>
#include <Core/Vectors/VECTOR.h>

namespace PhysBAM{

class SHADER_HOLDER;
class QT_CAMERA;
template<class T> class QT_RENDER_OPTIONS;

class QT_PICK
{
    ARRAY<std::pair<int,ARRAY<int> > > history;
    ARRAY<int> names;
    int next=1;
public:
    void Reset()
    {
        history.Remove_All();
        names.Remove_All();
        next=1;
    }
    void Push(int n)
    {
        names.Append(n);
    }
    void Pop()
    {
        names.Pop();
    }
    void Set(int n)
    {
        names.Last()=n;
    }
    void Commit_Names(int num)
    {
        history.Append({next,names});
        next+=num;
    }
    int Next() const
    {
        return next;
    }
    void Find(ARRAY<int>& a,int n)
    {
        std::pair<int,ARRAY<int> > p={n,{}};
        auto it=std::upper_bound(history.begin(),history.end(),p,
            [](const auto& r,const auto& s){return r.first<s.first;});
        PHYSBAM_ASSERT(it!=history.begin());
        const auto& h=*--it;
        a=h.second;
        a.Append(n-h.first);
    }
};
}
#endif
