///
/// HybridRiveViewComponent.cpp
/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
/// https://github.com/mrousavy/nitro
/// Copyright © Marc Rousavy @ Margelo
///

#include "HybridRiveViewComponent.hpp"

#include <string>
#include <exception>
#include <utility>
#include <NitroModules/NitroDefines.hpp>
#include <NitroModules/JSIConverter.hpp>
#include <NitroModules/PropNameIDCache.hpp>
#include <react/renderer/core/RawValue.h>
#include <react/renderer/core/ShadowNode.h>
#include <react/renderer/core/ComponentDescriptor.h>
#include <react/renderer/components/view/ViewProps.h>

namespace margelo::nitro::rive::views {

  extern const char HybridRiveViewComponentName[] = "RiveView";

  HybridRiveViewProps::HybridRiveViewProps(const react::PropsParserContext& context,
                                           const HybridRiveViewProps& sourceProps,
                                           const react::RawProps& rawProps):
    react::ViewProps(context, sourceProps, rawProps, filterObjectKeys),
    artboardName([&]() -> CachedProp<std::optional<std::string>> {
      try {
        const react::RawValue* rawValue = rawProps.at("artboardName", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.artboardName;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::optional<std::string>>::fromRawValue(*runtime, value, sourceProps.artboardName);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("RiveView.artboardName: ") + exc.what());
      }
    }()),
    stateMachineName([&]() -> CachedProp<std::optional<std::string>> {
      try {
        const react::RawValue* rawValue = rawProps.at("stateMachineName", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.stateMachineName;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::optional<std::string>>::fromRawValue(*runtime, value, sourceProps.stateMachineName);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("RiveView.stateMachineName: ") + exc.what());
      }
    }()),
    autoPlay([&]() -> CachedProp<std::optional<bool>> {
      try {
        const react::RawValue* rawValue = rawProps.at("autoPlay", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.autoPlay;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::optional<bool>>::fromRawValue(*runtime, value, sourceProps.autoPlay);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("RiveView.autoPlay: ") + exc.what());
      }
    }()),
    file([&]() -> CachedProp<std::shared_ptr<HybridRiveFileSpec>> {
      try {
        const react::RawValue* rawValue = rawProps.at("file", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.file;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::shared_ptr<HybridRiveFileSpec>>::fromRawValue(*runtime, value, sourceProps.file);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("RiveView.file: ") + exc.what());
      }
    }()),
    alignment([&]() -> CachedProp<std::optional<Alignment>> {
      try {
        const react::RawValue* rawValue = rawProps.at("alignment", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.alignment;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::optional<Alignment>>::fromRawValue(*runtime, value, sourceProps.alignment);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("RiveView.alignment: ") + exc.what());
      }
    }()),
    fit([&]() -> CachedProp<std::optional<Fit>> {
      try {
        const react::RawValue* rawValue = rawProps.at("fit", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.fit;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::optional<Fit>>::fromRawValue(*runtime, value, sourceProps.fit);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("RiveView.fit: ") + exc.what());
      }
    }()),
    layoutScaleFactor([&]() -> CachedProp<std::optional<double>> {
      try {
        const react::RawValue* rawValue = rawProps.at("layoutScaleFactor", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.layoutScaleFactor;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::optional<double>>::fromRawValue(*runtime, value, sourceProps.layoutScaleFactor);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("RiveView.layoutScaleFactor: ") + exc.what());
      }
    }()),
    dataBind([&]() -> CachedProp<std::optional<std::variant<std::shared_ptr<HybridViewModelInstanceSpec>, DataBindMode, DataBindByName>>> {
      try {
        const react::RawValue* rawValue = rawProps.at("dataBind", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.dataBind;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::optional<std::variant<std::shared_ptr<HybridViewModelInstanceSpec>, DataBindMode, DataBindByName>>>::fromRawValue(*runtime, value, sourceProps.dataBind);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("RiveView.dataBind: ") + exc.what());
      }
    }()),
    onError([&]() -> CachedProp<std::function<void(const RiveError& /* error */)>> {
      try {
        const react::RawValue* rawValue = rawProps.at("onError", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.onError;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::function<void(const RiveError& /* error */)>>::fromRawValue(*runtime, value.asObject(*runtime).getProperty(*runtime, PropNameIDCache::get(*runtime, "f")), sourceProps.onError);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("RiveView.onError: ") + exc.what());
      }
    }()),
    hybridRef([&]() -> CachedProp<std::optional<std::function<void(const std::shared_ptr<HybridRiveViewSpec>& /* ref */)>>> {
      try {
        const react::RawValue* rawValue = rawProps.at("hybridRef", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.hybridRef;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::optional<std::function<void(const std::shared_ptr<HybridRiveViewSpec>& /* ref */)>>>::fromRawValue(*runtime, value.asObject(*runtime).getProperty(*runtime, PropNameIDCache::get(*runtime, "f")), sourceProps.hybridRef);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("RiveView.hybridRef: ") + exc.what());
      }
    }()) { }

  bool HybridRiveViewProps::filterObjectKeys(const std::string& propName) {
    switch (hashString(propName)) {
      case hashString("artboardName"): return true;
      case hashString("stateMachineName"): return true;
      case hashString("autoPlay"): return true;
      case hashString("file"): return true;
      case hashString("alignment"): return true;
      case hashString("fit"): return true;
      case hashString("layoutScaleFactor"): return true;
      case hashString("dataBind"): return true;
      case hashString("onError"): return true;
      case hashString("hybridRef"): return true;
      default: return false;
    }
  }

  HybridRiveViewComponentDescriptor::HybridRiveViewComponentDescriptor(const react::ComponentDescriptorParameters& parameters)
    : ConcreteComponentDescriptor(parameters,
                                  react::RawPropsParser(/* enableJsiParser */ true)) {}

  std::shared_ptr<const react::Props> HybridRiveViewComponentDescriptor::cloneProps(const react::PropsParserContext& context,
                                                                                    const std::shared_ptr<const react::Props>& props,
                                                                                    react::RawProps rawProps) const {
    // 1. Prepare raw props parser
    rawProps.parse(rawPropsParser_);
    // 2. Copy props with Nitro's cached copy constructor
    return HybridRiveViewShadowNode::Props(context, /* & */ rawProps, props);
  }

#ifdef ANDROID
  void HybridRiveViewComponentDescriptor::adopt(react::ShadowNode& shadowNode) const {
    // This is called immediately after `ShadowNode` is created, cloned or in progress.
    // On Android, we need to wrap props in our state, which gets routed through Java and later unwrapped in JNI/C++.
    auto& concreteShadowNode = static_cast<HybridRiveViewShadowNode&>(shadowNode);
    const std::shared_ptr<const HybridRiveViewProps>& constProps = concreteShadowNode.getConcreteSharedProps();
    const std::shared_ptr<HybridRiveViewProps>& props = std::const_pointer_cast<HybridRiveViewProps>(constProps);
    HybridRiveViewState state{props};
    concreteShadowNode.setStateData(std::move(state));
  }
#endif

} // namespace margelo::nitro::rive::views
