//
//  CardScannerViewManager.m
//  DWP
//
//  Created by Ahmed Wahdan on 04/12/2024.
//

#import "CardScannerViewManager.h"
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
#import <React/RCTUIManager.h>

// Ensure Swift bridging header is included correctly
#if __has_include("react_native_pay_components-Swift.h")
#import "react_native_pay_components-Swift.h"
#else
#import <react_native_pay_components/react_native_pay_components-Swift.h>
#endif

@implementation CardScannerViewManager

RCT_EXPORT_MODULE(CardScannerView)

- (UIView *)view {
  if (@available(iOS 13.0, *)) {
    return [[CardScannerView alloc] init];
  } else {
    // Fallback for older iOS versions
    UIView *fallbackView = [[UIView alloc] init];
    fallbackView.backgroundColor = [UIColor lightGrayColor];
    UILabel *label = [[UILabel alloc] initWithFrame:fallbackView.bounds];
    label.text = @"Card Scanner is not supported on this iOS version.";
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.numberOfLines = 0;
    [fallbackView addSubview:label];
    return fallbackView;
  }
}

// Expose startScanning method
RCT_EXPORT_METHOD(startScanning:(nonnull NSNumber *)reactTag) {
  [self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
    UIView *view = viewRegistry[reactTag];
    if (@available(iOS 13.0, *)) {
      if ([view isKindOfClass:[CardScannerView class]]) {
        [(CardScannerView *)view startScanning];
      } else {
        RCTLogError(@"Invalid view returned from registry, expecting CardScannerView, got: %@", view);
      }
    } else {
      RCTLogError(@"CardScannerView is not available on this iOS version.");
    }
  }];
}

RCT_EXPORT_METHOD(retryScanning:(nonnull NSNumber *)reactTag) {
  [self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
    UIView *view = viewRegistry[reactTag];
    if (@available(iOS 13.0, *)) {
      if ([view isKindOfClass:[CardScannerView class]]) {
        [(CardScannerView *)view retryScanning];
      } else {
        RCTLogError(@"Invalid view returned from registry, expecting CardScannerView, got: %@", view);
      }
    } else {
      RCTLogError(@"CardScannerView is not available on this iOS version.");
    }
  }];
}

RCT_EXPORT_METHOD(stopScanning:(nonnull NSNumber *)reactTag) {
  [self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
    UIView *view = viewRegistry[reactTag];
    if (@available(iOS 13.0, *)) {
      if ([view isKindOfClass:[CardScannerView class]]) {
        [(CardScannerView *)view stopScanning];
      } else {
        RCTLogError(@"Invalid view returned from registry, expecting CardScannerView, got: %@", view);
      }
    } else {
      RCTLogError(@"CardScannerView is not available on this iOS version.");
    }
  }];
}

RCT_EXPORT_METHOD(finshedScanning:(nonnull NSNumber *)reactTag) {
  [self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
    UIView *view = viewRegistry[reactTag];
    if (@available(iOS 13.0, *)) {
      if ([view isKindOfClass:[CardScannerView class]]) {
        [(CardScannerView *)view finshedScanning];
      } else {
        RCTLogError(@"Invalid view returned from registry, expecting CardScannerView, got: %@", view);
      }
    } else {
      RCTLogError(@"CardScannerView is not available on this iOS version.");
    }
  }];
}

// Expose onCardScanned callback
RCT_EXPORT_VIEW_PROPERTY(onCardScanned, RCTDirectEventBlock)

@end
