// graph-tool -- a general graph modification and manipulation thingy
//
// Copyright (C) 2006-2026 Tiago de Paula Peixoto <tiago@skewed.de>
//
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option) 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 Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef GRAPH_BLOCKMODEL_MCMC_HH
#define GRAPH_BLOCKMODEL_MCMC_HH

#include "config.h"

#include <vector>

#include "graph_tool.hh"
#include "../../support/graph_state.hh"
#include "../../loops/mcmc_loop.hh"

#include <boost/mpl/vector.hpp>

namespace graph_tool
{
using namespace boost;
using namespace std;

#define MCMC_LOOP_STATE_params(State)                                          \
    ((state, State&, 0))                                                       \
    ((groups, std::vector<typename State::group_t>&, 0))                       \
    ((beta, double, 0))                                                        \
    ((entropy_args, typename State::entropy_args_t, 0))                        \
    ((sequential, bool, 0))                                                    \
    ((deterministic, bool, 0))                                                 \
    ((verbose, int, 0))                                                        \
    ((niter, size_t, 0))

template <class State>
struct MCMC
{
    GEN_STATE_BASE(MCMCLoopStateBase, MCMC_LOOP_STATE_params(State))

    template <class... Ts>
    class MCMCLoopState
        : public MCMCLoopStateBase<Ts...>,
          public MetropolisStateBase
    {
    public:
        GET_PARAMS_USING(MCMCLoopStateBase<Ts...>,
                         MCMC_LOOP_STATE_params(State))
        GET_PARAMS_TYPEDEF(Ts, MCMC_LOOP_STATE_params(State))

        template <class... ATs>
        MCMCLoopState(ATs&&... as)
           : MCMCLoopStateBase<Ts...>(std::forward<ATs>(as)...)
        {
            GILRelease gil_release;
            if (_groups.empty())
            {
                for (auto r : vertices_range(_state._bg))
                {
                    if (_state._wr[r] > 0)
                        _groups.push_back(r);
                }
            }
        }

        typedef State::group_t vertex_t;
        typedef int64_t group_t;

        constexpr static group_t _null_move = std::numeric_limits<group_t>::max();

        group_t node_state(vertex_t v)
        {
            return _state._spec._u[v];
        }

        constexpr bool skip_node(vertex_t)
        {
            return false;
        }

        template <class RNG>
        group_t move_proposal(vertex_t v, RNG& rng)
        {
            group_t s = _state._spec.sample_group_label(v, rng);
            if (s == node_state(v))
                return _null_move;
            return s;
        }

        std::tuple<double, double>
        virtual_move_dS(vertex_t v, group_t nr)
        {
            group_t r = node_state(v);
            if (r == nr)
                return std::make_tuple(0., 0.);

            double dS = _state._spec.relabel_group_dS(v, nr, _entropy_args);
            double a = 0;
            if (!std::isinf(_beta))
            {
                double pf = _state._spec.get_group_label_prob(v, r, nr, false);
                double pb = _state._spec.get_group_label_prob(v, nr, r, true);
                a = pb - pf;
            }
            return std::make_tuple(dS, a);
        }

        void perform_move(vertex_t v, group_t nr)
        {
            _state._spec.relabel_group(v, nr);
        }

        bool is_deterministic()
        {
            return _deterministic;
        }

        bool is_sequential()
        {
            return _sequential;
        }

        auto& get_vlist()
        {
            return _groups;
        }

        double get_beta()
        {
            return _beta;
        }

        size_t get_niter()
        {
            return _niter;
        }
    };
};

template <class State>
GEN_DISPATCH(mcmc_loop_state, MCMC<State>::template MCMCLoopState,
             MCMC_LOOP_STATE_params(State))

} // graph_tool namespace

#endif //GRAPH_BLOCKMODEL_MCMC_HH
