//
//  RNSwipeableManager.m
//  humand
//
//  Created by Oleksandr Shumihin on 16/2/26.
//  Copyright © 2026 Humand. All rights reserved.
//

#import <Foundation/Foundation.h>

#import <React/RCTViewManager.h>
#import "Humand-Swift.h"

@interface RNSwipeableManager : RCTViewManager
@end

@implementation RNSwipeableManager

+ (BOOL)requiresMainQueueSetup
{
  return NO;
}

RCT_EXPORT_MODULE(RNSwipeableView)

- (UIView *)view
{
  return [[RNSwipeableView alloc] init];
}

RCT_EXPORT_VIEW_PROPERTY(onSwipeableBegan, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onSwipeableFinish, RCTDirectEventBlock)

RCT_CUSTOM_VIEW_PROPERTY(maxLeftOffset, float, RNSwipeableView) {
  float maxLeftOffset =
      json ? [RCTConvert float:json] : defaultView.maxOffsetLeft;
  view.maxOffsetLeft = maxLeftOffset;
}

RCT_CUSTOM_VIEW_PROPERTY(maxRightOffset, float, RNSwipeableView) {
  float maxRightOffset =
      json ? [RCTConvert float:json] : defaultView.maxOffsetRight;
  view.maxOffsetRight = maxRightOffset;
}

RCT_CUSTOM_VIEW_PROPERTY(thresholdOpenLeft, float, RNSwipeableView) {
  view.thresholdOpenLeft =  json ? [RCTConvert float:json] : defaultView.thresholdOpenLeft;
}

RCT_CUSTOM_VIEW_PROPERTY(thresholdCloseLeft, float, RNSwipeableView) {
  view.thresholdCloseLeft = json ? [RCTConvert float:json] : defaultView.thresholdCloseLeft;
}

RCT_CUSTOM_VIEW_PROPERTY(thresholdOpenRight, float, RNSwipeableView) {
  view.thresholdOpenRight = json ? [RCTConvert float:json] : defaultView.thresholdOpenRight;
}

RCT_CUSTOM_VIEW_PROPERTY(thresholdCloseRight, float, RNSwipeableView) {
  view.thresholdCloseRight = json ? [RCTConvert float:json] : defaultView.thresholdCloseRight;
}

RCT_EXPORT_METHOD(resetAll){
  dispatch_async(dispatch_get_main_queue(), ^{
    [RNSwipeableView closeAll];
  });
}

RCT_EXPORT_METHOD(reset:(nonnull NSNumber *)reactTag){
  [self.bridge.uiManager addUIBlock:^(
      RCTUIManager *uiManager,
      NSDictionary<NSNumber *, UIView *> *viewRegistry) {

    UIView *view = viewRegistry[reactTag];
    if (!view) return;

    RNSwipeableView *swipe = (RNSwipeableView *)view;
    [swipe reset];
  }];
}

@end
