#import "RNCOnInsetsChangeEvent.h"
#import <React/RCTAssert.h>

@implementation RNCOnInsetsChangeEvent {
  UIEdgeInsets _insets;
  UIEdgeInsets _verticalCornerAdaptedInsets;
  UIEdgeInsets _horizontalCornerAdaptedInsets;
  CGRect _frame;
  uint16_t _coalescingKey;
}

@synthesize eventName = _eventName;
@synthesize viewTag = _viewTag;

- (instancetype)initWithEventName:(NSString *)eventName
                         reactTag:(NSNumber *)reactTag
                           insets:(UIEdgeInsets)insets
      verticalCornerAdaptedInsets:(UIEdgeInsets)verticalCornerAdaptedInsets
    horizontalCornerAdaptedInsets:(UIEdgeInsets)horizontalCornerAdaptedInsets
                            frame:(CGRect)frame
                    coalescingKey:(uint16_t)coalescingKey
{
  RCTAssertParam(reactTag);

  if ((self = [super init])) {
    _eventName = [eventName copy];
    _viewTag = reactTag;
    _frame = frame;
    _insets = insets;
    _verticalCornerAdaptedInsets = verticalCornerAdaptedInsets;
    _horizontalCornerAdaptedInsets = horizontalCornerAdaptedInsets;
    _coalescingKey = coalescingKey;
  }

  return self;
}

RCT_NOT_IMPLEMENTED(-(instancetype)init)

- (uint16_t)coalescingKey
{
  return _coalescingKey;
}

- (NSDictionary *)body
{
  NSDictionary *body = @{
    @"insets" : @{
      @"top" : @(_insets.top),
      @"right" : @(_insets.right),
      @"bottom" : @(_insets.bottom),
      @"left" : @(_insets.left),
    },
    @"verticalCornerAdaptedInsets" : @{
      @"top" : @(_verticalCornerAdaptedInsets.top),
      @"right" : @(_verticalCornerAdaptedInsets.right),
      @"bottom" : @(_verticalCornerAdaptedInsets.bottom),
      @"left" : @(_verticalCornerAdaptedInsets.left),
    },
    @"horizontalCornerAdaptedInsets" : @{
      @"top" : @(_horizontalCornerAdaptedInsets.top),
      @"right" : @(_horizontalCornerAdaptedInsets.right),
      @"bottom" : @(_horizontalCornerAdaptedInsets.bottom),
      @"left" : @(_horizontalCornerAdaptedInsets.left),
    },
    @"frame" : @{
      @"x" : @(_frame.origin.x),
      @"y" : @(_frame.origin.y),
      @"width" : @(_frame.size.width),
      @"height" : @(_frame.size.height),
    },
  };

  return body;
}

- (BOOL)canCoalesce
{
  return YES;
}

- (RNCOnInsetsChangeEvent *)coalesceWithEvent:(RNCOnInsetsChangeEvent *)newEvent
{
  return newEvent;
}

+ (NSString *)moduleDotMethod
{
  return @"RCTEventEmitter.receiveEvent";
}

- (NSArray *)arguments
{
  return @[ self.viewTag, RCTNormalizeInputEventName(self.eventName), [self body] ];
}

@end
