//  CustomLoadingView
//  JasonDevelop
//
//  Created by Jason Lee on 10/23/14.
//  Copyright (c) jasondevelop. All rights reserved.
//
#import "CommonUtil.h"
#import "CustomLoadingView.h"

@interface CustomLoadingView () {
    UIView *_bgView, *_containerView;
    UIImageView* _animatedView;
    UIActivityIndicatorView *_indicatorView;
    
    UILabel *_lblMessage;
    UIButton *_btnCancel;
    
    id targetForExecution;
    id objectForExecution;
}

@end

@implementation CustomLoadingView

static CustomLoadingView *__loadingView = nil;

#pragma mark - Singleton

+ (CustomLoadingView *)loadingView {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        CGRect screenRect = [UIScreen mainScreen].bounds;
        __loadingView = [[CustomLoadingView alloc] initWithFrame:screenRect];
    });
    return __loadingView;
}

- (void)showInView:(UIView *)container {
    if ([self superview]) {
        return;
    }
    
    [container addSubview:self];
    [self start];
}

- (void)showInView:(UIView *)container whileExecutingBlock:(dispatch_block_t)block {
    [self showInView:container whileExecutingBlock:block completionHandler:nil];
}

- (void)showInView:(UIView *)container whileExecutingBlock:(dispatch_block_t)block completionHandler:(JDFacadeCompletionCallBackHandler)completion {
    if ([self superview]) {
        return;
    }
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    self.completionBlock = completion;
    
    [container addSubview:self];
    [self start];
    
    dispatch_async(queue, ^(void) {
        block();
        dispatch_async(dispatch_get_main_queue(), ^(void) {
            [self cleanUp];
        });
    });
}

- (void)setOperation:(NSOperation *)operation {
    if (!_operation) {
        _operation = operation;
    }
}

- (void)hide {
    if (self) {
        [self stop];
        [self removeFromSuperview];
    }
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        
        _bgView = [[UIView alloc] initWithFrame:self.frame];
        _bgView.backgroundColor = [UIColor darkGrayColor];
        _bgView.alpha = 0.35;
        [self addSubview:_bgView];

        
        //container
        _containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 127, 40)];
        _containerView.backgroundColor = RGBCOLOR(248, 102, 103);
//        _containerView.backgroundColor = [UIColor blackColor];
        _containerView.layer.cornerRadius = 18.0f;
        
        //indicator
        CGRect lRect = CGRectMake(5, 5, 30, 30);
        
        _indicatorView = [[UIActivityIndicatorView alloc] initWithFrame:lRect];
        _indicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
        _indicatorView.frame = lRect;
        
        //Label
        _lblMessage = [[UILabel alloc] initWithFrame:CGRectMake(38, 5, 60, 30)];
        _lblMessage.font = [UIFont systemFontOfSize:14.0f];
        _lblMessage.text = @"Loading";
        _lblMessage.textColor = [UIColor whiteColor];
        
        //button
        _btnCancel = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnCancel.frame = CGRectMake(97, 10, 20, 20);
        [_btnCancel setImage:[UIImage imageNamed:@"common_list_btn_del"] forState:UIControlStateNormal];
        [_btnCancel addTarget:self action:@selector(btnCancelTouched:) forControlEvents:UIControlEventTouchUpInside];

        CGRect center = [CommonUtil getCenterPosition:_containerView fromContainer:self hasNavigationBar:YES hasTabBar:NO];
        _containerView.frame = center;
        
        [_containerView addSubview:_indicatorView];
        [_containerView addSubview:_lblMessage];
        [_containerView addSubview:_btnCancel];
        
        [self addSubview:_containerView];
    }
    return self;
}



- (void)start {
    [_indicatorView startAnimating];
}

- (void)stop {
    [_indicatorView stopAnimating];
}

- (void)btnCancelTouched:(id)sender {
    if (_operation.isExecuting) {
        [_operation cancel];
    }
    
    [self cleanUp];
}

- (void)cleanUp {
    if (_operation) {
        _operation = nil;
    }
    
    [self stop];
    [self removeFromSuperview];
    
    if (self.completionBlock) {
        self.completionBlock();
        self.completionBlock = NULL;
    }
}

@end
