/*
 * 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 "cista/containers/tuple.h"
#include "cista/storage/unordered_set.h"
#include "cista/storage/vector.h"
#include "mimir/common/printers.hpp"
#include "mimir/common/types_cista.hpp"
#include "mimir/formalism/declarations.hpp"
#include "mimir/search/declarations.hpp"

#include <ostream>
#include <tuple>

namespace mimir
{
struct StripsActionPrecondition
{
    FlatBitset m_positive_static_atoms = FlatBitset();
    FlatBitset m_negative_static_atoms = FlatBitset();
    FlatBitset m_positive_fluent_atoms = FlatBitset();
    FlatBitset m_negative_fluent_atoms = FlatBitset();
    FlatBitset m_positive_derived_atoms = FlatBitset();
    FlatBitset m_negative_derived_atoms = FlatBitset();

    template<PredicateTag P>
    FlatBitset& get_positive_precondition();

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

    template<PredicateTag P>
    FlatBitset& get_negative_precondition();

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

    template<DynamicPredicateTag P>
    bool is_applicable(State state) const;

    bool is_dynamically_applicable(State state) const;

    bool is_statically_applicable(const FlatBitset& static_positive_atoms) const;

    bool is_applicable(Problem problem, State state) const;

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

    bool is_applicable(const FlatBitset& fluent_state_atoms, const FlatBitset& derived_state_atoms, const FlatBitset& static_initial_atoms) const;
};

struct StripsActionEffect
{
    FlatBitset m_positive_effects = FlatBitset();
    FlatBitset m_negative_effects = FlatBitset();
    ContinuousCost m_cost = ContinuousCost(0.0);

    FlatBitset& get_positive_effects();
    const FlatBitset& get_positive_effects() const;

    FlatBitset& get_negative_effects();
    const FlatBitset& get_negative_effects() const;

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

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

using SimpleFluentEffectList = cista::offset::vector<SimpleFluentEffect>;

struct ConditionalEffect
{
    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();
    SimpleFluentEffectList m_effect = SimpleFluentEffectList();
    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 */
    SimpleFluentEffectList& get_simple_effect();
    const SimpleFluentEffectList& get_simple_effect() const;

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

    /* Utility */
    template<DynamicPredicateTag P>
    bool is_applicable(State state) const;

    bool is_dynamically_applicable(State state) const;

    bool is_statically_applicable(Problem problem) const;

    bool is_applicable(Problem problem, State state) const;
};

using ConditionalEffects = cista::offset::vector<ConditionalEffect>;

/**
 * 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();
    StripsActionPrecondition m_strips_precondition = StripsActionPrecondition();
    StripsActionEffect m_strips_effect = StripsActionEffect();
    ConditionalEffects m_conditional_effects = ConditionalEffects();

    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 */
    StripsActionPrecondition& get_strips_precondition();
    const StripsActionPrecondition& get_strips_precondition() const;
    StripsActionEffect& get_strips_effect();
    const StripsActionEffect& get_strips_effect() const;
    /* Conditional effects */
    ConditionalEffects& get_conditional_effects();
    const ConditionalEffects& get_conditional_effects() const;

    bool is_dynamically_applicable(State state) const;

    bool is_statically_applicable(const FlatBitset& static_positive_atoms) const;

    bool is_applicable(Problem problem, State state) const;
};

}

template<>
struct cista::storage::DerefStdHasher<mimir::GroundActionImpl>
{
    size_t operator()(const mimir::GroundActionImpl* ptr) const;
};

template<>
struct cista::storage::DerefStdEqualTo<mimir::GroundActionImpl>
{
    bool operator()(const mimir::GroundActionImpl* lhs, const mimir::GroundActionImpl* rhs) const;
};

namespace mimir
{

/**
 * Mimir types
 */

using GroundActionImplSet = cista::storage::UnorderedSet<GroundActionImpl>;

/**
 * Pretty printing
 */

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

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

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

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

template<>
std::ostream& operator<<(std::ostream& os, const std::tuple<ConditionalEffect, 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
