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

#include "HybridOmniViewComponent.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::omni::views {

  extern const char HybridOmniViewComponentName[] = "OmniView";

  HybridOmniViewProps::HybridOmniViewProps(const react::PropsParserContext& context,
                                           const HybridOmniViewProps& sourceProps,
                                           const react::RawProps& rawProps):
    react::ViewProps(context, sourceProps, rawProps, filterObjectKeys),
    player([&]() -> CachedProp<std::shared_ptr<HybridOmniPlayerSpec>> {
      try {
        const react::RawValue* rawValue = rawProps.at("player", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.player;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::shared_ptr<HybridOmniPlayerSpec>>::fromRawValue(*runtime, value, sourceProps.player);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("OmniView.player: ") + 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("OmniView.autoplay: ") + exc.what());
      }
    }()),
    autoPip([&]() -> CachedProp<std::optional<bool>> {
      try {
        const react::RawValue* rawValue = rawProps.at("autoPip", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.autoPip;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::optional<bool>>::fromRawValue(*runtime, value, sourceProps.autoPip);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("OmniView.autoPip: ") + exc.what());
      }
    }()),
    subtitleAssets([&]() -> CachedProp<std::optional<SubtitleAssets>> {
      try {
        const react::RawValue* rawValue = rawProps.at("subtitleAssets", nullptr, nullptr);
        if (rawValue == nullptr) return sourceProps.subtitleAssets;
        const auto& [runtime, value] = (std::pair<jsi::Runtime*, jsi::Value>)*rawValue;
        return CachedProp<std::optional<SubtitleAssets>>::fromRawValue(*runtime, value, sourceProps.subtitleAssets);
      } catch (const std::exception& exc) {
        throw std::runtime_error(std::string("OmniView.subtitleAssets: ") + exc.what());
      }
    }()),
    hybridRef([&]() -> CachedProp<std::optional<std::function<void(const std::shared_ptr<HybridOmniViewSpec>& /* 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<HybridOmniViewSpec>& /* 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("OmniView.hybridRef: ") + exc.what());
      }
    }()) { }

  bool HybridOmniViewProps::filterObjectKeys(const std::string& propName) {
    switch (hashString(propName)) {
      case hashString("player"): return true;
      case hashString("autoplay"): return true;
      case hashString("autoPip"): return true;
      case hashString("subtitleAssets"): return true;
      case hashString("hybridRef"): return true;
      default: return false;
    }
  }

  HybridOmniViewComponentDescriptor::HybridOmniViewComponentDescriptor(const react::ComponentDescriptorParameters& parameters)
    : ConcreteComponentDescriptor(parameters,
                                  react::RawPropsParser()) {}

  std::shared_ptr<const react::Props> HybridOmniViewComponentDescriptor::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 HybridOmniViewShadowNode::Props(context, /* & */ rawProps, props);
  }

#ifdef ANDROID
  void HybridOmniViewComponentDescriptor::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<HybridOmniViewShadowNode&>(shadowNode);
    const std::shared_ptr<const HybridOmniViewProps>& constProps = concreteShadowNode.getConcreteSharedProps();
    const std::shared_ptr<HybridOmniViewProps>& props = std::const_pointer_cast<HybridOmniViewProps>(constProps);
    HybridOmniViewState state{props};
    concreteShadowNode.setStateData(std::move(state));
  }
#endif

} // namespace margelo::nitro::omni::views
