//
//  UIButton+ALReClicks.m
//  GMAlpha
//
//  Created by zhouLiang on 2019/4/4.
//  Copyright © 2019 Gengmei. All rights reserved.
//

#import "UIButton+ALReClicks.h"
#import <objc/runtime.h>

@implementation UIButton (ALReClicks)

static const char *UIControl_acceptEventInterval = "UIControl_acceptEventInterval";
static const char *UIControl_acceptEventTime = "UIControl_acceptEventTime";

- (NSTimeInterval)multiControlTimeInterva {
    return  [objc_getAssociatedObject(self, UIControl_acceptEventInterval) doubleValue];
}

- (void)setMultiControlTimeInterva:(NSTimeInterval)multiControlTimeInterva {
    objc_setAssociatedObject(self, UIControl_acceptEventInterval, @(multiControlTimeInterva), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSTimeInterval)cs_acceptEventTime {
    return [objc_getAssociatedObject(self, UIControl_acceptEventTime) doubleValue];
}

- (void)setCs_acceptEventTime:(NSTimeInterval)cs_acceptEventTime {
    objc_setAssociatedObject(self, UIControl_acceptEventTime, @(cs_acceptEventTime), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
    
    if (self.multiControlTimeInterva == 0) {
        self.multiControlTimeInterva = 0;
    }
    
    if (self.multiControlTimeInterva > 0) {
        
        if ([NSDate date].timeIntervalSince1970 - self.cs_acceptEventTime < self.multiControlTimeInterva) {
            return;
        }
        self.cs_acceptEventTime = [NSDate date].timeIntervalSince1970;
    }
    [super sendAction:action to:target forEvent:event];
}

@end
