#import <objc/runtime.h>
#import <Foundation/Foundation.h>
#import <React/RCTViewManager.h>
#import <React/RCTMultilineTextInputViewManager.h>
#import <React/RCTSinglelineTextInputViewManager.h>
#import <React/RCTMultilineTextInputView.h>
#import <React/RCTSinglelineTextInputView.h>
#import <React/RCTUITextView.h>
#import <React/RCTUITextField.h>
#import "humand-Swift.h"

// MARK: - Extender View Managers para soportar nuevas props

@implementation RCTMultilineTextInputViewManager (HMTextInputProps)

RCT_EXPORT_VIEW_PROPERTY(onPasteImage, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(pasteImageLabel, NSString)

@end

@implementation RCTSinglelineTextInputViewManager (HMTextInputProps)

RCT_EXPORT_VIEW_PROPERTY(onPasteImage, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(pasteImageLabel, NSString)

@end

@implementation RCTMultilineTextInputViewManager(Patch)

+ (void)load {
 static dispatch_once_t once;
 dispatch_once(&once, ^{
  Class cls = [RCTMultilineTextInputViewManager class];
  Method orig = class_getInstanceMethod(cls, @selector(view));
  Method swz = class_getInstanceMethod(cls, @selector(rnp_view_swizzled));
  method_exchangeImplementations(orig, swz);
 });
}

- (UIView *)rnp_view_swizzled {
  // Custom INPUT
 return [[HMTextInputMultilineView alloc] initWithBridge:self.bridge];
}

@end

@implementation RCTSinglelineTextInputViewManager(Patch2)

+ (void)load {
 static dispatch_once_t once;
 dispatch_once(&once, ^{
  Class cls = [RCTSinglelineTextInputViewManager class];
  Method orig = class_getInstanceMethod(cls, @selector(view));
  Method swz = class_getInstanceMethod(cls, @selector(rnp_view_swizzled));
  method_exchangeImplementations(orig, swz);
 });
}

- (UIView *)rnp_view_swizzled {
 // Custom INPUT
 return [[HMTextInputSinglelineView alloc] initWithBridge:self.bridge];
}

@end

@implementation RCTUITextField(Patch3)

+ (void)load {
  static dispatch_once_t once;
  dispatch_once(&once, ^{
    Class cls = [RCTUITextField class];
    Method orig = class_getInstanceMethod(cls, @selector(paste:));
    Method swz = class_getInstanceMethod(cls, @selector(rnp_paste_swizzled:));
    method_exchangeImplementations(orig, swz);
  });
}

-(void) rnp_paste_swizzled:(id)sender {
  UIView *v = self;

  // Find superview (HMTextInputSinglelineView)
  while (v != nil && ![v isKindOfClass:[HMTextInputSinglelineView class]]) {
    v = v.superview;
  }

  if(v != nil){
    HMTextInputSinglelineView *input = (HMTextInputSinglelineView *)v;
    if(input.onPasteImage != nil){
      [input pasteWithDefaultHandler:^{
        [self rnp_paste_swizzled:sender];
      }];
      return;
    }
  }

  [self rnp_paste_swizzled:sender];
}


@end

@implementation RCTUITextView(Patch4)

+ (void)load {
  static dispatch_once_t once;
  dispatch_once(&once, ^{
    Class cls = [RCTUITextView class];
    Method orig = class_getInstanceMethod(cls, @selector(paste:));
    Method swz = class_getInstanceMethod(cls, @selector(rnp_paste_swizzled:));
    method_exchangeImplementations(orig, swz);
  });
}

-(void) rnp_paste_swizzled:(id)sender {
  UIView *v = self;

  // Find superview (HMTextInputMultilineView)
  while (v != nil && ![v isKindOfClass:[HMTextInputMultilineView class]]) {
    v = v.superview;
  }

  if(v != nil){
    HMTextInputMultilineView *input = (HMTextInputMultilineView *)v;

    if(input.onPasteImage != nil){
      [input pasteWithDefaultHandler:^{
        [self rnp_paste_swizzled:sender];
      }];
      return;
    }
  }

  [self rnp_paste_swizzled:sender];
}

@end


