//
//  InputScrollViewPlugin.m
//  InputScrollViewPlugin
//
//  Created by LvBingru on 4/11/16.
//  Copyright © 2016 erica. All rights reserved.
//

#import "InputScrollViewPlugin.h"
#import <React/RCTShadowView.h>
#import <React/RCTUIManager.h>

@implementation InputScrollViewPlugin

RCT_EXPORT_MODULE();

@synthesize bridge = _bridge;

RCT_EXPORT_METHOD(isSubview:(nonnull NSNumber *)reactTag
                  relativeTo:(nonnull NSNumber *)ancestorReactTag
                  callback:(RCTResponseSenderBlock)callback)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIView *view = [_bridge.uiManager viewForReactTag:reactTag];
        UIView *ancestorView = [_bridge.uiManager viewForReactTag:ancestorReactTag];
        
        while (view) {
            view = view.superview;
            if (view == ancestorView) {
                if (callback) {
                    callback(@[@(YES)]);
                }
                return;
            }
        }
        
        if (callback) {
            callback(@[@(NO)]);
        }
    });
}

RCT_EXPORT_METHOD(isTextInput:(nonnull NSNumber *)reactTag
                  callback:(RCTResponseSenderBlock)callback)
{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIView *view = [_bridge.uiManager viewForReactTag:reactTag];
        
        //删除对RCTTextView 和 RCTTextField的引用，不做内部引用，检查属性是否存在同样可以适用
        if ([view respondsToSelector:@selector(reactPaddingInsets)]) {
            if (callback) {
                callback(@[@(YES)]);
            }
        }
        else {
            if (callback) {
                callback(@[@(NO)]);
            }
        }
    });
}


@end
