//
//  JDProgressHUD.m
//  kneet2
//
//  Created by Jason Lee on 10/5/15.
//  Copyright © 2015 ntels. All rights reserved.
//
#import "JDObject.h"
#import "JDProgressHUD.h"

@interface JDProgressHUD () {
    BOOL useAnimation;
    id targetForExecution;
    id objectForExecution;
}

@property (copy) JDFacadeCompletionCallBackHandler completionBlock;
@property (assign) BOOL taskInProgress;
@property (atomic, strong) NSTimer *graceTimer;
@property (assign) float graceTime;


@end

@implementation JDProgressHUD

- (void)showLoadingWhileExecutingBlock:(dispatch_block_t)block {
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    [self showLoadingWhileExecutingBlock:block onQueue:queue completionBlock:NULL];
}

- (void)showLoadingWhileExecutingBlock:(dispatch_block_t)block completionHandler:(JDFacadeCompletionCallBackHandler)handler {
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    [self showLoadingWhileExecutingBlock:block onQueue:queue completionBlock:handler];
}

- (void)showLoadingWhileExecutingBlock:(dispatch_block_t)block onQueue:(dispatch_queue_t)queue completionBlock:(JDFacadeCompletionCallBackHandler)completion {
    
    self.taskInProgress = YES;
    self.completionBlock = completion;
    dispatch_async(queue, ^(void) {
        block();
        dispatch_async(dispatch_get_main_queue(), ^(void) {
            [self cleanUp];
        });
    });
    [self showInView:[CommonUtil topView]];
}

- (void)cleanUp {
    _taskInProgress = NO;
#if !__has_feature(objc_arc)
    [targetForExecution release];
    [objectForExecution release];
#else
    targetForExecution = nil;
    objectForExecution = nil;
#endif
    [self dismissAnimated:YES];
    
    if (self.completionBlock) {
        self.completionBlock();
        self.completionBlock = NULL;
    }
}

//
//- (void)dismissAnimated:(BOOL)animated {
//    [super dismissAnimated:animated];
//    
//    
//}

@end
