//
//  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;
    UIImageView* _animatedView;
    UIActivityIndicatorView *_indicatorView;
}

@end

@implementation CustomLoadingView

static CustomLoadingView *__loadingView = nil;

+ (void)showWithView:(UIView *)superView {
    if ([__loadingView superview]) {
        return;
    }
    
    CGRect screenRect = [UIScreen mainScreen].bounds;
    __loadingView = [[CustomLoadingView alloc] initWithFrame:screenRect];
    
    [superView addSubview:__loadingView];
    [__loadingView start];
}

+ (void)hide {
    if (__loadingView) {
        [__loadingView stop];
        [__loadingView 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.65;
        [self addSubview:_bgView];
        
        CGRect lRect = CGRectMake(0, 0, 90, 90);
        
        _indicatorView = [[UIActivityIndicatorView alloc] initWithFrame:lRect];
        _indicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
        
        CGRect center = [CommonUtil getCenterPosition:_indicatorView fromContainer:self hasNavigationBar:YES hasTabBar:NO];
        _indicatorView.frame = center;
        [self addSubview:_indicatorView];
    }
    return self;
}



- (void)start {
    [_indicatorView startAnimating];
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

- (void)stop {
    [_indicatorView stopAnimating];
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

@end
