/*
 * Copyright (C) 2023 Dominik Drexler and Simon Stahlberg
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef MIMIR_SEARCH_ACTION_HPP_
#define MIMIR_SEARCH_ACTION_HPP_

#include "mimir/buffering/unordered_set.h"
#include "mimir/buffering/vector.h"
#include "mimir/common/printers.hpp"
#include "mimir/common/types_cista.hpp"
#include "mimir/formalism/declarations.hpp"
#include "mimir/search/declarations.hpp"

#include <loki/details/utils/equal_to.hpp>
#include <loki/details/utils/hash.hpp>

namespace mimir
{
struct GroundConditionStrips
{
    FlatIndexList m_positive_static_atoms = FlatIndexList();
    FlatIndexList m_negative_static_atoms = FlatIndexList();
    FlatIndexList m_positive_fluent_atoms = FlatIndexList();
    FlatIndexList m_negative_fluent_atoms = FlatIndexList();
    FlatIndexList m_positive_derived_atoms = FlatIndexList();
    FlatIndexList m_negative_derived_atoms = FlatIndexList();

    template<PredicateTag P>
    FlatIndexList& get_positive_precondition();

    template<PredicateTag P>
    const FlatIndexList& get_positive_precondition() const;

    template<PredicateTag P>
    FlatIndexList& get_negative_precondition();

    template<PredicateTag P>
    const FlatIndexList& get_negative_precondition() const;

    template<PredicateTag P>
    bool is_applicable(const FlatBitset& atoms) const;

    bool is_dynamically_applicable(const DenseState& dense_state) const;

    bool is_statically_applicable(const FlatBitset& static_positive_atoms) const;

    bool is_applicable(Problem problem, const DenseState& dense_state) const;
};

struct GroundEffectStrips
{
    FlatIndexList m_positive_effects = FlatIndexList();
    FlatIndexList m_negative_effects = FlatIndexList();
    ContinuousCost m_cost = ContinuousCost(0.0);

    FlatIndexList& get_positive_effects();
    const FlatIndexList& get_positive_effects() const;

    FlatIndexList& get_negative_effects();
    const FlatIndexList& get_negative_effects() const;

    ContinuousCost& get_cost();
    const ContinuousCost& get_cost() const;
};

/// @brief `GroundEffectFluentLiteral` encapsulates the effect on a single grounded atom.
/// We cannot consistently use cista::tuple since nested tuples will automatically be flattened.
struct GroundEffectFluentLiteral
{
    bool is_negated = false;
    Index atom_index = Index(0);
};

using GroundEffectFluentLiteralList = cista::offset::vector<GroundEffectFluentLiteral>;

struct GroundEffectConditional
{
    FlatIndexList m_positive_static_atoms = FlatIndexList();
    FlatIndexList m_negative_static_atoms = FlatIndexList();
    FlatIndexList m_positive_fluent_atoms = FlatIndexList();
    FlatIndexList m_negative_fluent_atoms = FlatIndexList();
    FlatIndexList m_positive_derived_atoms = FlatIndexList();
    FlatIndexList m_negative_derived_atoms = FlatIndexList();
    GroundEffectFluentLiteralList m_effects = GroundEffectFluentLiteralList();
    ContinuousCost m_cost = ContinuousCost(0.0);

    /* Precondition */
    template<PredicateTag P>
    FlatIndexList& get_positive_precondition();
    template<PredicateTag P>
    const FlatIndexList& get_positive_precondition() const;

    template<PredicateTag P>
    FlatIndexList& get_negative_precondition();
    template<PredicateTag P>
    const FlatIndexList& get_negative_precondition() const;

    /* Simple effects */
    GroundEffectFluentLiteralList& get_fluent_effect_literals();
    const GroundEffectFluentLiteralList& get_fluent_effect_literals() const;

    /* Costs */
    ContinuousCost& get_cost();
    const ContinuousCost& get_cost() const;

    /* Utility */
    template<PredicateTag P>
    bool is_applicable(const FlatBitset& atoms) const;

    bool is_dynamically_applicable(const DenseState& dense_state) const;

    bool is_statically_applicable(Problem problem) const;

    bool is_applicable(Problem problem, const DenseState& dense_state) const;
};

using GroundEffectConditionalList = cista::offset::vector<GroundEffectConditional>;

/**
 * Implementation class
 *
 * Reads the memory layout generated by the search node builder.
 */
struct GroundActionImpl
{
    Index m_index = Index(0);
    Index m_action_index = Index(0);
    FlatIndexList m_objects = FlatIndexList();
    GroundConditionStrips m_strips_precondition = GroundConditionStrips();
    GroundEffectStrips m_strips_effect = GroundEffectStrips();
    GroundEffectConditionalList m_conditional_effects = GroundEffectConditionalList();

    Index& get_index();
    Index& get_action_index();
    FlatIndexList& get_objects();

    Index get_index() const;
    Index get_action_index() const;
    const FlatIndexList& get_object_indices() const;

    /* STRIPS part */
    GroundConditionStrips& get_strips_precondition();
    const GroundConditionStrips& get_strips_precondition() const;
    GroundEffectStrips& get_strips_effect();
    const GroundEffectStrips& get_strips_effect() const;
    /* Conditional effects */
    GroundEffectConditionalList& get_conditional_effects();
    const GroundEffectConditionalList& get_conditional_effects() const;

    bool is_dynamically_applicable(const DenseState& dense_state) const;

    bool is_statically_applicable(const FlatBitset& static_positive_atoms) const;

    bool is_applicable(Problem problem, const DenseState& dense_state) const;

    /// @brief Return a tuple of const references to the members that uniquely identify an object.
    /// This enables the automatic generation of `loki::Hash` and `loki::EqualTo` specializations.
    ///
    /// Only return the lifted schema index and the binding because they imply the rest.
    /// @return a tuple containing const references to the members defining the object's identity.
    auto identifiable_members() const { return std::forward_as_tuple(std::as_const(m_action_index), std::as_const(m_objects)); }
};

/// @brief STL does not define operator== for std::span.
inline bool operator==(const std::span<const GroundAction>& lhs, const std::span<const GroundAction>& rhs)
{
    return (lhs.data() == rhs.data()) && (lhs.size() == rhs.size());
}

/**
 * Mimir types
 */

using GroundActionImplSet = mimir::buffering::UnorderedSet<GroundActionImpl>;

/**
 * Pretty printing
 */

template<>
std::ostream& operator<<(std::ostream& os, const std::tuple<GroundEffectFluentLiteral, const PDDLRepositories&>& data);

template<>
std::ostream& operator<<(std::ostream& os, const std::tuple<GroundEffectFluentLiteralList, const PDDLRepositories&>& data);

template<>
std::ostream& operator<<(std::ostream& os, const std::tuple<GroundConditionStrips, const PDDLRepositories&>& data);

template<>
std::ostream& operator<<(std::ostream& os, const std::tuple<GroundEffectStrips, const PDDLRepositories&>& data);

template<>
std::ostream& operator<<(std::ostream& os, const std::tuple<GroundEffectConditional, const PDDLRepositories&>& data);

struct FullActionFormatterTag
{
};
struct PlanActionFormatterTag
{
};

template<>
std::ostream& operator<<(std::ostream& os, const std::tuple<GroundAction, const PDDLRepositories&, FullActionFormatterTag>& data);

template<>
std::ostream& operator<<(std::ostream& os, const std::tuple<GroundAction, const PDDLRepositories&, PlanActionFormatterTag>& data);

}

#endif
