//
//  SsRefreshHeader.m
//  react-native-ss-rn-native-pull-refresh
//
//  Created by SHEN on 2020/12/4.
//

#import "SsRefreshHeader.h"
#import <ss_rn_native_pull_refresh/ss_rn_native_pull_refresh-Swift.h>

@interface SsRefreshHeader ()
@property (nonatomic, copy) RCTDirectEventBlock pullBlock;
@property (nonatomic, copy) RCTDirectEventBlock stateChangeBlock;
@end

@implementation SsRefreshHeader

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    return self;
}

- (void)dealloc {
    NSLog(@"SsRefreshHeader dealloc");
}

- (void)setOnPull:(RCTDirectEventBlock)onPull {
    self.pullBlock = onPull;
}

- (void)setOnStateChange:(RCTDirectEventBlock)onStateChange {
    self.stateChangeBlock = onStateChange;
}

- (void)setBgColor:(NSString *)bgColor {
    self.backgroundColor = [[UIColor alloc] initWithHex:bgColor]; 
}

- (void)setRefreshing:(BOOL)refreshing {
    if (refreshing) {
        [self beginRefreshing];
    } else {
        [self endRefreshing]; 
    }
}

- (void)setState:(MJRefreshState)state {
    [super setState:state]; 
    if (self.stateChangeBlock) {
        self.stateChangeBlock(@{@"state": @(state)});
    }
} 

- (void)setPullingPercent:(CGFloat)pullingPercent {
    [super setPullingPercent:pullingPercent];
    if (self.pullBlock) {
        
        self.pullBlock(@{@"percent": @(pullingPercent), @"offset": @(self.scrollView.contentOffset.y)}); 
    }
}
  
@end
