#import "React/RCTViewManager.h"
#import "React/RCTUIManager.h"
#import "BluestackBannerView.h"

@interface RCT_EXTERN_MODULE(BluestackModuleViewManager, RCTViewManager)

RCT_EXPORT_VIEW_PROPERTY(placementId, NSString)
RCT_EXPORT_VIEW_PROPERTY(preference, NSString)
RCT_EXPORT_VIEW_PROPERTY(width, CGFloat)
RCT_EXPORT_VIEW_PROPERTY(height, CGFloat)
RCT_EXPORT_VIEW_PROPERTY(shouldLoadWhenReady, BOOL)
RCT_EXPORT_VIEW_PROPERTY(shouldLoadNow, BOOL)

RCT_EXPORT_VIEW_PROPERTY(onAdLoaded, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onAdFailedToLoad, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onAdRefreshed, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onAdFailedToRefresh, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onAdClicked, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onAdLoadCalled, RCTBubblingEventBlock)


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

RCT_EXPORT_METHOD(destroy:(nonnull NSNumber*) reactTag) {
//    NSLog(@" BluestackBannerView  destroy callNativeMethod with tag: #%@", reactTag);
    [self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *,UIView *> *viewRegistry) {
        BluestackBannerView *view = viewRegistry[reactTag];
        if (!view || ![view isKindOfClass:[BluestackBannerView class]]) {
            return;
        }
//        NSLog(@" BluestackBannerView callNativeMethod destroy");
        [view destroy];
    }];
}

RCT_EXPORT_METHOD(refresh:(nonnull NSNumber*) reactTag setState:(BOOL)state) {
//    NSLog(@" BluestackBannerView  refresh callNativeMethod with tag: #%@", reactTag);
    [self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *,UIView *> *viewRegistry) {
        BluestackBannerView *view = viewRegistry[reactTag];
        if (!view || ![view isKindOfClass:[BluestackBannerView class]]) {
            return;
        }
//        NSLog(@" BluestackBannerView callNativeMethod refresh with state: #%i", state);
        [view refresh:state];
    }];
}

@end

