//
//  VideoFeedManager.m
//  react-native-firework-sdk
//
//  Created by Jeff Zheng on 2021/12/22.
//
//  The FWVideoFeedManager exposes instances of Objective-C (native) classes to JavaScript (JS) as JS objects

#import <React/RCTBridgeModule.h>
#import <React/RCTViewManager.h>
#import <React/RCTUIManager.h>

#if __has_include(<react_native_firework_sdk/react_native_firework_sdk-Swift.h>)
#import <react_native_firework_sdk/react_native_firework_sdk-Swift.h>
#else
#import "react_native_firework_sdk-Swift.h"
#endif

@interface RCT_EXTERN_REMAP_MODULE(FWVideoFeed, FWVideoFeedManager, NSObject)

RCT_CUSTOM_VIEW_PROPERTY(source, VideFeedSourceType, VideoFeed) {
    if (json) {
        VideFeedSourceType sourceType = [RCTConvert videoFeedSourceType:json];
        view.sourceType = sourceType;
    }
}
RCT_EXPORT_VIEW_PROPERTY(channel, NSString)
RCT_EXPORT_VIEW_PROPERTY(playlist, NSString)
RCT_EXPORT_VIEW_PROPERTY(playlistGroup, NSString)
RCT_EXPORT_VIEW_PROPERTY(dynamicContentParameters, NSDictionary)
RCT_EXPORT_VIEW_PROPERTY(hashtagFilterExpression, NSString)
RCT_EXPORT_VIEW_PROPERTY(productIds, NSArray)
RCT_EXPORT_VIEW_PROPERTY(contentId, NSString)

RCT_CUSTOM_VIEW_PROPERTY(mode, VideoFeedMode, VideoFeed) {
    if (json) {
        VideoFeedMode mode = [RCTConvert videoFeedMode:json];
        view.mode = mode;
    }
}
RCT_CUSTOM_VIEW_PROPERTY(videoFeedConfiguration, VideoFeedConfiguration, VideoFeed) {
    VideoFeedConfiguration *config = [RCTConvert videoFeedConfiguration:json];
    view.feedViewConfig = config;
}
RCT_CUSTOM_VIEW_PROPERTY(videoPlayerConfiguration, VideoPlayerConfiguration, VideoFeed) {
    VideoPlayerConfiguration *config = [RCTConvert videoPlayerConfiguration:json];
    view.playerViewConfig = config;
}

RCT_CUSTOM_VIEW_PROPERTY(adConfiguration, AdConfiguration, VideoFeed) {
    AdConfiguration *config = [RCTConvert adConfiguration:json];
    view.adConfiguration = config;
}

// Both are fixed when the native feed is created; JS re-creates the view when they change.
RCT_EXPORT_VIEW_PROPERTY(autosizing, BOOL)
RCT_EXPORT_VIEW_PROPERTY(maxVideos, NSNumber)

RCT_EXPORT_VIEW_PROPERTY(onVideoFeedLoadFinished, RCTBubblingEventBlock)

RCT_EXPORT_VIEW_PROPERTY(onVideoFeedEmpty, RCTBubblingEventBlock)

RCT_EXPORT_VIEW_PROPERTY(onVideoFeedDidStartPictureInPicture, RCTBubblingEventBlock)

RCT_EXPORT_VIEW_PROPERTY(onVideoFeedDidStopPictureInPicture, RCTBubblingEventBlock)

RCT_EXPORT_VIEW_PROPERTY(onVideoFeedGetFeedId, RCTBubblingEventBlock)

// Autosizing height feedback (native -> JS). Direct event with a component-specific name:
// a bare `onContentSizeChange` would map to `topContentSizeChange`, which React Native
// core already registers for TextInput, and re-registering a core event name with a
// different type fails at bridge start-up.
RCT_EXPORT_VIEW_PROPERTY(onVideoFeedContentSizeChange, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onVideoFeedVideosLoaded, RCTBubblingEventBlock)

RCT_EXPORT_METHOD(refresh:(nonnull NSNumber *)reactTag) {
    [self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *,UIView *> *viewRegistry) {
        VideoFeed *view = (VideoFeed *)(viewRegistry[reactTag]);
        if (!view || ![view isKindOfClass:[VideoFeed class]]) {
            RCTLogError(@"Cannot find NativeView with tag #%@", reactTag);
            return;
        }
        [view refresh];
    }];
}

@end
