//
//  DDTimerObject.m
//  DDToolsKit_Private
//  Created by DDIsFriend on 2022/7/29.

#import "DDTimerObject.h"

typedef void(^TimerEventHandler)(DDTimerObject *_Nullable timerObject);

@interface DDTimerObject ()
@property (nonatomic, strong, nullable)dispatch_source_t timer;
@property (nonatomic, strong) dispatch_queue_t timerQueue;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSTimeInterval timeInterval;
@property (nonatomic, assign) NSUInteger repeatCount;
@property (nonatomic, assign, readwrite) DDTimerRunState runState;
@end

@implementation DDTimerObject
+ (DDTimerObject *)dd_timerObjectWithName:(NSString *)name timeInterval:(NSTimeInterval)timerInterval repeatCount:(NSInteger)repeatCount timerBlock:(nullable void(^)(DDTimerObject *_Nullable timerObject))timerBlock completionHandler:(nullable void(^)(DDTimerObject *_Nullable timerObject))completionHandler{
    DDTimerObject *timerObject = [[DDTimerObject alloc] init];
    timerObject.name = name;
    timerObject.timeInterval = timerInterval;
    [timerObject objectAddTimer:timerInterval repeatCount:repeatCount timerBlock:timerBlock completionHandler:completionHandler];
    return timerObject;
}


- (void)objectAddTimer:(NSTimeInterval)timerInterval repeatCount:(NSInteger)repeatCount timerBlock:(void(^)(DDTimerObject *_Nullable timerObject))timerBlock completionHandler:(nullable void(^)(DDTimerObject *_Nullable timerObject))completionHandler{
    self.repeatCount = 0;
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, self.timerQueue);
    dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, timerInterval * NSEC_PER_SEC, 0);
    __weak typeof(self) weakSelf = self;
    dispatch_source_set_event_handler(self.timer, ^{
        __strong typeof(weakSelf) strongSelf = weakSelf;
        ///run
        if (timerBlock) {
            timerBlock(strongSelf);
        }
        
        if ((repeatCount > 0 && strongSelf.repeatCount >= repeatCount)) {
            ///stop
            if (completionHandler) {
                completionHandler(strongSelf);
            }
            return;
        }
        
        strongSelf.repeatCount++;

    });
    
    self.runState = DDTimerRunStateNotActivate;
}

- (DDTimerRunState)dd_timerRunState{
    return self.runState;
}

- (void)dd_activateTimerObject{
    if (self.timer == nil) return;
    if (self.runState == DDTimerRunStateSuspend) return;
    if (self.runState == DDTimerRunStateActivate) return;
    
    dispatch_activate(self.timer);
    self.runState = DDTimerRunStateActivate;
}

- (void)dd_cancelTimerObject{
    if (self.timer == nil) return;
    if (self.runState == DDTimerRunStateNotActivate) return;
    dispatch_cancel(self.timer);
    NSLog(@"名为 ’%@‘ 的定时器取消了",self.name);
    self.timer = nil;
    self.runState = DDTimerRunStateNotActivate;
}

- (void)dd_suspendTimerObject{
    if (self.timer == nil) return;
    if (self.runState == DDTimerRunStateNotActivate) return;
    if (self.runState == DDTimerRunStateSuspend) return;
    
    dispatch_suspend(self.timer);
    NSLog(@"名为 ’%@‘ 的定时器暂停了",self.name);
    self.runState = DDTimerRunStateSuspend;
}

- (void)dd_resumeTimerObject{
    if (self.timer == nil) return;
    if (self.runState == DDTimerRunStateActivate) return;
    
    dispatch_resume(self.timer);
    NSLog(@"名为 ’%@‘ 的定时器恢复运行了",self.name);
    self.runState = DDTimerRunStateActivate;
}

#pragma mark -- lazy
- (dispatch_queue_t)timerQueue{
    if (!_timerQueue) {
        _timerQueue = dispatch_queue_create("timerObject_queue", DISPATCH_QUEUE_CONCURRENT);
    }
    return _timerQueue;
}
@end
