#pragma once

#include <jsi/jsi.h>
#include <reanimated/Compat/ReactNativeRendererCore.h>
#include <reanimated/Fabric/ShadowTreeCloner.h>

#include <react/renderer/animationbackend/AnimatedProps.h>
#include <react/renderer/animationbackend/AnimatedPropsBuilder.h>
#include <react/renderer/animationbackend/AnimationBackend.h>

#include <memory>
#include <optional>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

namespace reanimated {

using namespace facebook;
using namespace react;

using UpdatesBatch = std::vector<std::pair<ShadowNodeFamily::Shared, folly::dynamic>>;

struct AnimatedPropsEntry {
  const ShadowNodeFamily::Shared shadowNodeFamily;
  AnimatedProps animatedProps;
  const bool hasLayoutUpdates;
};
using UpdatesBatchAnimatedProps = std::vector<AnimatedPropsEntry>;

using PropNamePredicate = bool (*)(const std::string &);

using RegistryMap = std::unordered_map<Tag, std::pair<ShadowNodeFamily::Shared, folly::dynamic>>;

#ifdef ANDROID
struct PropsToRevert {
  ShadowNodeFamily::Shared shadowNodeFamily;
  std::unordered_set<std::string> props;
};

using PropsToRevertMap = std::unordered_map<Tag, PropsToRevert>;
#endif

// All public methods require `UpdatesRegistryManager::lock()` to be held.
class UpdatesRegistry {
 public:
  virtual ~UpdatesRegistry() {}

  virtual bool isEmpty() const;
  folly::dynamic get(Tag tag) const;
  void mergeInto(Tag tag, folly::dynamic &target) const;
  std::optional<folly::dynamic> getProp(Tag tag, const char *propName) const;
#ifdef ANDROID
  void mergeInto(Tag tag, folly::dynamic &target, PropNamePredicate isIncluded) const;
#endif
  void remove(Tag tag);
  virtual void removeUnmounted(const ShadowNodeFamily::Shared &shadowNodeFamily);
  virtual void handleRemount(Tag /*tag*/) {}

#ifdef ANDROID
  bool hasPropsToRevert() const;
  void collectPropsToRevert(PropsToRevertMap &propsToRevertMap);
#endif

  // Drains pending style updates as folly::dynamic.
  void flushUpdates(UpdatesBatch &updatesBatch);

  // Drains pending typed animated props.
  void flushUpdates(UpdatesBatchAnimatedProps &updatesBatch);

  // Get only non-layout updates (for android event handling) in the animation backend path.
  void flushNonLayoutUpdates(jsi::Runtime &rt, facebook::react::AnimationMutations &mutations);
  bool hasPendingAnimatedPropsUpdates() const;
  void addAnimatedPropsToBatch(
      const ShadowNodeFamily::Shared &shadowNodeFamily,
      AnimatedProps animatedProps,
      bool hasLayoutUpdates = false);
  void addRawPropsToAnimatedPropsBatch(const ShadowNodeFamily::Shared &shadowNodeFamily, folly::dynamic props);
  void addJSIPropsToAnimatedPropsBatch(
      const ShadowNodeFamily::Shared &shadowNodeFamily,
      jsi::Runtime &rt,
      jsi::Value &props);

  void collectProps(PropsMap &propsMap) const;
  UpdatesBatch getPendingUpdates();

 protected:
  RegistryMap updatesRegistry_;

  /// Assumes the caller already locked the registry.
  void flush(UpdatesBatch &updatesBatch);

  // Assumes the caller already locked the registry.
  void flush(UpdatesBatchAnimatedProps &updatesBatch);

  /// Assumes the caller already locked the registry.
  virtual void removeTag(Tag tag) = 0;

  /// Assumes the caller already locked the registry.
  void addUpdatesToBatch(const ShadowNodeFamily::Shared &shadowNodeFamily, const folly::dynamic &props);
  void addUpdatesToBatch(const ShadowNodeFamily::Shared &shadowNodeFamily, folly::dynamic &&props);

  /// Assumes the caller already locked the registry.
  std::unordered_set<Tag> getPendingTags() const;

  /// Assumes the caller already locked the registry.
  folly::dynamic getUpdatesFromRegistry(const Tag tag) const;

  /// Assumes the caller already locked the registry.
  void setInUpdatesRegistry(const ShadowNodeFamily::Shared &shadowNodeFamily, const folly::dynamic &props);

  /// Assumes the caller already locked the registry.
  void removeFromUpdatesRegistry(Tag tag);

  /// Assumes the caller already locked the registry.
  void removePendingUpdates(Tag tag);

 private:
  UpdatesBatch updatesBatch_;

  UpdatesBatchAnimatedProps updatesBatchAnimatedProps_;
  AnimatedPropsBuilder animatedPropsBuilder_;

  void flushUpdatesToRegistry(const UpdatesBatch &updatesBatch);

#ifdef ANDROID
  PropsToRevertMap propsToRevertMap_;

  void updatePropsToRevert(Tag tag, const folly::dynamic *newProps = nullptr);
#endif
};

} // namespace reanimated
